defining array of structures

  • Thread starter Thread starter mblatch
  • Start date Start date
M

mblatch

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
 
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
 
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
 
mblatch said:
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?)
 
[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/default.asp?url=/library/en-us/csref/html/vcwlkstructstutorial.asp

Arrays tutorial
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vcwlkarraystutorial.asp

Using structs (C# programmer's reference)
http://whidbey.msdn.microsoft.com/l...html/cea4a459-9eb9-442b-8d08-490e0797ba38.asp

Mark
 
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
 
Back
Top