Can we use the Queue Class for structs?
-
The Queue Class documentation doesn't say if we can use the class for queueing data structures but I am guessing that as the struct is considered a data type it might be OK. Can anyone confirm or deny that? Secondly, how do I define and instantiate the equivalent of this C structure in C# struct ADRMSG { long PGN; byte PGNspec; byte RxData[8]; <--- This is the confusing part for me } Thirdly, can I put this definition in a header file so that two seperate class files can enqueue and dequeue these structs from the queue? Thanks for your patience with my newbie questions. Cheers, Bruce
-
The Queue Class documentation doesn't say if we can use the class for queueing data structures but I am guessing that as the struct is considered a data type it might be OK. Can anyone confirm or deny that? Secondly, how do I define and instantiate the equivalent of this C structure in C# struct ADRMSG { long PGN; byte PGNspec; byte RxData[8]; <--- This is the confusing part for me } Thirdly, can I put this definition in a header file so that two seperate class files can enqueue and dequeue these structs from the queue? Thanks for your patience with my newbie questions. Cheers, Bruce
You can use a value type as T in a Queue<T> if that's what you were asking. You can also use a fixed length array, like so: unsafe fixed byte array[8]; you'd have to turn on unsafe code, obviously. You can Not put it in a header file, but you don't need to either. Just declare your struct public (on internal).
Last modified: 40mins after originally posted --
-
The Queue Class documentation doesn't say if we can use the class for queueing data structures but I am guessing that as the struct is considered a data type it might be OK. Can anyone confirm or deny that? Secondly, how do I define and instantiate the equivalent of this C structure in C# struct ADRMSG { long PGN; byte PGNspec; byte RxData[8]; <--- This is the confusing part for me } Thirdly, can I put this definition in a header file so that two seperate class files can enqueue and dequeue these structs from the queue? Thanks for your patience with my newbie questions. Cheers, Bruce
Yes, you can put struct values in a queue, but you probably should not use a struct at all in this case. A struct in .NET is a different concept from a struct in C. In .NET a struct is always a value type, while in C the struct is just a definition of the data and if it's a value type or reference type depends on how you use it. Use a class like this:
public class AddressMessage {
public long ParameterGroupNumber { get; private set; }
public byte ParameterGroupNumberSpecific { get; private set; }
public byte[] RecievedData { get; private set; }public AddressMessage(long number, byte specific, byte[] data) {
ParameterGroupNumber = number;
ParameterGroupNumberSpecific = specific;
RecievedData = data;
}}
C# doesn't use header files at all, and class files are not compiled separately so you don't need header files to share definitions. As long as the class is in the same application, you can reach it by simply specifying the namespace where it is.
Despite everything, the person most likely to be fooling you next is yourself.
modified on Friday, February 20, 2009 4:20 PM
-
The Queue Class documentation doesn't say if we can use the class for queueing data structures but I am guessing that as the struct is considered a data type it might be OK. Can anyone confirm or deny that? Secondly, how do I define and instantiate the equivalent of this C structure in C# struct ADRMSG { long PGN; byte PGNspec; byte RxData[8]; <--- This is the confusing part for me } Thirdly, can I put this definition in a header file so that two seperate class files can enqueue and dequeue these structs from the queue? Thanks for your patience with my newbie questions. Cheers, Bruce
Yeah, what they said. Plus, yes, you can use a header file if you like (I do), but you don't need to and shouldn't.
-
Yeah, what they said. Plus, yes, you can use a header file if you like (I do), but you don't need to and shouldn't.
PIEBALDconsult wrote:
yes, you can use a header file if you like (I do)
I would like to know a little more about that. TIA
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
-
Yes, you can put struct values in a queue, but you probably should not use a struct at all in this case. A struct in .NET is a different concept from a struct in C. In .NET a struct is always a value type, while in C the struct is just a definition of the data and if it's a value type or reference type depends on how you use it. Use a class like this:
public class AddressMessage {
public long ParameterGroupNumber { get; private set; }
public byte ParameterGroupNumberSpecific { get; private set; }
public byte[] RecievedData { get; private set; }public AddressMessage(long number, byte specific, byte[] data) {
ParameterGroupNumber = number;
ParameterGroupNumberSpecific = specific;
RecievedData = data;
}}
C# doesn't use header files at all, and class files are not compiled separately so you don't need header files to share definitions. As long as the class is in the same application, you can reach it by simply specifying the namespace where it is.
Despite everything, the person most likely to be fooling you next is yourself.
modified on Friday, February 20, 2009 4:20 PM
Thanks for the feedback everyone. I am porting a C ap to C# and in the legacy code we have a file that handles packets of data that currently arrive as a structure from another file. I would like to pass data into the class file as a structure rather than by seperate set commands so that the data arrives as a combined packet. So am I still wrong to try and do this as a a queue of structs? Cheers, Bruce
-
Thanks for the feedback everyone. I am porting a C ap to C# and in the legacy code we have a file that handles packets of data that currently arrive as a structure from another file. I would like to pass data into the class file as a structure rather than by seperate set commands so that the data arrives as a combined packet. So am I still wrong to try and do this as a a queue of structs? Cheers, Bruce
Yes, you can't read the data directly from the file as a struct anyway, so there is no reason to use a struct at all, use a class. You can use a BinaryReader to read raw data from a file, but you need to read each value by itself.
Despite everything, the person most likely to be fooling you next is yourself.