重定向到类方法 [英] Redirect to class method

查看:90
本文介绍了重定向到类方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 得到了接口IMathOperationDemo,其中定义了Normale类的方法.

Hi Got interface IMathOperationDemo where are defined the methods of class Normale.

IMathOperationDemo CurrentOperation;
       private void comboShperndarjet_SelectedIndexChanged(object sender, EventArgs e)
       {
           switch (comboShperndarjet.SelectedItem.ToString)
           {
               case "Normale":
                   decimal Alpha = numericUpAlpha.Value;
                   CurrentOperation = new Normale();
                   break;



在主表单中有一个组合框,想要填充一些文本框
通过在案例Normale中访问类Normal方法.

我怎样才能做到这一点?

关于



In the main form got a combo-box and want to fill some textbox
by accessing class Normal methods inside the case Normale.

How can I make this possible?

Regards

推荐答案

仅在switch语句中使用立即数字符串常量(硬编码)的事实使您的情况几乎毫无希望.这种样式与实际编程无关.您也没有显示Normale类,也没有解释组合框列表项对应的内容.

(一种可接受的解决方案:将非字符串描述性数据存储在组合框中,覆盖object.ToString以便在组合框中显示为字符串;由于您的问题表达方式不正确,我不想提供更多详细信息.) br/>
-SA
The mere fact that you use an immediate string constant (hard-coded) in a switch statement makes your case nearly hopeless. This style has nothing to do with real programming. You also did not show the class Normale and explain what the combo box list items correspond to.

(One of acceptable solutions: store non-string descriptive data in combo-box, override object.ToString to show as string in the combo box; I don''t want to give more detail because you question is not properly formulated).

—SA


这似乎是策略模式的完美用例: ^ ]

我可能会将字符串转换为常量.然后,您可以拥有一个字典,其中包含映射字符串常量->.实现类(在您的示例中:"Normale" ...).重要的是它们都有一个共同的超类(或与此相关的接口).这样,您就有了Dictionary<IMathOperationDemo>.看起来您已经有了此界面,这很好.

考虑一下,您可以按照在那做的方式来做.只需在构造函数中传递Alpha和Beta.你不需要我想的字典.只需将字符串转换为常量!

为什么所有方法中都有字符串参数?您不使用它们...
This seems to be a perfect use case for the strategy pattern:Wikipedia[^]

I would probably turn the Strings into constants. Then you can have a dictionary with the mapping String Constant -> Implementation Class (in your example: "Normale"...). It is important that they all have a common superclass (or Interface for that matter). That way you have a Dictionary<IMathOperationDemo>. It looks like, that you already have this interface, which is good.

Thinking about it, you could just do it the way you did up there. Just pass Alpha and Beta in the Constructor. You don''t need the dictionary I suppose. Just turn the Strings into Constants!

Why are there string arguments in all the methods? You do not use them...


这篇关于重定向到类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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