Structs Array

  • Thread starter Thread starter Ollie
  • Start date Start date
O

Ollie

I have a struct (below) and want to declare it as an Dynamic Array and add
values. but I am having trouble. I am new to C# and have looked high and low
but cannot find anywhere that tells me how to do it.

public struct Packs
{
public string PackName;
public long PackCount;
}

Cheers

Ollie
 
Hi Ollie,

You can use an ArrayList and store the Packs in there. To retrieve a pack, simply cast it to Packs. It could be that I misunderstand what you need.

ArrayList packList = new ArrayList();

packList.Add(new Packs());

Packs p = (Packs)packList[0];

p.PackName will be null in this sample and p.PackCount will be 0;
 
Ollie said:
I have a struct (below) and want to declare it as an Dynamic Array and add
values. but I am having trouble. I am new to C# and have looked high and low
but cannot find anywhere that tells me how to do it.

public struct Packs
{
public string PackName;
public long PackCount;
}

Cheers

Ollie
Arrays are not dynamic in C#. I believe you want the ArrayList class. Check
the help for ArrayList. There's a good explanation with code samples in VB,
C# and C++ showing how to create, populate and use it.
 
Ok, How about if I don't use a dynamic array.


Ollie said:
I have a struct (below) and want to declare it as an Dynamic Array and add
values. but I am having trouble. I am new to C# and have looked high and low
but cannot find anywhere that tells me how to do it.

public struct Packs
{
public string PackName;
public long PackCount;
}

Cheers

Ollie
Arrays are not dynamic in C#. I believe you want the ArrayList class. Check
the help for ArrayList. There's a good explanation with code samples in VB,
C# and C++ showing how to create, populate and use it.
 
It's a little unclear what you want. If you want to store Packs, you can do that easily with an ArrayList. If you don't need it to be dynamic you can create a Packs array

Packs[] packList = new Packs[size];
 
OK, What I need to do is count Packs. I don't know the PackName nor do I
know how many there are. What I do know that there are multiple instances of
the same PackName.

So, What I want to do is create a dynamic array of Packs and add these to
the array. Where the PackName already exists I just want to increment the
PackCount for the corresponding PackName.

Hope that makes sense.



It's a little unclear what you want. If you want to store Packs, you can do
that easily with an ArrayList. If you don't need it to be dynamic you can
create a Packs array

Packs[] packList = new Packs[size];
 
OK, What I need to do is count Packs. I don't know the PackName nor do
I know how many there are. What I do know that there are multiple
instances of the same PackName.

So, What I want to do is create a dynamic array of Packs and add these
to the array. Where the PackName already exists I just want to
increment the PackCount for the corresponding PackName.

Hope that makes sense.
<snip>

Use a hashtable

Hashtable list = new Hashtable();

// add XYZ to the list
if (list.Contains("XYZ"))
list["XYZ"] = (Int32)list["XYZ"] + 1;
else
list["XYZ"] = 1;

this will add XYZ to the list with a value of 1, if it's not already
there. If it's there, read out the previous value, increment it by 1 and
store it back into the list.

Of course, if you need to store more than a Int32 value for each name,
you should create a class to hold the various values.
 
Sounds like it would be easier to have two arrays...

String[] PackName;
Int[] PackCount;

Just not as tidy as using the Stuct.

OK, What I need to do is count Packs. I don't know the PackName nor do
I know how many there are. What I do know that there are multiple
instances of the same PackName.

So, What I want to do is create a dynamic array of Packs and add these
to the array. Where the PackName already exists I just want to
increment the PackCount for the corresponding PackName.

Hope that makes sense.
<snip>

Use a hashtable

Hashtable list = new Hashtable();

// add XYZ to the list
if (list.Contains("XYZ"))
list["XYZ"] = (Int32)list["XYZ"] + 1;
else
list["XYZ"] = 1;

this will add XYZ to the list with a value of 1, if it's not already
there. If it's there, read out the previous value, increment it by 1 and
store it back into the list.

Of course, if you need to store more than a Int32 value for each name,
you should create a class to hold the various values.
 
Back
Top