|
-
Aug 20th, 2005, 05:04 AM
#1
sized array as a member of a struct?
Is it possible? To give a size to an array that is a member of a struct at coding time?
Like in C++ I would have done this:
Code:
struct MD2_Tri{
float point[3];
char morro;
};
but I can't do that in C# can I? And it doesn't look like I can do it using NEW outside a function either can I? And I can't have a struct with a constructor without parameters in C# can I?
This sounds stupid. So if I want an sized array in my struct in C# I need to have a constructor with a parameter that I don't even use? Please tell me that I am totaly wrong here, and that me puking all the time have made me hurl up half my brain....
- ØØ -
-
Aug 20th, 2005, 05:39 AM
#2
Fanatic Member
Re: sized array as a member of a struct?
I know I'm being a smartypants but why don't you turn in into a class and be done with it? 
"so just keep in mind that fantasy is not the same as realtiy and make sure u remember that wii sports may be fun but u cant count on it as exercise ok cool bye" - HungarianHuman
-
Aug 20th, 2005, 05:53 AM
#3
Re: sized array as a member of a struct?
-
Aug 20th, 2005, 06:17 AM
#4
Fanatic Member
Re: sized array as a member of a struct?
Your array would be a reference to the heap-allocated array anyway.
.NET 2.0: http://msdn2.microsoft.com/library/zycewsya.aspx
"so just keep in mind that fantasy is not the same as realtiy and make sure u remember that wii sports may be fun but u cant count on it as exercise ok cool bye" - HungarianHuman
-
Aug 20th, 2005, 06:44 AM
#5
Re: sized array as a member of a struct?
But all the other 999 members will still be on the stack. And as it says in your link there will be a way to achive this in C# 2.0...grrrrrr....why the BEEEEP can't they just implement stuff like this right away. This is the 999 time I am trying to port some C++ code, and then it suddenly stops because C# is dumb, and then they say it will be out in C# 2.0.....ggggggggrrrrrrrrr
-
Aug 20th, 2005, 07:52 AM
#6
Re: sized array as a member of a struct?
If C# is dumb then why not use C++.NET? C# is a different language so you have to expect some differences. grilkip's link also mentions that fixed members are unsafe code, which is not the end of the world but is generally to be avoided unless circumstances really do require it.
What about if you give your structure a private variable that is an array and then a public property to access it. You could code the property so that it checks whether the array is null and, if so, instantiates it:
Code:
private float[] m_Point;
public float[] Point
{
get
{
if (this.m_Point == null)
this.m_Point = new float[3];
return this.m_Point;
}
}
-
Aug 20th, 2005, 08:15 AM
#7
Re: sized array as a member of a struct?
 Originally Posted by jmcilhinney
If C# is dumb then why not use C++.NET? C# is a different language so you have to expect some differences. grilkip's link also mentions that fixed members are unsafe code, which is not the end of the world but is generally to be avoided unless circumstances really do require it.
What about if you give your structure a private variable that is an array and then a public property to access it. You could code the property so that it checks whether the array is null and, if so, instantiates it:
Code:
private float[] m_Point;
public float[] Point
{
get
{
if (this.m_Point == null)
this.m_Point = new float[3];
return this.m_Point;
}
}
Yeah, but then again, then I could just have a constructor with the size couldn't I? That is what I mean with dumb. You are allowed to do it as in C++, just have to use two steps. And the other thing I ment with dumb is that they are making the change, just not yet....
And about unsafe code. Correct me if I am wrong. If I want to check the size of the struct then I have to use unsafe code anyway, don't I? Something like this:
Code:
int structSize;
unsafe { structSize = sizeof(myStruct); }
//Here I can use structSize.
Because what I finaly want to do is to read in from a Binary file, and the struct is the header. But I guess that if I interfeer with a lot of properties and functions, then the sizeof function will no longer return the right size, will it? Not even sure if it is so smart to read in a struct from a file anyway, since it will start to overcomplicate C# and make it look even worse then C++. Using memcpy API would probably be prettier then something like this:
Code:
[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct REKORD1
{
byte b1;
ushort w1;
ushort w2;
uint dw1;
}
byte[] data; // read from file
GCHandle gData = GCHandle.Alloc(data, GCHandleType.Pinned);
REKORD1 newRecord = (REKORD1)Marshal.PtrToStructur(gData.AddrOfPinnendObject(), typeof(REKORD1));
gch.Free();
Hmmmm......I feel like I am biting my self in the foot today..
- ØØ -
-
Aug 20th, 2005, 10:07 AM
#8
Re: sized array as a member of a struct?
How about this as...
Code:
[Serializable]
struct MD2_Tri
{
float[] point;
char morro;
public MD2_Tri(char morroInit)
{
point = new float[3];
morro = morroInit;
}
}
There is a parameter but at least it is used, you can always ignore it though, or you could have a char constant called MD2_TRI_INITIALIZER that you pass into the constructor from your calling method.
Also see that it is marked Serializable, that makes it writable to a binary file. Why are you writing/reading files though? I thought I was doing the map loader.
Binary files are simple, you don't need to do half as much work as you do in C++.
I don't live here any more.
-
Aug 20th, 2005, 01:44 PM
#9
Re: sized array as a member of a struct?
No this is not for DoomSharp..
I started to think that serialization might be the way to go. So I started to read up about it. But I still have this big bloaty head, and the puking, so I didn't get around it all. But to me it looked like it had to be saved with serializatoin first before you could read it. Is that right? It looked to me that C# is doing some padding for you to allign the bytes in a struct, so you can never be sure what order the member variables in a struct are alligned in memory because .NET takes care of that for you and padds it. Does the [Serializable] say to .NEt that it should not pad it and rearange, or do you really have to save it with [Serializable] first to be able to read it with [Serializable] again later?
- ØØ -
-
Aug 20th, 2005, 04:29 PM
#10
Re: sized array as a member of a struct?
A serializer cannot load something that was not saved by a serializer.
From the sound of it you are updating old files that were saved in a different format. What I suggest is that you load the old files in the old format and then save them immediately with serialization. Probably by running a new .net object along side the old object and copying the variable data across manually, then serializing.
If you are converting file types then you must load them manually and save serially. You could do this all in one class if you wanted. But its fiddly to load old data structures manually, you normally have to use marshalling to help out. I hate marshalling.
Serialization has its own way of saving things and I don't know the system, but thats ok because its not necessary to know it.
Last edited by wossname; Aug 20th, 2005 at 04:37 PM.
I don't live here any more.
-
Aug 21st, 2005, 08:05 AM
#11
Re: sized array as a member of a struct?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|