Click here to Skip to main content
15,888,098 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there,

I have a structure that I have created in a class and I create a constructor for the Struct. Now when I create an instance of the Structure from my Main procedure, and then send this new instance to a procedure to do stuff, it comes up with the above error.

C#
public Class Someclass
{
  public Struct Clues
  {
    public Clues(int value)
    {
       Array = new byte[2];
       num = value;
    }
    public byte[] Array; 
    public int num;
  };
  
}

So now in main I create the instance of this class struct
Someclass.Clues S = new Someclass.Clues(0);

And somewhere down the Line I parse this new Instance to a procedure:
SomeProcedure(S);

where it allocates values for the member of that structure

C#
SomeProcedure(Someclass.Clues qq)
{
   qq.Array[0]=0;   //Error Pops Up Here
   qq.num=25;
}
Posted
Updated 11-Oct-11 21:03pm
v2
Comments
StM0n 12-Oct-11 3:20am    
Are "Class" and "Struct" spelled correctly in your code? They should be lowercase...
codenameyash 12-Oct-11 3:22am    
yeah, they are all standard, exactly how C# likes them
theanil 12-Oct-11 3:26am    
no your 'c' in class and 's' in struct should be in lower case.
codenameyash 12-Oct-11 3:28am    
errrrrrmmmm, maybe I should have been more clear,the program compiles.. And they are all Lower case. dunno Y i didnt write them like that here :)
StM0n 12-Oct-11 3:29am    
it's ok ;)

Even i was getting the same error but by placing some procedure in some class it worked
here is the code

C#
public class Someclass
    {
        public struct Clues
        {
            public Clues(int value)
            {
                Array = new byte[2];
                num = value;
            }
            public byte[] Array;
            public int num;
        };
        public void xyz(Someclass.Clues qq)
        {
            qq.Array[0] = 0;   
            qq.num = 25;
        }
  }

    class Program
    {
        static void Main(string[] args)
        {
            Someclass.Clues S = new Someclass.Clues(0);
            Someclass x = new Someclass();
            x.xyz(S);

        }

    }
 
Share this answer
 
You were right, I was changing "S" ..... Very Very Very Silly Mistake, thanks for that....
 
Share this answer
 
Comments
StM0n 12-Oct-11 5:05am    
You're welcome ;)
qq = not set to new Someclass.Clues();

or make Clues static or create the object reference

C#
Someclass.Clues qq = new Someclass.Clues();
SomeProcedure(qq);


SomeProcedure(Someclass.Clues qq)
{
   qq.Array[0]=0;   //Error Pops Up Here
   qq.num=25;
}
 
Share this answer
 
C#
using System;
using System.Collections.Generic;

public class Someclass {
    public struct Clues {
        public Clues(int value) {
            Array = new byte[2];
            num = value;
        }
        public byte[] Array;
        public int num;
    };
}

public class MyClass {

    public static void Main() {
        Someclass.Clues S = new Someclass.Clues(0);        
        doSomething(S);        
    }

    private static void doSomething(Someclass.Clues qq) {
        qq.Array[0] = 0;   
        qq.num = 25;
    }        
}


Works fine with me... could you try?
 
Share this answer
 

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