Solved

Defining a constant array of constant strings inside a local scope

Posted on 2008-10-14
20
634 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
 
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

705 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now