How to declare an use a struct containing an array?
-
I want to declare a simple array of stuct with the struct containing an array as shown below. But it does not compile. If you can't tell I'm very new to C#. public struct ChangeRec { public datetime LastTimeStamp; public int last ; public int mode[10] ; // I want an integer array of values } if I change the last line as shown, it will compile, but how do I control the size and access the elements? public int [] mode ; This seems too simple to be this hard. Thanks
You declare an array with int[] and then you set it's size when you create it. mode = new int[10]; This can go in your contructor. If your array was of a class then they would all be null and would need to be newd individually, too. As they are int, they will all be fine, but I mention this for future reference.
Christian Graus Driven to the arms of OSX by Vista.
-
I want to declare a simple array of stuct with the struct containing an array as shown below. But it does not compile. If you can't tell I'm very new to C#. public struct ChangeRec { public datetime LastTimeStamp; public int last ; public int mode[10] ; // I want an integer array of values } if I change the last line as shown, it will compile, but how do I control the size and access the elements? public int [] mode ; This seems too simple to be this hard. Thanks
Why does it have to be a struct? Unless this is for Interop I don't see the value in storing a reference type within a value type - why not just declare it as a class?
It definitely isn't definatley
-
I want to declare a simple array of stuct with the struct containing an array as shown below. But it does not compile. If you can't tell I'm very new to C#. public struct ChangeRec { public datetime LastTimeStamp; public int last ; public int mode[10] ; // I want an integer array of values } if I change the last line as shown, it will compile, but how do I control the size and access the elements? public int [] mode ; This seems too simple to be this hard. Thanks
Personally, I'd make your struct a class, and use a generic list instead of an array. At that point you could do something like this:
public class ChangeRec
{
private List<int> m_modes = new List<int>();public DateTime LastTimeStamp { get; set; } public int LastValue { get; set; } public List<int> Modes { get { return m\_modes; } } public ChangeRec() {}
}
There is now no need to initialize the list beyond what's done in the class. One other thing... I the variable "Last" refers to the last value added to the list. If that's the case, you can do this (uif you're using the code I posted above):
ChangeRec changeRec = new ChangeRec();
changeRec.Modes.Add(1);
changeRec.Modes.Add(2);
changeRec.Modes.Add(3);
changeRec.Modes.Add(4);int lastValue = (int)(changeRec.Modes.Last());
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001modified on Monday, January 5, 2009 11:42 AM
-
I want to declare a simple array of stuct with the struct containing an array as shown below. But it does not compile. If you can't tell I'm very new to C#. public struct ChangeRec { public datetime LastTimeStamp; public int last ; public int mode[10] ; // I want an integer array of values } if I change the last line as shown, it will compile, but how do I control the size and access the elements? public int [] mode ; This seems too simple to be this hard. Thanks
i think the fixed keyword might help you unsafe public struct ChangeRec { public DateTime LastTimeStamp; public int last ; public fixed int mode[10] ; // I want an integer array of values } } The big problem here is that you'll have to allow unsafe code within your project so it's probably not the best plan unless you're planning to use interop. i'd be tempted to implement the thing as a class and create getMode(int i) and setMode(int i) methods. Russ
-
Why does it have to be a struct? Unless this is for Interop I don't see the value in storing a reference type within a value type - why not just declare it as a class?
It definitely isn't definatley
moon_stick wrote:
storing a reference type within a value type
What reference type?
only two letters away from being an asset
-
moon_stick wrote:
storing a reference type within a value type
What reference type?
only two letters away from being an asset
An array is a reference type, no? Of type System.Array??
It definitely isn't definatley
-
An array is a reference type, no? Of type System.Array??
It definitely isn't definatley
I guess I looked at the data type and ignored the array
only two letters away from being an asset
-
I want to declare a simple array of stuct with the struct containing an array as shown below. But it does not compile. If you can't tell I'm very new to C#. public struct ChangeRec { public datetime LastTimeStamp; public int last ; public int mode[10] ; // I want an integer array of values } if I change the last line as shown, it will compile, but how do I control the size and access the elements? public int [] mode ; This seems too simple to be this hard. Thanks
The reason is that C# does not allow us to have instance field initializer in struct. there is a workaround if you do not want to use the unsafe code, such as: struct MyStruct { public int last; public int[] mode; public int[] Mode { get { if (this.mode == null) { this.mode = new int[10]; } return this.mode; } } } The code above can be compiled successfully. Tan Li I Love KongFu~
-
I want to declare a simple array of stuct with the struct containing an array as shown below. But it does not compile. If you can't tell I'm very new to C#. public struct ChangeRec { public datetime LastTimeStamp; public int last ; public int mode[10] ; // I want an integer array of values } if I change the last line as shown, it will compile, but how do I control the size and access the elements? public int [] mode ; This seems too simple to be this hard. Thanks
Like everyone else, I would question why this is not a class. That doesn't answer your question, however. If you do indeed need a struct...which indicates you need it to be allocated on the stack, I would do something like the following (this might not be exact...havn't used stackalloc in a long time):
public struct ChangeRec
{
public ChangeRec(byte modeSize)
{
mode = stackalloc int[modeSize];
}public DateTime LastTimeStamp; public int Last; public int\[\] mode;
}
-
Like everyone else, I would question why this is not a class. That doesn't answer your question, however. If you do indeed need a struct...which indicates you need it to be allocated on the stack, I would do something like the following (this might not be exact...havn't used stackalloc in a long time):
public struct ChangeRec
{
public ChangeRec(byte modeSize)
{
mode = stackalloc int[modeSize];
}public DateTime LastTimeStamp; public int Last; public int\[\] mode;
}
Thanks for all your inputs, I've got it working as a class. I still don't know how to make this an array using this structure. What I need is an array of 200 records with each record having the structure as originally shown and guidance on how to actually use it.