【问题标题】:How to set parameter in method like settings type如何在设置类型等方法中设置参数
【发布时间】:2013-06-15 23:10:35
【问题描述】:

我想创建具有某种设置的方法,当然我可以使用 int Para,然后使用 if==1、if==2 等的一些 if。但我不想要这个。我的谷歌技能不允许我搜索解决方案,因为我错过了一些单词或其他东西,一些短语。我想让它看起来像:

void Method(int var1, int var2, Settings.type.type1){

if(type1){


}
if(type2){


}
if(type3){

}
}

在框架的某些方法中,我看到了类似这样的内容:templateType.DefoultTemplate。 我不要字符串!我想要明确的参数。 对不起我的英语

【问题讨论】:

  • 很抱歉,但仍然真的不清楚你的目标是什么。

标签: c# .net variables methods parameters


【解决方案1】:

您在寻找enum 和switch:

public enum CustomType {
      Type1 = 1,
      Type2 = 2,
      Type3 = 3
 };

public void Method(CustomType t)
{
    switch (t)
    {
        case CustomType.Type1:
                  // code here
                  break;
        case CustomType.Type2:
                  // code here
                  break;
        case CustomType.Type3:

    }
}

【讨论】:

  • 使用 Type 会非常糟糕...这是一个类名。
  • @DonBoitnott - 同意。已修改。
  • 这么快的答案:) 我正在寻找的东西。如果有人会在帖子中编辑和纠正我的语法,我会很高兴,如果有人对标题有更好的想法,那也很好
【解决方案2】:

创建一个枚举 SettingsType 并将这些值添加到其中。然后只需添加 switch case 来做所有有条件的事情。

因此您可以像SettingsType.Type1 等一样访问它。

【讨论】:

  • 对模糊问题的模糊回答。代码中的示例怎么样?
  • @DonBoitnott 这些问题有这样的答案:)
  • 好吧,他说他错过了一个单词或短语,以便自己在谷歌中找到答案。我相信他正在寻找“枚举”
猜你喜欢
  • 2018-11-04
  • 1970-01-01
  • 2019-02-23
  • 2022-01-24
  • 1970-01-01
  • 1970-01-01
  • 2012-10-15
  • 1970-01-01
  • 2017-07-25
相关资源
最近更新 更多