Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Defining a constant array of constant strings inside a local scope

Posted on 2008-10-14
20
Medium Priority
?
644 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
19 Comments
 
LVL 37

Assisted Solution

by:gregoryyoung
gregoryyoung earned 300 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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
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 30

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 300 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 30

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 400 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 30

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

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

783 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