Looking for small example on collections

  • Thread starter Thread starter Edwin Knoppert
  • Start date Start date
E

Edwin Knoppert

I often need a collection having a keyname and string data

I currently use a struct like:

struct ItemsInfo
{
public String CC;
public String C;
};
ItemsInfo[] Items;

Of course this **** :)

I want to access an item by keyname as index.
Like Items["hello"] as Items[100]
Thanks,
 
Edwin said:
I often need a collection having a keyname and string data

I currently use a struct like:

struct ItemsInfo
{
public String CC;
public String C;
};
ItemsInfo[] Items;

Of course this **** :)

I want to access an item by keyname as index.
Like Items["hello"] as Items[100]
Thanks,
Structs are alright except that they are value types while collections
like HashTable and Arraylist are reference types.

Cheers
Ray
 
In System.Collection you will find Hashtable class which you can use to
store key/pair values.

E.g.

Hashtable ht = new Hashtable();
ht.Add("test",new object());

object obj = ht["test"]

In .NET 2 in the System.Collection.Generic namespace you can also find
the generic implementation of Dictionary, which you can use instead of
a typed dictionary.

Dictionary<string,int> dict = new Dictionary<string,int>();
dict["age1"] = 10;
dict["age2"] = 5;

Best regards,
Tasos
 
Thanks!
Hashtable seems my logical one here :)
(So closeby and i kept messing with structs)
 
You'd be better off with a Generic Dictionary Collection, such as:

System.Collections.Generic.Dictionary<TKey, TValue>

The HashTable uses an Object Key and an Object value, which is late bound,
while the Generic Dictionary Collection has a strongly-typed Key and Value.
Available on the .Net 2.0 Platform.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

The man who questions opinions is wise.
The man who quarrels with facts is a fool.

Edwin Knoppert said:
Thanks!
Hashtable seems my logical one here :)
(So closeby and i kept messing with structs)



Tasos Vogiatzoglou said:
In System.Collection you will find Hashtable class which you can use to
store key/pair values.

E.g.

Hashtable ht = new Hashtable();
ht.Add("test",new object());

object obj = ht["test"]

In .NET 2 in the System.Collection.Generic namespace you can also find
the generic implementation of Dictionary, which you can use instead of
a typed dictionary.

Dictionary<string,int> dict = new Dictionary<string,int>();
dict["age1"] = 10;
dict["age2"] = 5;

Best regards,
Tasos
 
Correction: I should have said either late-bound or cast.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

The man who questions opinions is wise.
The man who quarrels with facts is a fool.

Edwin Knoppert said:
Thanks!
Hashtable seems my logical one here :)
(So closeby and i kept messing with structs)



Tasos Vogiatzoglou said:
In System.Collection you will find Hashtable class which you can use to
store key/pair values.

E.g.

Hashtable ht = new Hashtable();
ht.Add("test",new object());

object obj = ht["test"]

In .NET 2 in the System.Collection.Generic namespace you can also find
the generic implementation of Dictionary, which you can use instead of
a typed dictionary.

Dictionary<string,int> dict = new Dictionary<string,int>();
dict["age1"] = 10;
dict["age2"] = 5;

Best regards,
Tasos
 
I'll check it out, thanks!

Kevin Spencer said:
You'd be better off with a Generic Dictionary Collection, such as:

System.Collections.Generic.Dictionary<TKey, TValue>

The HashTable uses an Object Key and an Object value, which is late bound,
while the Generic Dictionary Collection has a strongly-typed Key and
Value. Available on the .Net 2.0 Platform.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

The man who questions opinions is wise.
The man who quarrels with facts is a fool.

Edwin Knoppert said:
Thanks!
Hashtable seems my logical one here :)
(So closeby and i kept messing with structs)



Tasos Vogiatzoglou said:
In System.Collection you will find Hashtable class which you can use to
store key/pair values.

E.g.

Hashtable ht = new Hashtable();
ht.Add("test",new object());

object obj = ht["test"]

In .NET 2 in the System.Collection.Generic namespace you can also find
the generic implementation of Dictionary, which you can use instead of
a typed dictionary.

Dictionary<string,int> dict = new Dictionary<string,int>();
dict["age1"] = 10;
dict["age2"] = 5;

Best regards,
Tasos
 
Thanks, it works :)


Edwin Knoppert said:
I'll check it out, thanks!

Kevin Spencer said:
You'd be better off with a Generic Dictionary Collection, such as:

System.Collections.Generic.Dictionary<TKey, TValue>

The HashTable uses an Object Key and an Object value, which is late
bound, while the Generic Dictionary Collection has a strongly-typed Key
and Value. Available on the .Net 2.0 Platform.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

The man who questions opinions is wise.
The man who quarrels with facts is a fool.

Edwin Knoppert said:
Thanks!
Hashtable seems my logical one here :)
(So closeby and i kept messing with structs)



"Tasos Vogiatzoglou" <[email protected]> schreef in bericht
In System.Collection you will find Hashtable class which you can use to
store key/pair values.

E.g.

Hashtable ht = new Hashtable();
ht.Add("test",new object());

object obj = ht["test"]

In .NET 2 in the System.Collection.Generic namespace you can also find
the generic implementation of Dictionary, which you can use instead of
a typed dictionary.

Dictionary<string,int> dict = new Dictionary<string,int>();
dict["age1"] = 10;
dict["age2"] = 5;

Best regards,
Tasos
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top