Results 1 to 11 of 11

Thread: sized array as a member of a struct?

  1. #1

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    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....



    - ØØ -

  2. #2
    Fanatic Member
    Join Date
    May 2005
    Posts
    898

    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

  3. #3

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: sized array as a member of a struct?

    Quote Originally Posted by grilkip
    I know I'm being a smartypants but why don't you turn in into a class and be done with it?

    To me in seems like you put on the dumbypants..


    They are not the same...

    - Classes are reference types and structs are value types
    - Classes are allocated on the heap, structs are allocated on the stack...


    Now find me a solution...

    - ØØ -

  4. #4
    Fanatic Member
    Join Date
    May 2005
    Posts
    898

    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

  5. #5

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    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

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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;
    			}
    		}
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: sized array as a member of a struct?

    Quote 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..


    - ØØ -

  8. #8
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    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.

  9. #9

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    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?


    - ØØ -

  10. #10
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    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.

  11. #11

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: sized array as a member of a struct?

    Quote Originally Posted by wossname
    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.

    Ain't going to happen.....just to bring you on my side of the view here, so you understand what I am doing.. (hehe..funny way of saying it..)

    MD2 is the 3D file format that was made for Quake II......so what I am doing is to make a loader and renderer for MD2 files in C# and DirectX. I think the loader is working now except for loading of the skins. The texture coordinates are still a bit out of the blue.. But I found out that I need to make a camera class first to be able to see clearer what happens..

    So it is not my app that saves the files, hence they are not going to be serialized. At the moment, I am loading them the hard way. In stead of loading one chunk at a time (i.e a strckt with i.e 60bytes), I am loading byte for byte and puts it where it should be. The code gets extremly long this way. But at least it works. I could always use the C++ way with memcpy API to copy a struct into memory, but I would like to do it the .NET way...you know the feeling...


    PS: LIke your new sig...


    - ØØ -

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width