RishadanPort
asked on
Defining a constant array of constant strings inside a local scope
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.Leng th; index++){
if(str.Contains(RequiredVa riableName s[index])) {
switch(RequiredVariableNam es[index]) {
case RequiredVariableNames[0]: <--- compile error here since it is not constant
}
}
}
}
Any advice
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.Leng
if(str.Contains(RequiredVa
switch(RequiredVariableNam
case RequiredVariableNames[0]: <--- compile error here since it is not constant
}
}
}
}
Any advice
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
ASKER
If you have a suggestion on improving this, I will be happy to hear it.
ASKER
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;
}
}
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sounds like you need an enumeration instead
ASKER
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
ASKER
oh wait that worked...
ASKER
Yes that worked. thanks. What was your suggestion jaime_olivares before I give points out?
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;
}
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ack... I wish I could give you some points too...
ASKER
Is there any way to do that in a local scope though? I don't want to define that array outside the function.
no, you can't, but it is not an array, and enumeration declaration doesn't generate class members as an array.
ASKER
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!
Thanks a lot!
glad to help :)
I don't mind splitting my points in any way.
So feel free to do that!
So feel free to do that!
ASKER
Thanks everyone
ASKER
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.
Open in new window