How do I get the property name of an object array to use in a SWITCH / CASE function as follows? Any other suggestions on how to do this are appreciated.
Object[] objEURUSD = ConvertStringtoObjectArray(string val1);
Object[] objAUDUSD = ConvertStringtoObjectArray(string val2);
Object[] objGBPUSD = ConvertStringtoObjectArray(string val3);
Example Function Use
Object[] mvavgEURUSD = mvavgObject(objEURUSD);
private Object[] mvavgObject(Object[] val)
{
string sym = val.ToString() // this does not return the name it returns 'System.Object'
switch (sym)
{
case "objEURUSD":
// do something
break;
case "objAUDUSD":
// do something
break;
case "objGBPUSD":
// do something
break;
}
}
I could include the name of the object 'EURUSD' in the Object Array itself, but it's already inlcuded in the name of the object.. I just can't figure out how to reference the name of the object.. either I don't know or开发者_运维百科 I am not conversant with Reflection.
I appreciate your help or suggestions on the matter.
How do I get the property name of an object array to use in a SWITCH / CASE function ?
By not doing the conversion. You can use a string
in a switch statement, but not an object[]
.
You can't because there is no such "name" for an object. That wouldn't even make sense - many variables can hold a reference to the same object. Which one should be the "name"?
Imagine for a moment that there would be such a function called GetName(). What would the program output in each of these cases?
Case 1:
var a = new MyObject();
var b = a;
Console.WriteLine(GetName(b)); // Is it "a" or "b"?
Case 2:
Console.WriteLine(GetName(new MyObject())); // What is the name now?
Case 3:
Console.WriteLine(GetName(null)); // I guess you can return null here, but see case 4:
Case 4:
MyObject a = null;
Console.WriteLine(GetName(a)); // Should this be "a" or null?
And so on and so forth.
If you want to give your objects "names", you'll have to do so yourself. Perhaps create a "NamedObject" wrapper class like this:
class NamedObject<T>
{
public readonly T Object;
public readonly string Name;
public NamedObject(string name, string value)
{
this.Object = value;
this.Name = name;
}
}
Without further information, I can only guess what ConvertStringtoObjectArray
does. My guess is that it looks something up using the string as a key, and then returns an array of some values.
In most cases, I would expect that the original string is no longer contained in the object array. Therefore, obviously, you cannot retrieve it, so you will have to “remember” it, i.e. pass it around.
For example, instead of
Object[] objEURUSD = ConvertStringtoObjectArray(myVal);
Object[] mvavgEURUSD = mvavgObject(objEURUSD);
private Object[] mvavgObject(Object[] val)
{
// ...
you could write something like this:
Object[] mvavgEURUSD = mvavgObject(myVal);
private Object[] mvavgObject(string val)
{
Object[] objEURUSD = ConvertStringtoObjectArray(val);
// The string is in the parameter ‘val’, so we can use it here
switch (val)
{
// ...
}
}
the default behavior of ToString() is to return the class name. This can be overridden in any class as all classes inherit from object. string.ToString() is the behavior you are looking for so you probably need to first cast the object back to string (the array part is tricky though since I don't know what the ConvertStringtoObjectArray method does), but you need to do a cast to access what you are looking for.
I assume that the object[] is a char[]? if so the cast should be as easy as casting the object to a char[] and then to the string.
if the variable declaration of "objEURUSD" is in your current scope, you could compare your parameter to that variable to see if both are referencing the same object ... this won't lookup some sort of name, but maybe it's what you want...
Object[] objEURUSD; //initialized somewhere else
private Object[] mvavgObject(Object[] val)
{
if(val==objEURUSD)
{
//it's the array referenced by objEURUSD ...
}
}
as an alternative, you could use a Dictionary<string,Object[]>
or Dictionary<Object[],string>
to store your arrays, so you can lookup the array for a given name or vice versa
精彩评论