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

Structs Array

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
Nov 16 '05 #1
7 16355
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;

--
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2

"Ollie" <ol**********@hotmail.com> wrote in message
news:e8**************@TK2MSFTNGP09.phx.gbl...
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.

--
Peter [MVP Visual Developer]
Jack of all trades, master of none.
Nov 16 '05 #3
Ok, How about if I don't use a dynamic array.

"Peter van der Goes" <p_**********@toadstool.u> wrote in message
news:uN**************@TK2MSFTNGP10.phx.gbl...

"Ollie" <ol**********@hotmail.com> wrote in message
news:e8**************@TK2MSFTNGP09.phx.gbl...
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.

--
Peter [MVP Visual Developer]
Jack of all trades, master of none.

Nov 16 '05 #4
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];

--
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #5
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.

"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:opr91s26b0klbvpo@morten_x.edunord...
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];

--
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #6
"Ollie" <ol**********@hotmail.com> wrote in
news:OD**************@TK2MSFTNGP10.phx.gbl:
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.

--
Lasse Vågsæther Karlsen
la***@vkarlsen.no
PGP KeyID: 0x0270466B
Nov 16 '05 #7
Sounds like it would be easier to have two arrays...

String[] PackName;
Int[] PackCount;

Just not as tidy as using the Stuct.

"Lasse Vågsæther Karlsen" <la***@vkarlsen.no> wrote in message
news:Xn*****************************@207.46.248.16 ...
"Ollie" <ol**********@hotmail.com> wrote in
news:OD**************@TK2MSFTNGP10.phx.gbl:
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.

--
Lasse Vågsæther Karlsen
la***@vkarlsen.no
PGP KeyID: 0x0270466B
Nov 16 '05 #8

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

Similar topics

10
by: Kieran Simkin | last post by:
Hi, I wonder if anyone can help me, I've been headscratching for a few hours over this. Basically, I've defined a struct called cache_object: struct cache_object { char hostname; char ipaddr;...
2
by: Michiel Rapati-Kekkonen | last post by:
recently I was put on the right trail in the matter of searching in arrays of structs. I got it working, a bit. Unfortunately, as soon as I want more, I'm stuck again: it is basically a...
5
by: Paminu | last post by:
Why make an array of pointers to structs, when it is possible to just make an array of structs? I have this struct: struct test { int a; int b;
15
by: Paminu | last post by:
Still having a few problems with malloc and pointers. I have made a struct. Now I would like to make a pointer an array with 4 pointers to this struct. #include <stdlib.h> #include <stdio.h>...
11
by: Cliff Martin | last post by:
Hi, I am reading a fairly large file a line at a time, doing some processing, and filtering out bits of the line. I am storing the interesting information in a struct and then printing it out....
6
by: =?Utf-8?B?QWxleGFuZGVyZmU=?= | last post by:
Hi, I have a C# program that uses an unmanaged dll that has a function similar to the signature below : void f(out MyStruct arr, out int num); // num = actual array length returned The array...
5
by: dev_15 | last post by:
Hi, I'm going through some code and thought that this allocates an array of structs but its supposed according to comments to allocate an array of pointer to structs. What does it actually do ...
2
by: jonpb | last post by:
Using .NET 3.5, I need to pass an array of structs as parameter to a C++ unmanaged function. The C++ dll stores some data in an unmanaged cache, the function writes the values into the array of...
2
by: hal | last post by:
Hi, I'm trying to make an array of pointers to 'TwoCounts' structs, where the size of the array is arraySize. Right now I'm just mallocing enough space for all the pointers to the structs, and...
3
by: charlesh3 | last post by:
I'm an engineer with a C background that is trying to do my first c# project. I created a struct: public struct MyStruct { public static UInt32 bUsed; public...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.