Link to home
Start Free TrialLog in
Avatar of RishadanPort
RishadanPort

asked on

Defining a constant array of constant strings inside a local scope

I need my method to do something like this:

public void method(List<string> strs){
   //Define constant array of constant strings
   <some constant type> string[] RequiredVariableNames = {"text", "text2", text3"};

   //declare loop
   for(int index = 0; index < RequiredVariableNames.Length; index++){
      if(str.Contains(RequiredVariableNames[index])){
         switch(RequiredVariableNames[index]){
                 case RequiredVariableNames[0]:   <--- compile error here since it is not constant
         }
      }
   }
}

Any advice
SOLUTION
Avatar of gregoryyoung
gregoryyoung
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RishadanPort
RishadanPort

ASKER

The actual code looks like what it is listed below.

What I am trying to do is if a certain thing does not appear in the MapLayout then I want to add it in myself.
        public void LoadIRCurves(MapLayout curveNames)
        {
             string[] RequiredVariableNames = new string[]{ "IRCurve", "MeanReversionCurve", "CapletVolCurve", "SwaptionVolCurve" };
 
            for (int index = 0; index < RequiredVariableNames.Length; index++)
            {
                //Variable was not found, add default variable into map
                if(curveNames.getVariable(RequiredVariableNames[index]) == null)
                {
                    switch (RequiredVariableNames[index])
                    {
                        case RequiredVariableNames[0]:
                            //Do something
                            break;
                        case RequiredVariableNames[1]:
                            //Do something
                            break;
                        case RequiredVariableNames[2]:
                            break;
                        case RequiredVariableNames[3]:
                            break;
                    }
                }
            }
        }

Open in new window

A MapLayout contains a group of Variables.

It may be possible that this function is invoked a variable missing. so what I want is that this switch statements adds in a default element into the MapLayout
If you have a suggestion on improving this, I will be happy to hear it.
here is an example what I could do, but this issues the same string twice, and I find it a bit harder to work with in compared to the previous:
        public void LoadIRCurves(MapLayout curveNames)
        {
            string[] RequiredVariableNames = new string[] { "IRCurve", "MeanReversionCurve", "CapletVolCurve", "SwaptionVolCurve" };
 
            for (int index = 0; index < RequiredVariableNames.Length; index++)
            {
                //Variable was not found, add default variable into map
                if (curveNames.getVariable(RequiredVariableNames[index]) == null)
                {
                    switch (RequiredVariableNames[index])
                    {
                        case "IRCurve":
                            //Do something
                            break;
                        case "MeanReversionCurve":
                            break;
                        case "CapletVolCurve":
                            break;
                        case "SwaptionVolCurve":
                            break;
                    }
                }
            }
        }

Open in new window

SOLUTION
Avatar of Dmitry G
Dmitry G
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sounds like you need an enumeration instead
Thanks for the idea anarki_jimbel, however it also cannot compile since it produces a "Constant value is expected" syntax error, at the case label
oh wait that worked...
Yes that worked. thanks. What was your suggestion jaime_olivares before I give points out?
I thing Gegoryyoung is right - why not use index:
public void method(List<string> strs){
   //Define constant array of constant strings
 
 const string c1 = "text1";
 const string c2 = "text2";
 const string c3 = "text3";
 string[] RequiredVariableNames = {c1,c2,c3};
   //declare loop
   for(int index = 0; index < RequiredVariableNames.Length; index++){
      if(strs[0].Contains(RequiredVariableNames[index])){
         switch(index){
                 case 0:  // <--- compile error here since it is not constant
                     break;
             case 1:
                 break;
         }
      }
   }
}

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ack... I wish I could give you some points too...
Is there any way to do that in a local scope though? I don't want to define that array outside the function.
no, you can't, but it is not an array, and enumeration declaration doesn't generate class members as an array.
Thanks a lot jaime_olivares... I like your way the best. I have requested attention from a moderator to come view this question and give me the ability to give you some points as well.

Thanks a lot!
glad to help :)
I don't mind splitting my points in any way.
So feel free to do that!
Thanks everyone