MVC,Windows窗体问题..请帮助. [英] MVC, Windows Form problem .. please help.

查看:55
本文介绍了MVC,Windows窗体问题..请帮助.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

>I am newbie and need your kind help. I am trying to implement MVC in a windows application which adds two number. I am using this project posted in this same website as a reference.
I have defined interface in forms.cs as below:
<code> //Interface definition for VIEW.
    interface iAddrView
    {
        void addListener(iAddrController controller);
        string sum
        {
            get;
            set;
        }
    }
    //Interface definition for CONTROLLER.
    public interface iAddrController
    {
        void OnClick (int operands);
        void adder();
    }
    //Interface definition for MODEL
    interface iAddrModel
    {
        int setInput(int number);
        //Supplied number processing and returning result.
        int adds(int operand1, int operand2);
        //Adds two supplied operands.
        void toAddState();
        //Change state to adding
        //int subs(int operand1, int operand2);
        //void toSubState();
        //Subtraction not implemented.
    }
</code>
I have separate class file which inherits the interface. My class file is as follows:
Controller Class
<code>namespace simpleAdder
{
    class saController : iAddrController
    {
        iAddrModel model;
        iAddrView  view;
        public saController(iAddrModel model, iAddrView view)
        {
            this.model = model;
            this.view  = view;
            this.view.addListener(this);
        }
        public void OnClick( int operands )
        {
            view.sum = model.setInput(number).ToString();
        }
        public void adds()
        {
            model.toAddState();
        }
    }
</code>
Model Class
<code>namespace simpleAdder
{
    class saModel : iAddrModel
    {
        public enum States { NoOperation, Add, Subtract };
        States state;
        int currentValue;
        public States State
        {
            set { state = value; }
        }
        public int SetInput(int number1)
        {
            if (state == States.NoOperation)
            {
                currentValue = number1;
            }
            else if (state == States.Add)
            {
                currentValue = Add(currentValue, number);
            }
            return currentValue;
        }
        public void toAddState()
        {
            this.state = States.Add;
        }
        public int adds(int operand1, int operand2)
        {
            return operand1 + operand2;
        }
        //public int Subtract(int value1, int value2)
        //{
        //    throw new System.ApplicationException(&quot; Not implemented yet&quot;);
        //}
    }
</code>
View Class
<code>public partial class saView : Form1, iAddrView
    {
        iAddrController controller;
        public saView()
        {
            InitializeComponent();
        }
        /// &lt;summary&gt;
        /// The view needs to interact with the controller to pass the click events
        /// This could be done with delegates instead.
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;controller&quot;&gt;&lt;/param&gt;
        public void addListener(iAddrController controller)
        {
            this.controller = controller;
        }
        private void lbl_Click(object sender, EventArgs e)
        {
            // Get the text out of the label to determine the letter and pass the click info the controller to distribute.
            controller.OnClick((Int32.Parse(((Label)sender).Text)));
        }
        private void lblPlus_Click(object sender, EventArgs e)
        {
            controller.adder();
        }
        #region iAddrView Members
        public string Total
        {
            get
            {
                return textBox1.Text;
            }
            set
            {
                textBox1.Text = value;
            }
        }
        #endregion
    }
</code>
The problem is as I build, it is shows an error saying
<pre lang="msil">Error   1   &#39;simpleAdder.saModel&#39; does not implement interface member &#39;simpleAdder.iAddrModel.setInput(int)&#39;
Error   2   &#39;simpleAdder.saView&#39; does not implement interface member &#39;simpleAdder.iAddrView.sum&#39;
Error   3   &#39;simpleAdder.saController&#39; does not implement interface member &#39;simpleAdder.iAddrController.adder()&#39;</pre>

Please kindly help me on this.</pre>

推荐答案

您必须更好地了解接口的作用及其实现. >
您需要学习相关的技术,这些技术非常简单,但需要深入了解设计基础.我建议您暂时搁置MVC,并在更轻松地设计,实现和使用接口和类之前,进一步提高处理更简单问题的技能.

You have to understand the role of interfaces and their implementation much better.

You need to learn related techniques which are very simple but require deep understanding of design fundamentals. I suggest you set aside MVC for a while and sharpen your skills on much more simple problems before you''re absolutely comfortable with designing, implementing and using interfaces and classes.

interface IAddrModel {
    int SetInput(int number);
    int Adds(int left, int right);
    void AddState();
}

class AddModel : IAddrModel {
    int IAddrModel.SetInput(int number) {
        /* set input here...*/ return number;
    }
    int IAddrModel.Adds(int left, int right) {
        /* for example...*/ return left + right;
    }
    void IAddrModel.AddState(/* ??? */) {
        /* add state... where are your parameters?! */
    }
    /* other members */
}



您应该实现所有接口的所有方法,不能跳过单个方法(就像您在代码编译中所做的一样).如果如图所示实现,则无法从类的实例访问接口的实现.为了使make易于访问,请使用不带限定名称(带有接口名称)的不同语法,改为使用具有相同名称的public方法.
我不理解您的toAddState方法.该名称的含义(注释具有误导性),并且没有参数怎么办?

(我还改进了命名方式,因为您破坏了所有命名方式(Microsoft推荐并且相当不错).这非常有用:



You should implement all the methods of all interfaces, you cannot skip a single one (as you did in your code failed to compile). If implemented as shown, the implementation of interface is not accessible from the instance of class. To make is accessible, use different syntax without qualified names (with interface name), use public method of the same names instead.
I don''t understand your toAddState method. What this name should mean (comment is misleading) and how can you do it without parameters?

(I also improved naming because you''re breaking all the naming styles (it is recommended by Microsoft and pretty good). This is very useful: http://msdn.microsoft.com/en-us/library/ms229002.aspx[^].)

And so on...

—SA


这篇关于MVC,Windows窗体问题..请帮助.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆