Передача параметров - C#

Узнай цену своей работы

Формулировка задачи:

Доброго времени суток всем! Возникла такая проблема, очень прошу помощи. Реализую паттерн MVP. После переноса бизнес логики приложения из Модели в Презентер программа перестала складывать и выдаёт 0. Есть подозрения, что где-то накосячил с правильным вызовом методов. Использую MS Visual Studio 2015. Сам проект из студии прикладываю. Заранее всем благодарен за советы!

View

Листинг программы
  1. using MVPPattern.Presenter;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace MVPPattern
  12. {
  13. public partial class ViewForm : Form
  14. {
  15. public PresenterClass Presenter { get; set; }
  16. public ViewForm()
  17. {
  18. InitializeComponent();
  19. }
  20. private void txtOperand1_TextChanged(object sender, EventArgs e)
  21. {
  22. try
  23. {
  24. Presenter.OnOperand1Changed(Int32.Parse(txtOperand1.Text));
  25. }
  26. catch (Exception)
  27. {
  28. MessageBox.Show("Ошибка введённых данных", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
  29. }
  30. }
  31. private void txtOperand2_TextChanged(object sender, EventArgs e)
  32. {
  33. try
  34. {
  35. Presenter.OnOperand2Changed(Int32.Parse(txtOperand2.Text));
  36. }
  37. catch (Exception)
  38. {
  39. MessageBox.Show("Ошибка введённых данных", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
  40. }
  41. }
  42. private void btAdd_Click(object sender, EventArgs e)
  43. {
  44. Presenter.OnAddCliked();
  45. }
  46. public void UpdateView(Int32 operand1, Int32 operand2, Int32 result)
  47. {
  48. txtOperand1.Text = operand1.ToString();
  49. txtOperand2.Text = operand2.ToString();
  50. txtResult.Text = result.ToString();
  51. }
  52. }
  53. }

Presenter

Листинг программы
  1. using MVPPattern.Model;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace MVPPattern.Presenter
  8. {
  9. public class PresenterClass
  10. {
  11. private ModelClass model;
  12. private ViewForm view;
  13. public PresenterClass(ModelClass model, ViewForm view)
  14. {
  15. this.model = model;
  16. this.view = view;
  17. view.Presenter = this;
  18. }
  19. public void OnOperand1Changed(Int32 operand1)
  20. {
  21. model.set_Operand1(operand1);
  22. }
  23. public void OnOperand2Changed(Int32 operand2)
  24. {
  25. model.set_Operand2(operand2);
  26. }
  27. protected void Add() {/*конструктор без параметров*/} //подозрения, что делаю что-то не то именно тут
  28. public void Add(Int32 result)
  29. {
  30. // model.Add();
  31. //model.set_Result(result) = model.get_Operand1() + model.get_Operand2(); //Result = Operand1 + Operand2;
  32. //result = model.get_Operand1() + model.get_Operand2();
  33. //model.set_Result(result);
  34. result = model.get_Operand1() + model.get_Operand2();
  35. model.set_Result(result);
  36. }
  37. public void OnAddCliked()
  38. {
  39. Add();
  40. UpdateView();
  41. }
  42. private void UpdateView()
  43. {
  44. view.UpdateView(model.get_Operand1(), model.get_Operand2(), model.get_Result());
  45. }
  46. }
  47. }

Model

Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace MVPPattern.Model
  7. {
  8. public class ModelClass
  9. {
  10. //данные
  11. //operand1
  12. private Int32 Operand1;
  13. public void set_Operand1(Int32 value)
  14. {
  15. Operand1 = value;
  16. }
  17. public Int32 get_Operand1()
  18. {
  19. return Operand1;
  20. }
  21. //operand2
  22. private Int32 Operand2;
  23. public void set_Operand2(Int32 value)
  24. {
  25. Operand2 = value;
  26. }
  27. public Int32 get_Operand2()
  28. {
  29. return Operand2;
  30. }
  31. //result
  32. private Int32 Result;
  33. public void set_Result(Int32 value) //было private
  34. {
  35. Result = value;
  36. }
  37. public Int32 get_Result()
  38. {
  39. return Result;
  40. }
  41. //бизнес логика
  42. /* была раньше тут
  43. public void Add()
  44. {
  45. Result = Operand1 + Operand2;
  46. }
  47. */
  48. }
  49. }

Если нужно, тело программы

Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows.Forms;
  5. using MVPPattern.Model;
  6. using MVPPattern;
  7. using MVPPattern.Presenter;
  8. namespace WindowsFormsApplication1
  9. {
  10. static class Program
  11. {
  12. /// <summary>
  13. /// The main entry point for the application.
  14. /// </summary>
  15. [STAThread]
  16. static void Main()
  17. {
  18. Application.EnableVisualStyles();
  19. Application.SetCompatibleTextRenderingDefault(false);
  20. //model
  21. ModelClass model = new ModelClass();
  22. //view
  23. ViewForm view = new ViewForm();
  24. //presenter
  25. PresenterClass presenter = new PresenterClass(model, view);
  26.  
  27. Application.Run(view);
  28. }
  29. }
  30. }

Решение задачи: «Передача параметров»

textual
Листинг программы
  1. public void Add()
  2. {
  3.          Int32 result;
  4.          result = model.get_Operand1() + model.get_Operand2();
  5.          model.set_Result(result);          
  6. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

8   голосов , оценка 4 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут