Передача параметров - C#
Формулировка задачи:
Доброго времени суток всем!
Возникла такая проблема, очень прошу помощи. Реализую паттерн MVP. После переноса бизнес логики приложения из Модели в Презентер программа перестала складывать и выдаёт 0. Есть подозрения, что где-то накосячил с правильным вызовом методов.
Использую MS Visual Studio 2015. Сам проект из студии прикладываю.
Заранее всем благодарен за советы!
View
using MVPPattern.Presenter;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MVPPattern
{
public partial class ViewForm : Form
{
public PresenterClass Presenter { get; set; }
public ViewForm()
{
InitializeComponent();
}
private void txtOperand1_TextChanged(object sender, EventArgs e)
{
try
{
Presenter.OnOperand1Changed(Int32.Parse(txtOperand1.Text));
}
catch (Exception)
{
MessageBox.Show("Ошибка введённых данных", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void txtOperand2_TextChanged(object sender, EventArgs e)
{
try
{
Presenter.OnOperand2Changed(Int32.Parse(txtOperand2.Text));
}
catch (Exception)
{
MessageBox.Show("Ошибка введённых данных", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btAdd_Click(object sender, EventArgs e)
{
Presenter.OnAddCliked();
}
public void UpdateView(Int32 operand1, Int32 operand2, Int32 result)
{
txtOperand1.Text = operand1.ToString();
txtOperand2.Text = operand2.ToString();
txtResult.Text = result.ToString();
}
}
}Presenter
using MVPPattern.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MVPPattern.Presenter
{
public class PresenterClass
{
private ModelClass model;
private ViewForm view;
public PresenterClass(ModelClass model, ViewForm view)
{
this.model = model;
this.view = view;
view.Presenter = this;
}
public void OnOperand1Changed(Int32 operand1)
{
model.set_Operand1(operand1);
}
public void OnOperand2Changed(Int32 operand2)
{
model.set_Operand2(operand2);
}
protected void Add() {/*конструктор без параметров*/} //подозрения, что делаю что-то не то именно тут
public void Add(Int32 result)
{
// model.Add();
//model.set_Result(result) = model.get_Operand1() + model.get_Operand2(); //Result = Operand1 + Operand2;
//result = model.get_Operand1() + model.get_Operand2();
//model.set_Result(result);
result = model.get_Operand1() + model.get_Operand2();
model.set_Result(result);
}
public void OnAddCliked()
{
Add();
UpdateView();
}
private void UpdateView()
{
view.UpdateView(model.get_Operand1(), model.get_Operand2(), model.get_Result());
}
}
}Model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MVPPattern.Model
{
public class ModelClass
{
//данные
//operand1
private Int32 Operand1;
public void set_Operand1(Int32 value)
{
Operand1 = value;
}
public Int32 get_Operand1()
{
return Operand1;
}
//operand2
private Int32 Operand2;
public void set_Operand2(Int32 value)
{
Operand2 = value;
}
public Int32 get_Operand2()
{
return Operand2;
}
//result
private Int32 Result;
public void set_Result(Int32 value) //было private
{
Result = value;
}
public Int32 get_Result()
{
return Result;
}
//бизнес логика
/* была раньше тут
public void Add()
{
Result = Operand1 + Operand2;
}
*/
}
}Если нужно, тело программы
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using MVPPattern.Model;
using MVPPattern;
using MVPPattern.Presenter;
namespace WindowsFormsApplication1
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//model
ModelClass model = new ModelClass();
//view
ViewForm view = new ViewForm();
//presenter
PresenterClass presenter = new PresenterClass(model, view);
Application.Run(view);
}
}
}Решение задачи: «Передача параметров»
textual
Листинг программы
public void Add()
{
Int32 result;
result = model.get_Operand1() + model.get_Operand2();
model.set_Result(result);
}