Click here to Skip to main content
15,896,527 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi!
how an array is declare in structure??

C#
public struct test
{
    public byte itemcode;
    public byte[] itemname;
    ////20
    public  byte method;
    public string[] controlname4QC;
    //  public string reserved;
    public byte[] reserved;
    public void initialize()
    {
        for (int i = 0; i < 20; i++)
        {
            reserved[i] = 0;
            controlname4QC[i] = "0";
        }
             
    }

    //test* p;
}


Is this code correct?
Posted
Updated 21-Aug-14 5:17am
v2
Comments
Kornfeld Eliyahu Peter 14-Aug-14 3:51am    
No.
1. Why struct and not class?
2. Why special initialization method instead of standard constructor?
3. Where you initialize those arrays? Nowhere I can see, so putting values at position i will cause troubles...
Member 10994712 14-Aug-14 4:30am    
at first time i declared tha array as
public byte[] itemname=new byte[20];
but at that time i got an error that
Error 1 'structuretest.Form1.test.itemname': cannot have instance field initializers in structs C:\Users\ADL65654\Desktop\128.14pointerunofinal\structuretest - Copy\structuretest\Form1.cs 25 27 structuretest
Kornfeld Eliyahu Peter 14-Aug-14 4:35am    
One more reason not to use struct...
See OGs' answer and the article in my comment...

No.
An array is a reference type, not a value type, and so to use it you need to allocate the space on the heap via the new keyword.
You don't do that, so you will get null reference errors when you try to access the arrays.

In addition, as Eliyahu has said, you should use the standard constructor instead of "inventing" your own.

Plus, that is probably above the maximum size guidelines for a struct which is 16 bytes: remember that each reference will take either 4 or 8 bytes depending on the 32/64 bit nature of your application / OS.

It looks like you are trying to "match" a c / c++ structure for intercommunication - if so, then you need to think rather more carefully about it: a reference is not the same as a pointer!
 
Share this answer
 
Comments
Kornfeld Eliyahu Peter 14-Aug-14 4:11am    
http://www.codeproject.com/Articles/728836/Using-struct-and-class-whats-that-all-about
OriginalGriff 14-Aug-14 4:26am    
I've seen that before somewhere! :laugh:
Member 10994712 14-Aug-14 4:27am    
then how can i do that..can u please correct my code?
OriginalGriff 14-Aug-14 4:43am    
Well - no, because it looks like it's not your code anyway, and you would seem to have copied it from C/C++ and just filed the serial numbers off! :laugh:

You can't do that: C# and C++ are very different languages which share a common syntax. You need to look carefully at what you started with (and how you are going to use it) rather than blindly translating. That is a recipe for a lot of problems later!
Member 10994712 21-Aug-14 7:21am    
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
public byte[] itemname;
is this possible?
This is a class with the same members of your struct, this is the best I can do for you.
C#
public class Test
    {
        public byte Itemcode{get; set;}
        public byte[] Itemname{get; set;}

        public byte Method { get; set; }
        public string[] Controlname4QC { get; set; }
        //  public string reserved;
        public byte[] Reserved{get; set;}
        public Test()
        {
            Itemcode = 0;
            Itemname = new byte[20];
            Method = 0;
            Controlname4QC = new string[20];
            Reserved = new byte[20];
        }
    }
 
Share this answer
 
It can be done in a stuct with a constructor like:

C#
public struct ProofWithStruct
    {

        public ProofWithStruct(int charArraySize)
                : this()
            {
                domain = new char[charArraySize];
                Peers = new int[20];
                NumberOfPeers = 20;
                assignValue = '[';
            }

            public char[] domain;
            public int[] Peers;
            public int NumberOfPeers;
            public char assignValue;
    }


but keep the maximum of 16 bytes for your struct in mind or you like to have performance issues.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900