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?

LoneXcoder
  • 2,121
  • 6
  • 38
  • 76
  • 1
    I can't understand the relationship between your snippets and your code example (e.g. `fullClassName`, `shrtNm`, `MethodName()`, `ConstantntStrName`). And it is *very* unclear what you are trying to do or ask. – lc. Sep 28 '12 at 02:32
  • 1
    @LoneXcoder you could at least correct the enumerous typos in your question... – horgh Sep 28 '12 at 02:40
  • @lc i can undarstand you did not , but can't understand your attetude downVoting i did work hard to make my question with two examples of usage, and as clear as i can . i am not a `main language=english` kind of guy , what i am asking is a solution for having a string type of values inside the IntelliSense to use on a Switch for example – LoneXcoder Sep 28 '12 at 02:41
  • @LoneXcoder - Just because lc posted a comment does not mean they gave you the downvote as well. People often give a downvote without explanation. – Tim Sep 28 '12 at 02:45
  • any help within the few comments ?? please ? – LoneXcoder Sep 28 '12 at 02:46
  • 1
    There is no way to get strings in intellisense as far as I know. That's what enums are for. Can I suggest using an enum and decorating the values with a string attribute like the example *in the question* http://stackoverflow.com/questions/4367723/get-enum-from-description-attribute? (Although in your example you do not need the switch and can just do `Response.Redirect(ElvaetTo + "Page.aspx")`, so I fail to see what you're trying to accomplish) – lc. Sep 28 '12 at 02:47
  • so you mean setting an attribute on top of enum declaration suggesting its string type ? then maybe make that class as static so not having to switch between declararion with the class name vs the name of the instance having to keep in mind which data is where...(for your lasr comment @lc) i am updating my post with "its only an example" – LoneXcoder Sep 28 '12 at 02:53
  • 1
    Yes, although if it's *always* the same as the enum value you don't need the attribute, and I would go your `ToString` route. ...Or you could use a `Dictionary` if you want to keep everything in `string`. I like the enums better though since it exhaustively lists (and thus restricts) the values you can have. But I don't know your use case so can't really comment much. (this is getting a bit too discussion-y for the Comments here, but I still don't know enough to post a full answer) – lc. Sep 28 '12 at 03:00

2 Answers2

2

Instead of declaring the variables as 'const' have your tired declaring them as 'readonly' ? This would enable you to simply instantiat the class only once :

fullClassName shrtNm = New FullClassName();

then you would like to call it as with

shrtNm.<VariableName>;

Guessing from the use case you enumerated, I doubt the difference between using const and readonly should matter...

Amar
  • 56
  • 2
  • @Amar . thanks for the kind answer even though lc corrected you with the comment that it's not correct to use it with switch (didnt test it . i can assume it's not (judging by his score (: ) so can you please atleast upvote my post i did re edit it too . (* – LoneXcoder Sep 28 '12 at 03:06
  • @Amar by the way...readonly does not work(with switch case) at all , even within the same scope /class /method – LoneXcoder Sep 28 '12 at 03:14
0

as far as i could get (only because it was important for me to addapt an aproach ) i found an example in a codeProjec page about using strings and String-Enumerations

that lead me to use that aproach and then turn my calss into a static class

 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";
        }

}

using sealed class accessing it via its parent className.Itsname e.g.

Qs.Act.edit

as edit would show in IntelliSense

LoneXcoder
  • 2,121
  • 6
  • 38
  • 76