user3211090
user3211090

Reputation: 21

Struct in Class access Class Members

since im new to C# im not sure if its possible to access a classes members from a structure in the class like this:

namespace Hello
{
  class Foo
  {
    public int cint0;
    private struct Struct7
    {
  public string str0;
      void Work()
      {
        str0 = "";
        cint0 = 22;//how to access cint0 from within the struct
      }
    }
  }
}

if there is a way to do it maybe someone can help

Upvotes: 1

Views: 159

Answers (2)

JaredPar
JaredPar

Reputation: 754715

The nested struct has proper access to the fields, even if they were private. They are instance fields though hence you must access them off of an instance of Foo

void Work(Foo f) {
  f.cint0 = 22; 
}

Upvotes: 1

Selman Genç
Selman Genç

Reputation: 101681

Nested struct inside of a class is a really bad idea,anyway, you need to a class instance to access a non-static members of a class like this:

Foo f = new Foo();
f.cint0 = 22

Upvotes: 0

Related Questions