0

When working with switch case, for example I could use

const string FirstFloor = "lvl1", SecondFloor = "lvl2", ThirdFloor = "lvl3"; 

string ElavaetTo= "lvl1";

switch(ElavaetTo)
{
  case FirstFloor:
  Response.Redirect(FirstFloor + "Page.aspx")
  break;

  case SecondFloor:
  Response.Redirect(SecondFloor + "Page.aspx")
  break;

  case ThirdFloor:
  Response.Redirect(ThirdFloor + "Page.aspx")
  break;       
}
  • Edited :

this is only an example of where constant string wil not work if placed in another class this is not a function / method i am trying to correct so it will work. thanks for your time, i am trying to base my methods , my approach...

This would work fine placed in the current or same class of the project but when all variables are stored outside this class instead of simply instantiating the class and methods only once :

fullClassName shrtNm = New fullClassName();

then you would like to call it as with

shrtNm.MethodName();

You need to go the 'Long way around' specially if not including the Namespace via using statement

and you would have to call it like:

string strnm = MyNameOfNameSpace.fullClassName.ConstantntStrName;

instead of:

string strnm = shrtNm.ConstantStrName;

Is there an alternative to using any type that will represent string values inside the IntelliSense in an easy way ?

I have tried to use

public enum Elavation
{
    lvl1,
    lvl2,
    lvl3
}

but then you need to declare it as in the long example plus a .ToString()

Is there any alternative at all?

4

2 回答 2

2

不是将变量声明为“const”,而是厌倦了将它们声明为“readonly”?这将使您能够仅将类实例化一次:

fullClassName shrtNm = New FullClassName();

那么您想将其称为

shrtNm.<VariableName>;

从您列举的用例中猜测,我怀疑使用 const 和 readonly 之间的区别应该很重要......

于 2012-09-28T02:43:40.360 回答
0

据我所知(只是因为添加方法对我来说很重要)我在codeProjec 页面中找到了一个关于使用字符串和字符串枚举的示例

这导致我使用这种方法,然后将我的课程变成静态课程

 public static class Qs
    {
        public sealed class Act
        {
            //private Act();
            public const string edit = "edit", add = "add", remove = "remove", replace = "replace";

        }
       public sealed class State
        {
           public const string addnewTableRow = "addnewTableRow", cancelInsert = "cancelInsert", loadpagefromlink="loadpagefromlink";
        }
       public sealed class Params
        {
             public const string state = "state";
             public const string custID = "custID";
             public const string recordID = "recordID";
        }

}

使用sealed class通过其父 className.Itsname 访问它,例如

Qs.Act.edit

编辑将在 IntelliSense 中显示

于 2012-09-29T18:36:19.940 回答