Dis-arrayed arrays

  • Thread starter Thread starter Mini Mouse
  • Start date Start date
M

Mini Mouse

Being new to C# (coming from PHP) I'm at a lose as to how to do this.


have an array of items where the structure is something like this:


myArray[String AccountNumber]
{
String name;
String City;
Decimal balance;
}

I've tried things like:
Using a Hashtable, but only seemed to allow me to store one item in
its value.

Then I moved on to Dictonaries with something like Dictionary<String,
Hashtable>
Dictionary<String, Hashtable> fooBar = new Dictionary<String,
Hashtable>();
Hashtable myHashTable = new HashTable;

accountNumber = "1234";
myHashtable.Add("user","Foo");
myHashtable.Add("Pass","Bar");
myHashtable.Add("Balance",22);

fooBar.Add(accountNumber, myHashTable);
myHashtable.Clear(); //clear it out or Index already exists error will
be thrown

accountNumber="2345";
myHashtable.Add("user","Benny");
myHashtable.Add("Pass","Hill");
myHashtable.Add("Balance",22);

fooBar.Add(accountNumber, myHashTable);

myHashtable.Clear(); //clear out or Index already exists error will be
thrown.

myHashtable = fooBar['1234"]; //This should load the stored hashtable
in to myHashtable

But doing like the above, myHashTable does not contain any values. :(

In PHP it is easy enough:
$orange["123"] = array("Foo","Bar",36);
$orange["456"] = array("Benny", "Hill", 22);
echo $orange["456"][0];


So how would I go about something like that in C#? Collections,
Dictionaries, and HashTables. Oh my!

I've looked through various books on C# but nothing comes close to
describing what I'm trying to do. :(

Thanks.
 
Being new to C# (coming from PHP) I'm at a lose as to how to do this.

have an array of items where the structure is something like this:

myArray[String AccountNumber]
{
String name;
String City;
Decimal balance;
}

Rather than assuming we all know PHP, perhaps it would be better for you
to explain the actual _behavior_ you're looking for.

That said, not knowing PHP myself I will take a wild guess and suppose
that the code you show above declares an array that is indexed by a string
("AccountNumber"), and which can associate three values with a single
"AccountNumber" ("name", "City", and "balance").

In .NET a Hashtable or Dictionary<> would be a fine way to do that. But
you need to also declare a structure to hold the data first:

struct MyData
{
public readonly string name;
public readonly string City;
public readonly decimal balance;

public MyData(string name, string City, decimal balance)
{
this.name = name;
this.City = City;
this.balance = balance;
}
}

Then you can just add new instances of the struct to your dictionary:

Dictionary<string, MyData> myArray = new Dictionary<string, MyData>();

// Add stuff to the collection...
myArray.Add("1234", new MyData("Foo", "Bar", 22));
myArray.Add("2345", new MyData("Benny", "Hill", 22));
// etc.

// Print the "name" field of one of the elements in the collection....
Console.WriteLine(myArray["2345"].name);

By the way, the code you tried would have actually worked, had you instead
of clearing the hashtable just initialize the variable with a new one.
Every time you called "myHashtable.Clear()", you removed everything from
the Hashtable, and you only ever stored the one Hashtable instance in the
Dictionary<> (in other words, all of your dictionary keys referenced the
same Hashtable and you always emptied that one Hashtable after you addeda
reference to it to your dictionary). It's not really the right way to
manage the data structure anyway, but it would have worked had you not
made that mistake.

The fact that you were having trouble suggests that you're not quite used
to the idea of reference types. To use C# effectively, you really need to
understand that concept well.

Pete
 
Being new to C# (coming from PHP) I'm at a lose as to how to do this.
have an array of items where the structure is something like this:
myArray[String AccountNumber]
{
String name;
String City;
Decimal balance;
}

Rather than assuming we all know PHP, perhaps it would be better for you
to explain the actual _behavior_ you're looking for.

That said, not knowing PHP myself I will take a wild guess and suppose
that the code you show above declares an array that is indexed by a string
("AccountNumber"), and which can associate three values with a single
"AccountNumber" ("name", "City", and "balance").

In .NET a Hashtable or Dictionary<> would be a fine way to do that. But
you need to also declare a structure to hold the data first:

struct MyData
{
public readonly string name;
public readonly string City;
public readonly decimal balance;

public MyData(string name, string City, decimal balance)
{
this.name = name;
this.City = City;
this.balance = balance;
}
}

Then you can just add new instances of the struct to your dictionary:

Dictionary<string, MyData> myArray = new Dictionary<string, MyData>();

// Add stuff to the collection...
myArray.Add("1234", new MyData("Foo", "Bar", 22));
myArray.Add("2345", new MyData("Benny", "Hill", 22));
// etc.

// Print the "name" field of one of the elements in the collection...
Console.WriteLine(myArray["2345"].name);

By the way, the code you tried would have actually worked, had you instead
of clearing the hashtable just initialize the variable with a new one.
Every time you called "myHashtable.Clear()", you removed everything from
the Hashtable, and you only ever stored the one Hashtable instance in the
Dictionary<> (in other words, all of your dictionary keys referenced the
same Hashtable and you always emptied that one Hashtable after you added a
reference to it to your dictionary). It's not really the right way to
manage the data structure anyway, but it would have worked had you not
made that mistake.

The fact that you were having trouble suggests that you're not quite used
to the idea of reference types. To use C# effectively, you really need to
understand that concept well.

Pete

I do understand the idea of reference types (well more than one might
think, but less than one might know). When I was sending it in to the
array I was like, oops that's going to bugger things up because only
that copy is going in and any subsequent changes via the method I was
going to use would muck things up. Told you I was new to C#. No
matter.

Before I came back to your reply I got on to the idea of creating my
own structure, then some how getting that in to the Dictionary/
Hashtable. The part that was eluding me was using the new option
within the call to stuff the data in to the dictionary (It's been a
while since I futzed with any M$ development so some of that slipped)
and with a nudge from you I was able to fit the pieces nicely together
now. I'm able to do what I need now.

After running along the same lines as your suggestion I had to do some
other things so that I could make changes to the information stuffed
in to the dictionary. Also, in an effort to be able to update items in
the struct I had to add some set;get in there as well. All an all an
interesting learning exercise.

Thanks for the nudge in the right direction.
 
Back
Top