Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 20th, 2008, 07:38 AM
Registered User
 
Join Date: Feb 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default Setting variable having found it in a HashTable se

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.


 
Old February 20th, 2008, 11:54 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

A Hashtable is a key/value pair. It has no concept of indexes (i.e. you cannot get the 'first' key-value pair by doing hashtable[0]).

However the key can be any object type, including an integer. So if you do

hashtable.Add(1, "Fred");
hashtable.Add(3, "Bert");

then the following will work as there is a key with a value of 1:

hashtable[1]

but the following wont as there is no key with a value of 2:

hashtable[2];

If the reference ID you are receiving is an index into an array then you can't do this (there is no guarantee to the ordering of a hashtable). Why is the referenceID not "PRICE"?

/- Sam Judson : Wrox Technical Editor -/
 
Old February 20th, 2008, 12:36 PM
Registered User
 
Join Date: Feb 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the reply.

I'm implementing a set interface, so the param that is a referenceID must be an int. . .

I see what you're getting at, but my problem is probably a bit more in terms of design. The update array can contain any of a number of referenceIDs - each of which refer to a different member variable of the class ie: Price, Name, QUantity etc.

I plan to put objects of type (int) into the hastable - but when I search it, and find this.Price, for example, how do I set the value of that variable equal to the value I've just got out of the update array? Without doing a switch and saying if referenceID = 1 Price = UpdateValue, if referenceID = 2, Quantity = TopicValue etc. How can I update the value of the variable without going through all this, because I will be registering for a different subset of referenceIDs each time the class is built/called. . . .

 
Old February 20th, 2008, 12:53 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You could keep the variables themselves in an array, and provide properties that pull the values out of the array for external classes. Alternatively you might be stuck with a switch statement.

/- Sam Judson : Wrox Technical Editor -/
 
Old February 21st, 2008, 05:47 AM
Registered User
 
Join Date: Feb 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks. But I'd like to handle the referenceID I receive from the external program, search the hashtable for the appropriate reference to a variable in my class and set that variable. The only thing I've succeeded in doing so far is (a) putting the VALUE of the variable I ultimately want to set into the Hashtable, (b) receiving the reference and update from the external app, (c) searching the hashtable with the reference, (d) setting the value of the hashtable entry relating to the original variable to the value of the update. Basically, I've only succeeded in updating the hashtable, not the variable I actually want to set. . .. .

I'd appreciate any help I could get with this,

Tom.

 
Old February 21st, 2008, 06:50 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

I can't think of any other way apart from a switch statement.

/- Sam Judson : Wrox Technical Editor -/





Similar Threads
Thread Thread Starter Forum Replies Last Post
conditionally setting variable ewel Beginning PHP 2 December 13th, 2007 07:39 PM
Newbie : How do you .. without setting a variable rqaran XSLT 2 August 6th, 2005 04:09 AM
declare a variable without setting a value to it crmpicco Javascript How-To 1 July 18th, 2005 07:25 PM
setting each value from database as a variable Ashleek007 Beginning PHP 21 April 18th, 2005 03:50 PM
ADO Update "Variable not found " dfaulks Classic ASP Databases 4 December 1st, 2003 12:51 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.