473,405 Members | 2,444 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Q: syntax for Hashtable within a Hashtable?

I bet I know the answer already.

I have a hashtable (hMaster) that holds several hashtables ("hTables") each
of which holds other hashtables ("hColumns"). Presently, I am getting at
the info I want thusly (this compiles, at least):

Hashtable hTable = (Hashtable)hMaster[tableName];
Hashtable hColumn = (Hashtable)hTable[columnName];
return hColumn.ContainsKey(codeValue);

This is kind of cumbersome. I'd rather use something like this:

return hMaster[tableName].[columnName].ContainsKey(codeValue);

But this syntax is not supported. I'm not surprised by this, I was asking a
lot, but maybe there is a cleaner approach than what I'm actually using with
all the steps and casts?

Matt

(P.S. I realize I could have leveraged ADO.NET dataset stuff to check my
code values. But I'm not up to speed on all the fancy DataSet/DataTable
stuff, so I stuck with what made sense to me. Also I had the idea that keys
from Hashtables would be faster, there's going to be a lot of these
validations.)
Nov 16 '05 #1
4 10368
Why not:

<snip>
return
((Hashtable)((Hashtable)hMaster[tableName]).[columnName]).ContainsKey(codeVa
lue);
</snip>

Still a bit cumbersome so it might not be what you were looking for.

Another solution would be to create a object that inherits from Hashtable
and override the indexer so that it can only contain Hashtable objects and
therefore will be strongly typed and won't require the type casting.

Patrick Altman

"Matt C." <ca*****@my-deja.com> wrote in message
news:Xn******************************@207.46.248.1 6...
I bet I know the answer already.

I have a hashtable (hMaster) that holds several hashtables ("hTables") each of which holds other hashtables ("hColumns"). Presently, I am getting at
the info I want thusly (this compiles, at least):

Hashtable hTable = (Hashtable)hMaster[tableName];
Hashtable hColumn = (Hashtable)hTable[columnName];
return hColumn.ContainsKey(codeValue);

This is kind of cumbersome. I'd rather use something like this:

return hMaster[tableName].[columnName].ContainsKey(codeValue);

But this syntax is not supported. I'm not surprised by this, I was asking a lot, but maybe there is a cleaner approach than what I'm actually using with all the steps and casts?

Matt

(P.S. I realize I could have leveraged ADO.NET dataset stuff to check my
code values. But I'm not up to speed on all the fancy DataSet/DataTable
stuff, so I stuck with what made sense to me. Also I had the idea that keys from Hashtables would be faster, there's going to be a lot of these
validations.)

Nov 16 '05 #2
Matt,

As suggested, you will have to use a cast. However, in .NET 2.0, with
Generics, you will be able to use the Generic Dictionary class, which will
be strongly typed, therefore eliminating the need for the cast.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Matt C." <ca*****@my-deja.com> wrote in message
news:Xn******************************@207.46.248.1 6...
I bet I know the answer already.

I have a hashtable (hMaster) that holds several hashtables ("hTables")
each
of which holds other hashtables ("hColumns"). Presently, I am getting at
the info I want thusly (this compiles, at least):

Hashtable hTable = (Hashtable)hMaster[tableName];
Hashtable hColumn = (Hashtable)hTable[columnName];
return hColumn.ContainsKey(codeValue);

This is kind of cumbersome. I'd rather use something like this:

return hMaster[tableName].[columnName].ContainsKey(codeValue);

But this syntax is not supported. I'm not surprised by this, I was asking
a
lot, but maybe there is a cleaner approach than what I'm actually using
with
all the steps and casts?

Matt

(P.S. I realize I could have leveraged ADO.NET dataset stuff to check my
code values. But I'm not up to speed on all the fancy DataSet/DataTable
stuff, so I stuck with what made sense to me. Also I had the idea that
keys
from Hashtables would be faster, there's going to be a lot of these
validations.)

Nov 16 '05 #3
Matt C. <ca*****@my-deja.com> wrote:
I bet I know the answer already.

I have a hashtable (hMaster) that holds several hashtables ("hTables") each
of which holds other hashtables ("hColumns"). Presently, I am getting at
the info I want thusly (this compiles, at least):

Hashtable hTable = (Hashtable)hMaster[tableName];
Hashtable hColumn = (Hashtable)hTable[columnName];
return hColumn.ContainsKey(codeValue);

This is kind of cumbersome. I'd rather use something like this:

return hMaster[tableName].[columnName].ContainsKey(codeValue);

But this syntax is not supported. I'm not surprised by this, I was asking a
lot, but maybe there is a cleaner approach than what I'm actually using with
all the steps and casts?


Why are you surprised by this? hMaster[tableName] is of type Object,
not Hashtable. There's nothing to stop you putting something completely
different in hMaster.

Generics in 2.0 will help you here, as you'll be able to tell the
compiler that hTable is a Dictionary<string,Dictionary<string,object>>
or whatever.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
"Patrick Altman" <paltmanATgmailDOTcom> wrote in
news:#X**************@TK2MSFTNGP11.phx.gbl:
Why not:

<snip>
return
((Hashtable)((Hashtable)hMaster[tableName]).[columnName]).ContainsKey(co
deVa lue);
</snip>

Still a bit cumbersome so it might not be what you were looking for.


Sweet. Thanks.

(You actually have to remove the dot between tableName and columnName, for
anyone else making use of this. My fault, I started us down that road.)

That is unlovely to read, but it does get rid of the temp variables which is
what I was really after.

I'm assuming the inline casting is actually going to be a little faster than
creating the temp Hashtable and assigning the internal Hashtables one step
at a time. But I'm not sure of that; maybe they amount to the same thing
once compiled. Does anyone know offhand if the nested parenthetical casting
above is faster than using temp variables in separate steps?

Matt

P.S. Thanks to those who pointed out generics in .NET 2.0. I'm looking
forward to those, this is only about the 100th time they would have come in
handy.
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: JezB | last post by:
How can I loop thru a hashtable changing the Value of each entry ? Whatever I try I always seem to get the error about modifying the collection within the loop.
33
by: Ken | last post by:
I have a C# Program where multiple threads will operate on a same Hashtable. This Hashtable is synchronized by using Hashtable.Synchronized(myHashtable) method, so no further Lock statements are...
8
by: Robin Tucker | last post by:
When I create a hashtable hashing on Object-->Item, can I mix "string" and "integer" as the key types? I have a single thumbnail cache for a database with (hashed on key) and a file view (hashed...
5
by: Dick | last post by:
Hello, I'm trying to serialize a class with a Hashtable within: ' Class code: Imports System.Collections Class clsOptions Public countID As Integer Public persons As New Hashtable End Class
2
by: bandroo | last post by:
Hi Guys How can I modify the items within a hashtable "in situ" so to speak? At the moment, I am locating the item that I want, extracting it, modifying the item, deleting the hashtable item,...
9
by: raylopez99 | last post by:
Hello all— I’m trying to get the below to work and cannot get the format right. It’s from this example: http://msdn.microsoft.com/en-us/library/8627sbea(VS.71).aspx What it is: I’m trying...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.