473,507 Members | 2,447 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

defining array of structures

Another basic C# question, but haven't figured out how to do this one
either. Am used to defining a list of structures in C++, but am unsure
if you can do this in C#. As a simplified example, I have:

struct Command
{
int commandEnum;
string commandName;
int commandLength;
byte[] command;
}

static Command[] CommandList =
{
{1,"test 1",2,new byte[] {100,101}},
{2,"test 2",2,new byte[] {200,201}}
};

I am wanting to pre-define a list of device commands that follow the
format of the structure. But I've tried many permutations of the
CommandList definition, and the compiler always has a complaint about
what I'm doing. Is this possible and can somebody help me out with the
syntax?

Thanks again,
Mike

Nov 17 '05 #1
5 38347
Hello mblatch,

You could try something like this:

Firstly add a constructor to the structure

struct Command
{
int commandEnum;
string commandName;
int commandLength;
byte[] command;

public Command(int _commandEnum, string _commandName, int _commandLength,
byte[] _command)
{
commandEnum = _commandEnum;
commandName = _commandName;
commandLength = _commandLength;
command = _command;
}
}

then you can use something like

Command[] commands = {new Command(x, x, x, x), new Command(x, x, x, x)};

Hope that helps, Martin
Another basic C# question, but haven't figured out how to do this one
either. Am used to defining a list of structures in C++, but am
unsure if you can do this in C#. As a simplified example, I have:

struct Command
{
int commandEnum;
string commandName;
int commandLength;
byte[] command;
}
static Command[] CommandList =
{
{1,"test 1",2,new byte[] {100,101}},
{2,"test 2",2,new byte[] {200,201}}
};
I am wanting to pre-define a list of device commands that follow the
format of the structure. But I've tried many permutations of the
CommandList definition, and the compiler always has a complaint about
what I'm doing. Is this possible and can somebody help me out with
the syntax?

Thanks again,
Mike


Nov 17 '05 #2
Martin,

Thanks for the suggestion. I realize I could dynamically construct the
list in this fashion, but was looking for a static implementation that
could be accessed across multiple classes within the app.

I am now experimenting with using Object type to accomplish this, but I
lose the structure names when doing this and have to cast the elements
when I use them. Not an ideal solution.

static Object[,] commandList1 =
{
{1,"test 1",2,new byte[] {100,101}},
{2,"test 2",2,new byte[] {200,201}},
};

Thought I might be able to cast the Object entries to (Command) but no
such luck there either.

Mike

Nov 17 '05 #3
mblatch <mb*****@yahoo.com> wrote:
Thanks for the suggestion. I realize I could dynamically construct the
list in this fashion, but was looking for a static implementation that
could be accessed across multiple classes within the app.


I don't quite see what you mean. You can declare the variable and
expose it (preferrably via a property) with exactly the pattern Martin
gave before.

(Is there any reason for making it a structure rather than a class, by
the way?)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4
"Martin Carolan" <mc******@gmail.com> schrieb im Newsbeitrag
news:21*********************@news.cable.ntlworld.c om...

[slightly edited; does *exactly* what the original poster wanted]
struct Command
{
int commandEnum;
string commandName;
int commandLength;
byte[] command;

// You have to add this constructor to solve your problem
public Command(int commandEnum, string commandName, int commandLength,
byte[] command)
{
this.commandEnum = commandEnum;
this.commandName = commandName;
this.commandLength = commandLength;
this.command = command;
}

public static Command[] CommandList = { // this syntax creates a new
Command
new Command(1, "test 1", 2, new byte[] {100, 101}), new Command(2,
"test 2", 2, new byte[] {200, 201})
};
}


This code is the most simple correct solution to your problem.
Which really is a syntactical problem at heart.

The code

{ 1, "test 1", 2, new byte[] {100, 101} }

does *not* create a Command, it creates an object[] array instead.
You can't create instances of structs with above syntax! This is one of the
differences between C# and C++.

If you look at the documentation, you'll understand.

Structs tutorial
http://msdn.microsoft.com/library/de...tstutorial.asp

Arrays tutorial
http://msdn.microsoft.com/library/de...ystutorial.asp

Using structs (C# programmer's reference)
http://whidbey.msdn.microsoft.com/li...0e0797ba38.asp

Mark
Nov 17 '05 #5
Martin, Mark, Jon-

Thanks again for the assistance. I think I understand what you're
saying now. I guess I was viewing the C# structs too much like C++
structs, which is why I was trying to make this out of a struct instead
of a class. I now see there is a very fine distinction between C#
structs and classes.

I'll take these suggestions and should be good to go now.

Thanks,
Mike

Nov 17 '05 #6

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

Similar topics

4
3673
by: Christian Hackl | last post by:
I honestly wasn't able to find an answer for this design question using Google and Google Groups, so I apologize if it is asked too frequently :) Anyway: Let's say I have a multidimensional array...
4
1614
by: siliconmike | last post by:
Hello, I'd like to implement a large 2-d array 2d array size: 5000 x 5000 each element: int 4 bytes type: static, MYISAM how is this done in MySQL? I'm a newbie, so details will help. Mike
0
2165
by: Wayne Wengert | last post by:
I have a basic understanding of XML and I have a similar basic grasp of VB.NET for developing Windows applications. I am still struggling with the when/where/how in the proper use of OO classes and...
2
2732
by: kittu_phani | last post by:
Iam very much new to C-programing.Please give the silution for following problem. How to read data from stdin to array ofstructures -- Posted via http://dbforums.com
20
6808
by: Luke Matuszewski | last post by:
Welcome As suggested i looked into JSON project and was amazed but... What about cyclical data structures - anybody was faced it in some project ? Is there any satisactional recomendation... ...
7
3153
by: Sam | last post by:
Hello I have a structure called Company. struct Company { char *employee; char *employee_address; }; I want to build an array of this structure but the number of employees will change...
10
1677
by: padh.ayo | last post by:
Hi, I have a array structure called people It is properly initialized to 100 array structure elements. Now, I'm reading in from the command line a txt file, and I get them to open correctly. In...
4
11657
by: chinu | last post by:
HI all, i am declaring an array in javascript var a = new array(); now before assigning a value to the ith element of this array, i have to check if some value has already been assigned there....
1
1368
by: shariquehabib | last post by:
Hi all, I am defining array of structure like that : STRUCT_NAME array. I am fetching some records from EMP table and copying into that array one by one using while loop. But it will work only...
0
7223
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
7114
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
7377
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...
1
7034
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7488
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5045
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
412
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.