引用另一个类中一个类的方法 [英] Referencing a method in one class in another class

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

问题描述

背景:大约4周C#,请耐心等待。我有一个下拉框,用户将项目状态更新为活动或非活动。



我有以下方法为我的下拉列表创建和赋值:

Background: About 4 weeks C#, please bear with me. I have a drop down box where users update the status of a project as either Active or Inactive.

I have the following methods that create and assign values to my dropdown list:

public class CommonSVC
    {
        #region Fill Item Status
        public void FillItemStatus(DropDownList uxStatusDdl, bool blankEntry, string blankEntryText, bool includeBothListItem)
        {
            if (includeBothListItem)
            {
                uxStatusDdl.Items.Add(new ListItem("Both", "-1"));
            }
            uxStatusDdl.Items.Add(new ListItem("Active", "0"));
            uxStatusDdl.Items.Add(new ListItem("Inactive", "1"));

            FillControl.AddBlankAndAllValues(uxStatusDdl, false, "", blankEntry, blankEntryText);
        }
        #endregion

        #region Fill Yes No
        public void FillYesNo(DropDownList ddl, bool blankEntry, string blankEntryText, bool includeBothListItem)
        {
            if (includeBothListItem)
            {
                ddl.Items.Add(new ListItem("Both", "-1"));
            }
            ddl.Items.Add(new ListItem("Yes", "1"));
            ddl.Items.Add(new ListItem("No", "0"));
            FillControl.AddBlankAndAllValues(ddl, false, "", blankEntry, blankEntryText);        
        }
        #endregion 

        #region Get Yes No Value By DDL Value
        public static string GetYesNoValueByDDLValue(string ddlValue)
        { 
            switch (ddlValue)
            {
                case "0":
                   return "0";
                case "1":
                    return "1";
                case "-1":
                    return "0,1";
            default:
                    return string.Empty;
            }
        }
        #endregion



调用项目状态更改的实际方法是:


The actual method that invokes a change to my project status is:

: accountTrackersvc.UpdateConnectionType(DataConverter.StringToInteger(ViewState["ConnectionTypeID"].ToString()),
uxConnectionTypeDescTxt.Text.Trim(),
Enums.GetIsDisabledByItemStatusValue(SafeValueAccessor.GetControlValue(uxStatusDdl)), /* This line updates project status */
CommonSVC.GetUserInfoFormattedFromSession());





我为GetIsDisabledByItemStatusValue()方法生成了一个方法存根,并进行了一些更改,到目前为止,这就是我所拥有的:





I've generated a method stub for the GetIsDisabledByItemStatusValue() method and made some changes as well, so far this is what I've got:

public static object GetIsDisabledByItemStatusValue(string p)
       {
           bool ActiveIndicator = true;
           if (ActiveIndicator) //Then I want to set the status to Active
           {
               CommonSVC.GetYesNoValueByDDLValue(string ddlValue) // error here reads: invalid expression term

           }

           return p;
       }
       #endregion





你会注意到感兴趣的方法是在一个枚举类中,我在一个预先构建的模板中工作,因此我只能使用我已经存在的东西。上面的前2个代码块中没有错误。我的问题是:如何在enums类中实现一个方法,该方法将连接我的CommonSVC类和调用类的2个代码块?



You'll notice that the method of interest is within an enum class, I'm working within a pre-built template and thus am confined to work with what I have already there. There are no errors in the top 2 code blocks above. My question is: how would I implement a method in the enums class that would connect the 2 blocks of code from my CommonSVC class and the invocation class?

推荐答案

问题在这里:

The problem is here:
CommonSVC.GetYesNoValueByDDLValue(string ddlValue)

并且(如果我们忽略丢失的分号),问题是你想将GetIsDisabledByItemStatusValue参数传递给GetYesNoValueByDDLValue方法并保存结果。所以试试:

And (if we ignore the missing semicolon), the problem is that you want to pass the GetIsDisabledByItemStatusValue parameter to the GetYesNoValueByDDLValue method and save teh result. So try:

string ddlValue = CommonSVC.GetYesNoValueByDDLValue(p);

并且你的错误信息应该消失。



但是,请帮个忙,并且不要为每种方法使用新的区域。使用区域通过各种方式对相关方法进行分组,但方法只需要一个XML注释标题: C#Documenting and评论 [ ^ ]如果你这样做,它们很容易看到,当你尝试使用这种方法时,Visual Studio Intelisense会接收它并帮助你。

如果你使用区域,那么你可以隐藏方法详细信息,您不需要 - 查看Visual Studio大纲功能: https:/ /msdn.microsoft.com/en-us/library/td6a5x4s.aspx [ ^ ] - 做同样的事情,但更灵活。

And your error message should go away.

But please, do yourself a favour, and don't use a new region for each method. Use regions to group related methods by all means, but methods just need an XML comment header: C# Documenting and Commenting[^] If you do that, they are easy to see, and Visual Studio Intelisense will pick up on it and help you when you try to use the method.
If you are using regions so you can "hide" method details, you don't need to - have a look at the Visual Studio Outlining feature: https://msdn.microsoft.com/en-us/library/td6a5x4s.aspx[^] - does the same thing, but a lot more flexibly.


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

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