473,322 Members | 1,352 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,322 software developers and data experts.

Dis-arrayed arrays

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, HashtablefooBar = 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.
Jan 16 '08 #1
1 1340
On Tue, 15 Jan 2008 16:32:22 -0800, Mini Mouse <ms*****@kittymail.com
wrote:
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, MyDatamyArray = 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
Jan 16 '08 #2

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

Similar topics

0
by: Y2KYZFR1 | last post by:
Granted this only stopps the "forgetfull". I tested it against the following clients, all of which supply a DIFFERENT "no message" string. WinCVS 1.3.14.1 Beta 14 ( Build 1 ) TortioseCVS...
36
by: Song Yun Zhao | last post by:
Hi, Just wondering what are the dis/advantages of using uint vs int. When would be the best time to use it? Personally I don't use uint that much, but I like to optimize my code and make it...
2
by: Rich | last post by:
Hello, I'm "trying" to write a program that creates a multi-dimensional array. I need to populate the array with values entered by the user. I thought I could do something like this; ...
0
by: zhu_dave | last post by:
Hello, I have a couple of questions on the dis module. Let's say that I have the following disassembled code (It's a simple program which checks to see whether key 'e' is in myDictionary): 0...
5
by: Ron Adam | last post by:
Can anyone show me an example of of using dis() with a traceback? Examples of using disassemble_string() and distb() separately if possible would be nice also. I'm experimenting with...
4
by: jenipriya | last post by:
how to write a query to calculate the salary deduction for an employee in a given dept for a given month? salary deduction is sum of Clubfees(from club table) + SAL(from employee table/30 * no of...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.