• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 235
  • Last Modified:

C# Array Problem

I am a PHP developer making the transition to C# and having a terrible time understanding how to use array and array like data structures in a way that works.

In PHP, a very common structure I have is this:

$aPatients['555362564-001'] = array("lastname" => "DAVIS", "firstname" => "KEITH", "gender" => "MALE");
$aPatients['425544789-001'] = array("lastname" => "JOHNSON", "firstname" => "MARY", "gender" => "FEMALE");

How do I build this same thing in C#? I need an array of arrays that has string elements and the primary array has a string index. Also, the secondary array has to be accessible AND EDITABLE by a string index.

Help!
0
laurin1
Asked:
laurin1
  • 6
  • 4
  • 3
1 Solution
 
NeoAshuraCommented:
hello and welcome to C#  try the following..code

this is also a very good link



string[] lastname = new string[3] {"Jones", "Smith", "Peterson"};
string[] firstname = new string[3] {"Matt", "Joanne", "Robert"};
string[] gender = new string[2] {"Male", "Female"};

Open in new window

0
 
NeoAshuraCommented:
if u wnat to do it all in one go like u have done then do the following

string[] person1 = new string[3] {"Matt", "Jones", "Male"};

etc for the rest.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
lazyberezovskyCommented:
For accessing values by key you can use
Hashtable - not strongly typed, but fast
Dictionary<TKey, TValue> - strongly typed analog of Hashtable
SortedList<TKey, TValue> - like dictionary, but its enties sorted by keys.

So, If I understand you right,
primary should be Dictionary<string, SortedList<string, string>>
and secondary is SortedList<string, string> (if you really need accessing by index).

But anyway, it's much better to use custom object for this purpose e.g.

public class Patient
{
private readonly string _lastName;
private readonly string _firstName;
private readonly Gender _gender;

public Patient(string lastName, string firstName, Gender gender)
{
 _firstName = firstName;
_lastName = lastName;
_gender = gender;
}

pubic string FirstName
{ get { return _firstName; }}

// etc, and override GetHashCode, Equals methods for using in hashtables
// specify enum for Gender
}


and then use Dictonary<string, Patient> (or create custom object for this issue)

patients.Add(new Patient("DAVIS", "KEITH", Gender.Male));
0
 
lazyberezovskyCommented:
Ok, enum:

public enum Gender
{
  Male,
  Female
}

And usage after patients was added:
textBox1.Text = patients["555362564-001"].FirstName;

BTW, key of dictionary also should be added to Patient class as field
0
 
laurin1Author Commented:
Isn't this missing the string key? patients.Add(new Patient("DAVIS", "KEITH", Gender.Male));

I've gone through a lot of what you guys are suggesting, and thanks, btw, but I run into dead-ends with all of it. Like the last solution:

patients.Add(new Patient("DAVIS", "KEITH", Gender.Male));

That's fine if I am adding it all at once, but the fact is that the data is much more complex than that and is added in pieces. Data is gathered and built from multiple spots. So, how do I add say the CASE # to the above patient, AFTER it's aready created. How do I edit those values, like if the name changes after it's already loaded?
0
 
lazyberezovskyCommented:
Opps, sorry (written without visual studio)
yes, you need key
patients.Add("555362564-001", new Patient("DAVIS", "KEITH", Gender.Male));
or
patients["555362564-001"] = new Patient("DAVIS", "KEITH", Gender.Male);

to change name:
patients["555362564-001"].FirstName = "Laurin";
or
Patient currentPatient = patients["555362564-001"];
currentPatient.FirstName = "Laurin"

Didn't get your question about CASE
0
 
laurin1Author Commented:
No, I think you answered it. I tried something similar, but thought I was off track because it did not work. Let me give it another shot.

Can you explain the enum to me?
0
 
laurin1Author Commented:
I understanding the gettting, but how does

public string FirstName
{ get { return _firstName; }}

allow for setting the FirstName?
0
 
laurin1Author Commented:
And why are your properties marked readonly?
0
 
lazyberezovskyCommented:
In my first example code was without setters, immutable. I.e. ValueObject pattern
http://c2.com/cgi/wiki?ValueObject

This is a direction for you to move forward.
If you should modify patients, then of course, you need to add setters to class interface and remove readonly key from fields definitions.

:)
0
 
laurin1Author Commented:
Ok, so do you have comments about using auto-implmented properties

string firstname { get; set; }

versus the traditional ones, like you just declared.
0
 
laurin1Author Commented:
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 6
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now