Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Defining a constant array of constant strings inside a local scope

Posted on 2008-10-14
20
Medium Priority
?
642 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 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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:…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

722 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