如何以两种不同形式使用同一方法? [英] How to use the same method with two different forms?

查看:106
本文介绍了如何以两种不同形式使用同一方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了阐明这一点:我有两种形式的控制器中使用相同方法的表单,我想知道如何使用相同的代码行,而不是复制和粘贴该方法并为每个方法使用不同的参数.

To clarify: I have two forms that use the same method in my Controller, and I was wondering how to use the same lines of code rather than copying and pasting the method and using a different parameter for each method.

例如.我现在所拥有的:

Eg. What I have now:

public static void PopulateOriginCombo(CableID_QueryView QView)
{
    if (QView.cmbAreaCode.Text != "")
    {
       //Code...
    }
}

public static void PopulateOriginCombo(CableID_CreateView CView)
{
    if (CView.cmbAreaCode.Text != "")
    {
       //Code...
    }
}

我可以将每种形式的参数组合成某种形式吗?

Can I combine the parameters of each form into one somehow?

推荐答案

您不需要使用继承来执行此操作.创建另一个包含您的方法并返回对象列表的类,然后以不同的形式使用它.

You don't need to use inheritance to do this. Create another class which contains your methods and returns list of objects, then use it on different forms.

public class Origin
{
    public string originName { get; set; }

    public static List<Origin> PopulateOriginCombo(CableID_QueryView QView)
    {
        if (QView.cmbAreaCode.Text != "")
        {
           //Code...
        }
    }

    public static List<Origin> PopulateOriginCombo(CableID_CreateView CView)
    {
        if (CView.cmbAreaCode.Text != "")
        {
           //Code...
        }
    }
}

然后在您的表单中,这样称呼它:

Then in your form, call it like this:

combo1.DataSource = Origin.PopulateOriginCombo(test);
combo1.DisplayMember = "originName";

一开始很难使用对象,但是最终您会发现它更易于操作.

Using objects is hard at first, but eventually you will find it easier to manipulate.

这篇关于如何以两种不同形式使用同一方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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