0

Using c# I would like to set the variables within a struct which is a member of a class, from within that class. Pretty new to c#. Help appreciated.

class myclass
{
   public struct mystruct
   {
       public int something;
   }

   public void init() 
   {
      mystruct.something = 20; // <-- this is an error
   }

   static void Main(string[] args)
   {
       myclass c = new myclass();
       c.init();          
   }
}

Error: 'An object reference is required for the non-static field, method, or property myclass.mystruct.something'

quetzalcoatl
  • 32,194
  • 8
  • 68
  • 107
Willeman
  • 720
  • 10
  • 24
  • 1
    Are you sure this is all code? This will give a compiler error, no runtime error. If you have a field `mystruct mystruct`, this error will not occur as it will be initialized. Please show the actual code. – CodeCaster Oct 30 '14 at 10:22
  • 1
    `mystruct` is a type, not a field of that type.. – Konrad Kokosa Oct 30 '14 at 10:23
  • You have defined a struct but not an instance of that struct. – DavidG Oct 30 '14 at 10:23
  • Wow, this is amazing! I would have bet that most if not all would point out that you are not only confusing Type with Instance but also not using Struct in the recommended way.. You should use structs only as immutables, meaning that you should make all members readonly and set them only in the constructor! If you need read-write acces to the members you should not use struct but class! – TaW Oct 30 '14 at 11:08

6 Answers6

5

mystruct is a type within class, but you do not have any field with that type:

class myclass
{
   public struct mystruct
   {
       public int something;
   }

   private mystruct field;

   public void init() 
   {
      field.something = 20; // <-- this is no longer an error :)
   }

   static void Main(string[] args)
   {
       myclass c = new myclass();
       c.init();          
   }
}
Konrad Kokosa
  • 16,563
  • 2
  • 36
  • 58
2

There is a difference between struct definition and struct instance. You need to instantiate mystruct first, then you can assign value to it - either that or declaring the mystruct something as static field.

public struct mystruct
{
  public int something;
}

var foo = new mystruct();
foo.something = 20;

or

public struct mystruct
{
  public static int something;
}

mystruct.something = 20;
Ondrej Svejdar
  • 21,349
  • 5
  • 54
  • 89
1

You should create an object for mystruct

public void init() 
{
  mystruct m = new mystruct();
  m.something = 20; 
}
  • consider adding more words (downvote is not mine btw) – quetzalcoatl Oct 30 '14 at 10:24
  • From what I see, someone gave you "-1", then after your edit, someone (apparently another person) gave you a "+1". I didn't do anything except for dropping you a note.. sorry, don't know more, but thanks for the bit of explanation in the answer! :) – quetzalcoatl Oct 30 '14 at 10:31
1
public struct mystruct
{
   public int something;
}

This is just a definition. As the error states, you must have an initialized object to use instance variables.

class myclass
{
   public struct mystruct
   {
       public int something;
   }

   public void init() 
   {
      mystruct haha = new mystruct();
      haha.something = 20; // <-- modify the variable of the specific instance
   }

   static void Main(string[] args)
   {
       myclass c = new myclass();
       c.init();          
   }
}
walther
  • 13,466
  • 5
  • 41
  • 67
1
class myclass
{
  mystruct m_mystruct;

   public void init() 
   {
      m_mystruct.something = 20; 
   }

   static void Main(string[] args)
   {
       myclass c = new myclass();
       c.init();          
   }
}

 public struct mystruct
   {
       public int something;
   }
ShayD
  • 920
  • 8
  • 18
1

Wow, this is amazing!

I would have bet that most if not all would point out that you are not only confusing Type with Instance but also not using Struct in the recommended way..

You should use structs only as immutables, meaning that you should make all members readonly and set them only in the constructor!

class myclass
{
  mystruct oneMyStruct;

  public struct mystruct
  {
    public readonly int something;
    public mystruct(int something_) { something = something_; }
  }

  public void init()
  {
    oneMyStruct = new mystruct(20);
  }

  static void Main(string[] args)
  {
    myclass c = new myclass();
    c.init();
  }

}

If you need read-write acces to the members you should not use struct but class!

TaW
  • 53,122
  • 8
  • 69
  • 111
  • Thanks for the in-depth analysis. What is the reasoning for defining the use of struct as a read-only construct? – Willeman Oct 30 '14 at 11:53
  • 1
    Many source on this. Start reading [here](http://stackoverflow.com/questions/3751911/why-are-c-sharp-structs-immutable)! – TaW Oct 30 '14 at 11:58