Solved

Defining a constant array of constant strings inside a local scope

Posted on 2008-10-14
20
639 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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 …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

635 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