Solved

Defining a constant array of constant strings inside a local scope

Posted on 2008-10-14
20
636 Views
Last Modified: 2013-12-17
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
0
Comment
Question by:RishadanPort
  • 11
  • 4
  • 3
  • +1
20 Comments
 
LVL 37

Assisted Solution

by:gregoryyoung
gregoryyoung earned 75 total points
ID: 22716786
It's probably a good thing that you are getting this error ...

when I see code like:

   for(int index = 0; index < RequiredVariableNames.Length; index++){
         switch(RequiredVariableNames[index]){
                 case RequiredVariableNames[0]:
         }
   }

I generally consider it dubious ... I mean you are comparing RequiredVariableNames[index] as strings to essentially figure out what index was in the first place? If you insist on this direction why not just use switch(index)?

What is the general problem you are trying to solve here?

Cheers,

Greg
0
 
LVL 6

Author Comment

by:RishadanPort
ID: 22716800
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

0
 
LVL 6

Author Comment

by:RishadanPort
ID: 22716817
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
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 6

Author Comment

by:RishadanPort
ID: 22716827
If you have a suggestion on improving this, I will be happy to hear it.
0
 
LVL 6

Author Comment

by:RishadanPort
ID: 22716845
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

0
 
LVL 29

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 75 total points
ID: 22716848
One possible solution is in the snippet. May be something better will come up to mind...
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(RequiredVariableNames[index]){
                 case c1:  // <--- compile error here since it is not constant
                     break;
         }
      }
   }
}

Open in new window

0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22716856
Sounds like you need an enumeration instead
0
 
LVL 6

Author Comment

by:RishadanPort
ID: 22716871
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
0
 
LVL 6

Author Comment

by:RishadanPort
ID: 22716880
oh wait that worked...
0
 
LVL 6

Author Comment

by:RishadanPort
ID: 22716892
Yes that worked. thanks. What was your suggestion jaime_olivares before I give points out?
0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 22716895
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

0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 100 total points
ID: 22716943
an enumeration can provide a true read-only array of values with a name, so you can use as an array of strings, something like:

enum RVN{ IRCurve, MeanReversionCurve, CapletVolCurve, SwaptionVolCurve };

public void LoadIRCurves(MapLayout curveNames)
{
      foreach (RVN name in Enum.GetValues(typeof(RVN))
     {
                //Variable was not found, add default variable into map
                if (curveNames.getVariable(name.ToString()) == null)
                {
                    switch (name)
                    {
                        case RVN.IRCurve:
                            //Do something
                            break;
                        case RVN.MeanReversionCurve:
                            break;
                        case RVN.CapletVolCurve:
                            break;
                        case RVN.SwaptionVolCurve:
                            break;
                    }
                }
            }
        }
0
 
LVL 6

Author Comment

by:RishadanPort
ID: 22716953
ack... I wish I could give you some points too...
0
 
LVL 6

Author Comment

by:RishadanPort
ID: 22716968
Is there any way to do that in a local scope though? I don't want to define that array outside the function.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22716998
no, you can't, but it is not an array, and enumeration declaration doesn't generate class members as an array.
0
 
LVL 6

Author Comment

by:RishadanPort
ID: 22717007
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!
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22717010
glad to help :)
0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 22717093
I don't mind splitting my points in any way.
So feel free to do that!
0
 
LVL 6

Author Closing Comment

by:RishadanPort
ID: 31506116
Thanks everyone
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question