I'm connecting to an external program to register for some updates to some values. The external program periodically returns updates to a method in my class. I submit a reference id when registering for all the different updates, and the external program returns the reference id I submitted and the relevant update. My problem is this. . .
When I get the update (with the reference id as a string also returned to me), if I search a hastable that has the reference id as a KEY, can I directly set the relevant variable by putting it in as the VALUE part of the Hashtable? Which other way could I do this, because if I have to hard code multiple Switch statements to implement it, I'll have lots of duplicate coding etc.
Here are some code snippets of what I'd like to implement:
Int Price = 0;
. . . .. . . . . . .. .
Hashtable TopicsForRegistering = new Hashtable();
TopicsForRegistering["PRICE"] = Product.Price;
. . . . . ... . . . .
[I have to implement this interface here. . ]
ExtProg.RegisterForUpdates("PRICE", , ,, , ,,, ,)
. . . . . . .. . . . .
[I have to implement this interface here also. .I collect an array with two columns and one row]
System.Array Updates = ExtProg.CollectUpdates();
int ReferenceID = Convert.ToInt32(Updates.GetValue(0,0);
int UpdateValue= Convert.ToInt32(Updates.GetValue(1,0);
TopicsForRegistering [ReferenceID] = UpdateValue;
[At this point, I'd like the value of the original Product.Price to have changed, not just the value in the Hashtable. . . . ]
I'd appreciate any help at all on this.
Best regards,
Tom.
Loading
AlanPosted Feb 24, 2008, 7:07 PM
Yes, if you make Price a class (not a struct) with a Value property, then setting Product.Price.Value will also set the Value property of the Price object stored in the Hashtable because they'll be the same object.
If you're using .NET 2.0 or later, I'd also consider using a generic Dictionary rather than a Hashtable to avoid conversions between System.Object and the actual type of the key or value:
http://msdn2.microsoft.com/en-us/library/xfhwa508.aspx
tomasPosted Feb 24, 2008, 4:24 PM
In that case, would it be more efficient to create Price objects, with accesors for value, and then set the variables by doing Price.value? This is an app that will be dealing with high-frequency real-time data, so a lot of variables are going to be set very often and in quick succession. . .
I'd appreciate any help I could get on this.
Best regards,
Tom.
AlanPosted Feb 23, 2008, 8:24 AM
I don't think that what you are hoping to do is possible because Product.Price is an int which is a value type rather than a reference type.
Consequently, when you place such a value in the Hashtable a copy will be made of the value rather than a reference to the original object. So updating the value in the Hashtable won't affect the value in Product.Price which will need to be updated separately.