Multidimensional Arrays??

  • Thread starter Thread starter Joshua Russell
  • Start date Start date
J

Joshua Russell

Hi,
I wish to parse an XML file into some sort or array or multidimensional storage. The XML file is as follows (without any parent tags just to give you an idea of the structure)...
<Ship Name="Disrupter" Class="CC" PrimaryTarget="CR" BattleOrder="1" Guns="5" Power="2"></Ship>
<Ship Name="Zealot" Class="CR" PrimaryTarget="CC" BattleOrder="2" Guns="25" Power="5"></Ship>
<Ship Name="Drone" Class="CC" PrimaryTarget="CC" BattleOrder="3" Guns="4" Power="2"></Ship>
<Ship Name="Invader" Class="CC" PrimaryTarget="CO" BattleOrder="4" Guns="3" Power="4"></Ship>
<Ship Name="Reformer" Class="CO" PrimaryTarget="FR" BattleOrder="5" Guns="5" Power="6"></Ship>

In PHP (my primary programming langugage) I would be able to create a multidimensional array that that woudl look something like this...

[0] ->
[Name] = "Disrupter"
[Class] = "CC"
[PrimaryTarget] = "CR"
[BattleOrder] = "1"
[Guns] = "5"
[Power] = "2"

[1] ->
[Name] = "Zealot"
[Class] = "CR"
[PrimaryTarget] = "CC"
[BattleOrder] = "2"
[Guns] = "25"
[Power] = "5"

etc...

I understand that you can't have text identifiers for array values in C# like you can in PHP. Can anyone suggest anything?

Thanx Josh
 
Joshua said:
Hi,
I wish to parse an XML file into some sort or array or multidimensional
storage. The XML file is as follows (without any parent tags just to
give you an idea of the structure)...

<Ship Name="Disrupter" Class="CC" PrimaryTarget="CR" BattleOrder="1"
Guns="5" Power="2"></Ship>
<Ship Name="Zealot" Class="CR" PrimaryTarget="CC" BattleOrder="2"
Guns="25" Power="5"></Ship>
<Ship Name="Drone" Class="CC" PrimaryTarget="CC" BattleOrder="3"
Guns="4" Power="2"></Ship>
<Ship Name="Invader" Class="CC" PrimaryTarget="CO" BattleOrder="4"
Guns="3" Power="4"></Ship>
<Ship Name="Reformer" Class="CO" PrimaryTarget="FR" BattleOrder="5"
Guns="5" Power="6"></Ship>

In PHP (my primary programming langugage) I would be able to create a
multidimensional array that that woudl look something like this...

*[0]* ->
* **[Name]* = "Disrupter"
* **[Class]* = "CC"
* **[PrimaryTarget]* = "CR"
*[BattleOrder] *= "1"
* **[Guns]* = "5"
* **[Power]* = "2"

*[1]* ->
* **[Name]* = "Zealot"
* **[Class]* = "CR"
* **[PrimaryTarget]* = "CC"
*[BattleOrder]* = "2"
*[Guns]* = "25"
*[Power]* = "5"

etc...

I understand that you can't have text identifiers for array values in C#
like you can in PHP. Can anyone suggest anything?

Thanx Josh
Use a System.Collections.Hashtable.

Hashtable ships = new Hashtable();
ships["Name"] = "Zealot";
// etc.
 
Hashtable....

Thx

Tyler Dixon said:
Joshua said:
Hi,
I wish to parse an XML file into some sort or array or multidimensional
storage. The XML file is as follows (without any parent tags just to
give you an idea of the structure)...

<Ship Name="Disrupter" Class="CC" PrimaryTarget="CR" BattleOrder="1"
Guns="5" Power="2"></Ship>
<Ship Name="Zealot" Class="CR" PrimaryTarget="CC" BattleOrder="2"
Guns="25" Power="5"></Ship>
<Ship Name="Drone" Class="CC" PrimaryTarget="CC" BattleOrder="3"
Guns="4" Power="2"></Ship>
<Ship Name="Invader" Class="CC" PrimaryTarget="CO" BattleOrder="4"
Guns="3" Power="4"></Ship>
<Ship Name="Reformer" Class="CO" PrimaryTarget="FR" BattleOrder="5"
Guns="5" Power="6"></Ship>

In PHP (my primary programming langugage) I would be able to create a
multidimensional array that that woudl look something like this...

*[0]* ->
* **[Name]* = "Disrupter"
* **[Class]* = "CC"
* **[PrimaryTarget]* = "CR"
*[BattleOrder] *= "1"
* **[Guns]* = "5"
* **[Power]* = "2"

*[1]* ->
* **[Name]* = "Zealot"
* **[Class]* = "CR"
* **[PrimaryTarget]* = "CC"
*[BattleOrder]* = "2"
*[Guns]* = "25"
*[Power]* = "5"

etc...

I understand that you can't have text identifiers for array values in C#
like you can in PHP. Can anyone suggest anything?

Thanx Josh
Use a System.Collections.Hashtable.

Hashtable ships = new Hashtable();
ships["Name"] = "Zealot";
// etc.
 
Use a System.Collections.Hashtable.

Hashtable ships = new Hashtable();
ships["Name"] = "Zealot";
// etc.

While that will do what he was trying to do (or at least, an Array of
HashTables would do it), I can hardly condone that sort of data structure.


It seems as if each item in the array has a certain number of properties.
Why not make a Ship class with a Name, ShipClass, PrimaryTarget,
BattleOrder, Guns, and Power properties?

Then you could make a ShipCollection class, if you wanted. Or an Ship[]
array may suffice.

Is there any real reason to want these in a "multi-dimensional array," or is
it just because that's the way you're used to doing it?

Having to look up each of these values in a hash-table would hurt
performance a bit, and you would lose any help from the IDE (IntelliSense).

If you're interested in making a Ship class, but don't know how, then I'll
be glad to help.


--Matthew W. Jackson
 
Yeah, I'm pretty new to C# so if you could help me make a Ships[] class that
woudl be appreciated.

Thanx Josh


Matthew W. Jackson said:
Use a System.Collections.Hashtable.

Hashtable ships = new Hashtable();
ships["Name"] = "Zealot";
// etc.

While that will do what he was trying to do (or at least, an Array of
HashTables would do it), I can hardly condone that sort of data structure.


It seems as if each item in the array has a certain number of properties.
Why not make a Ship class with a Name, ShipClass, PrimaryTarget,
BattleOrder, Guns, and Power properties?

Then you could make a ShipCollection class, if you wanted. Or an Ship[]
array may suffice.

Is there any real reason to want these in a "multi-dimensional array," or is
it just because that's the way you're used to doing it?

Having to look up each of these values in a hash-table would hurt
performance a bit, and you would lose any help from the IDE (IntelliSense).

If you're interested in making a Ship class, but don't know how, then I'll
be glad to help.


--Matthew W. Jackson
 
Yeah, I'm pretty new to C# so if you could help me make a Ships[] class that
woudl be appreciated.

Thanx Josh


Matthew W. Jackson said:
Use a System.Collections.Hashtable.

Hashtable ships = new Hashtable();
ships["Name"] = "Zealot";
// etc.

While that will do what he was trying to do (or at least, an Array of
HashTables would do it), I can hardly condone that sort of data structure.


It seems as if each item in the array has a certain number of properties.
Why not make a Ship class with a Name, ShipClass, PrimaryTarget,
BattleOrder, Guns, and Power properties?

Then you could make a ShipCollection class, if you wanted. Or an Ship[]
array may suffice.

Is there any real reason to want these in a "multi-dimensional array," or is
it just because that's the way you're used to doing it?

Having to look up each of these values in a hash-table would hurt
performance a bit, and you would lose any help from the IDE (IntelliSense).

If you're interested in making a Ship class, but don't know how, then I'll
be glad to help.


--Matthew W. Jackson
 
One other possibility, depending on how you wish to use this, is to use a DataTable (part of the System.Data namespace in ADO.NET). There may be a way to automatically create a DataTable (i.e., a 2 dimensional array-like thing) out of your existing XML file.

Hi,
I wish to parse an XML file into some sort or array or multidimensional storage. The XML file is as follows (without any parent tags just to give you an idea of the structure)...
<Ship Name="Disrupter" Class="CC" PrimaryTarget="CR" BattleOrder="1" Guns="5" Power="2"></Ship>
<Ship Name="Zealot" Class="CR" PrimaryTarget="CC" BattleOrder="2" Guns="25" Power="5"></Ship>
<Ship Name="Drone" Class="CC" PrimaryTarget="CC" BattleOrder="3" Guns="4" Power="2"></Ship>
<Ship Name="Invader" Class="CC" PrimaryTarget="CO" BattleOrder="4" Guns="3" Power="4"></Ship>
<Ship Name="Reformer" Class="CO" PrimaryTarget="FR" BattleOrder="5" Guns="5" Power="6"></Ship>

In PHP (my primary programming langugage) I would be able to create a multidimensional array that that woudl look something like this...

[0] ->
[Name] = "Disrupter"
[Class] = "CC"
[PrimaryTarget] = "CR"
[BattleOrder] = "1"
[Guns] = "5"
[Power] = "2"

[1] ->
[Name] = "Zealot"
[Class] = "CR"
[PrimaryTarget] = "CC"
[BattleOrder] = "2"
[Guns] = "25"
[Power] = "5"

etc...

I understand that you can't have text identifiers for array values in C# like you can in PHP. Can anyone suggest anything?

Thanx Josh
 
//here ya go
public struct Ship
{
#region Private Fields
private string name;
private Class classtype;
private Primary target;
private int battleorder, guns, power;
#endregion

#region Enums
public enum Class
{
CC,
CR,
CO
//add more if needed
}

public enum Primary
{
CR,
CC,
CO,
FR
}
#endregion

#region Constructor
public Ship(string name, Class ClassType, Primary target, int
BattleOrder, int Guns,int Power)
{
this.name=name;
this.classtype=ClassType;
this.target=target;
this.battleOrder=BattleOrder;
this.guns=Guns;
this.power=Power;
}
#endregion

#region Public Properties
public string Name
{
get
{
return this.name;
}
set
{
if (value!=null)
this.name=value;
}
}
public Class ClassType
{
get
{
return this.classtype;
}
set
{
this.classtype=value;
}
}

public Primary Target
{
get
{
return this.target;
}
set
{
this.target=value;
}
}

public int BattleOrder
{
get
{
return this.battleorder;
}
set
{
this.battleorder=value;
}
}

public int Guns
{
get
{
return this.guns;
}
set
{
this.guns=value;
}
}

public int Power
{
get
{
return this.power;
}
set
{
this.power=value;
}
}
#endregion

}
 
Example??

One other possibility, depending on how you wish to use this, is to use a DataTable (part of the System.Data namespace in ADO.NET). There may be a way to automatically create a DataTable (i.e., a 2 dimensional array-like thing) out of your existing XML file.

Hi,
I wish to parse an XML file into some sort or array or multidimensional storage. The XML file is as follows (without any parent tags just to give you an idea of the structure)...
<Ship Name="Disrupter" Class="CC" PrimaryTarget="CR" BattleOrder="1" Guns="5" Power="2"></Ship>
<Ship Name="Zealot" Class="CR" PrimaryTarget="CC" BattleOrder="2" Guns="25" Power="5"></Ship>
<Ship Name="Drone" Class="CC" PrimaryTarget="CC" BattleOrder="3" Guns="4" Power="2"></Ship>
<Ship Name="Invader" Class="CC" PrimaryTarget="CO" BattleOrder="4" Guns="3" Power="4"></Ship>
<Ship Name="Reformer" Class="CO" PrimaryTarget="FR" BattleOrder="5" Guns="5" Power="6"></Ship>

In PHP (my primary programming langugage) I would be able to create a multidimensional array that that woudl look something like this...

[0] ->
[Name] = "Disrupter"
[Class] = "CC"
[PrimaryTarget] = "CR"
[BattleOrder] = "1"
[Guns] = "5"
[Power] = "2"

[1] ->
[Name] = "Zealot"
[Class] = "CR"
[PrimaryTarget] = "CC"
[BattleOrder] = "2"
[Guns] = "25"
[Power] = "5"

etc...

I understand that you can't have text identifiers for array values in C# like you can in PHP. Can anyone suggest anything?

Thanx Josh
 
My fault. I forgot that you're loading this data from XML, and need a
String-to-Enumeration conversion.

Luckily, the .NET framework can do this for you, although it's not quite as
strightforward as parsing numbers.

The code for loading the two enumerations would be as follows:

ships.ShipClass = (ShipClass)Enum.Parse(typeof(ShipClass), reader.Value,
true);

ships.PrimaryTarget = (ShipClass)Enum.Parse(typeof(ShipClass),
reader.Value, true);

And by the way, I would change all the "Int16.Parse" calls to "Int32.Parse",
since you are parsing values to be stored in an "int" property (which maps
to "System.Int32"). If you really want to use "Int16" (and that's fine, if
your values will never exceed 32K), then change the Ship class to use
"short" or "Int16" (same thing) on those fields instead of "int".

Let me know if there's anything else keeping your program from working. I
didn't actually load it up in VisualStudio and run it...just glazed over it
in Notepad.


Joshua Russell said:
Hi,
Thanxs for that. I can't seem to get it to work tho. I thinik I'm using PHP
principals...
Would you mind looking through my source? I have attached it.

Thx

Here's some code to get you started. I made a few assumptions about the
ranges of certain values (where I throw the ArgumentOutOfRangeException).

I didn't include any of the code to load the XML, because there are several
good ways to do it (Xml Reader, Xml DOM, Dataset, Xml Fast Reader,
Serializer, etc.).

But this will certainly be better than an Array of Hashtables, for
performance, memory, and readability.

You may want to use an ArrayList instead of the Ship[] if you plan on adding
a variable number of ships to the collection. I don't want to tell you what
to do because I don't know enough about your project. I personally would
write my own collection (ShipCollection), and there are several examples
online describing how to use CollectionBase to do this (although I must
admit that many are incorrect since they don't implement OnValidate---but
I'm off topic).

If you have trouble with the attachment, let mw know and I'll find another
way to send it.

--Matthew W. Jackson
 
Hi,
I've think I've managed to get all my ships into the object 'ships'
succesfuly but I still don't quite understand how I'm suppost to iterate
through this object. Any ideas?

Thanx Josh

Matthew W. Jackson said:
My fault. I forgot that you're loading this data from XML, and need a
String-to-Enumeration conversion.

Luckily, the .NET framework can do this for you, although it's not quite as
strightforward as parsing numbers.

The code for loading the two enumerations would be as follows:

ships.ShipClass = (ShipClass)Enum.Parse(typeof(ShipClass), reader.Value,
true);

ships.PrimaryTarget = (ShipClass)Enum.Parse(typeof(ShipClass),
reader.Value, true);

And by the way, I would change all the "Int16.Parse" calls to "Int32.Parse",
since you are parsing values to be stored in an "int" property (which maps
to "System.Int32"). If you really want to use "Int16" (and that's fine, if
your values will never exceed 32K), then change the Ship class to use
"short" or "Int16" (same thing) on those fields instead of "int".

Let me know if there's anything else keeping your program from working. I
didn't actually load it up in VisualStudio and run it...just glazed over it
in Notepad.


Joshua Russell said:
Hi,
Thanxs for that. I can't seem to get it to work tho. I thinik I'm using PHP
principals...
Would you mind looking through my source? I have attached it.

Thx

Here's some code to get you started. I made a few assumptions about the
ranges of certain values (where I throw the ArgumentOutOfRangeException).

I didn't include any of the code to load the XML, because there are several
good ways to do it (Xml Reader, Xml DOM, Dataset, Xml Fast Reader,
Serializer, etc.).

But this will certainly be better than an Array of Hashtables, for
performance, memory, and readability.

You may want to use an ArrayList instead of the Ship[] if you plan on adding
a variable number of ships to the collection. I don't want to tell
you
what
to do because I don't know enough about your project. I personally would
write my own collection (ShipCollection), and there are several examples
online describing how to use CollectionBase to do this (although I must
admit that many are incorrect since they don't implement OnValidate---but
I'm off topic).

If you have trouble with the attachment, let mw know and I'll find another
way to send it.

--Matthew W. Jackson
 
foreach(Ship ship in ships) {
if(ship != null) {
MessageBox.Show(ship.Name);
}
}

--or--

for(int i = 0; i < shipCount; i++) {
MessageBox.Show(ships.Name);
}


There are other ways, but those should get you started.


Joshua Russell said:
Hi,
I've think I've managed to get all my ships into the object 'ships'
succesfuly but I still don't quite understand how I'm suppost to iterate
through this object. Any ideas?

Thanx Josh

My fault. I forgot that you're loading this data from XML, and need a
String-to-Enumeration conversion.

Luckily, the .NET framework can do this for you, although it's not quite as
strightforward as parsing numbers.

The code for loading the two enumerations would be as follows:

ships.ShipClass = (ShipClass)Enum.Parse(typeof(ShipClass), reader.Value,
true);

ships.PrimaryTarget = (ShipClass)Enum.Parse(typeof(ShipClass),
reader.Value, true);

And by the way, I would change all the "Int16.Parse" calls to "Int32.Parse",
since you are parsing values to be stored in an "int" property (which maps
to "System.Int32"). If you really want to use "Int16" (and that's fine, if
your values will never exceed 32K), then change the Ship class to use
"short" or "Int16" (same thing) on those fields instead of "int".

Let me know if there's anything else keeping your program from working. I
didn't actually load it up in VisualStudio and run it...just glazed over it
in Notepad.


Joshua Russell said:
Hi,
Thanxs for that. I can't seem to get it to work tho. I thinik I'm
using
PHP
principals...
Would you mind looking through my source? I have attached it.

Thx

Here's some code to get you started. I made a few assumptions about the
ranges of certain values (where I throw the ArgumentOutOfRangeException).

I didn't include any of the code to load the XML, because there are
several
good ways to do it (Xml Reader, Xml DOM, Dataset, Xml Fast Reader,
Serializer, etc.).

But this will certainly be better than an Array of Hashtables, for
performance, memory, and readability.

You may want to use an ArrayList instead of the Ship[] if you plan on
adding
a variable number of ships to the collection. I don't want to tell you
what
to do because I don't know enough about your project. I personally would
write my own collection (ShipCollection), and there are several examples
online describing how to use CollectionBase to do this (although I must
admit that many are incorrect since they don't implement OnValidate---but
I'm off topic).

If you have trouble with the attachment, let mw know and I'll find another
way to send it.

--Matthew W. Jackson

 
Back
Top