0

I have this example

 public class AClass
{
     public class BClass
     {
         public string name;
         public string id;
     }

}

now i want to make an intsance of Aclass

public class Program
{
    public static void Main()
    {

        AClass newitem = new AClass();

    }
}

but i haven't access to the properties of BClass with the object newitem. How can achieve that with an instance of AClass?

Vishwa Ratna
  • 5,567
  • 5
  • 33
  • 55
dimmits
  • 1,999
  • 3
  • 12
  • 30
  • If you haven't instantiated a class of type `AClass.BClass` then there is no `BClass` whose properties you can get. – Chris Dec 17 '18 at 15:00
  • 3
    Welcome to Stack Overflow. The code you've provided so far doesn't create any instances of `BClass`, so no, you wouldn't be able to access any members. (Note that you've also provided fields, not properties.) What are you trying to achieve here? Why have you used a nested class at all? There are perfectly good reasons for using nested classes, but until we know what you're trying to achieve we won't be able to tell whether it's a good idea in your case or not. – Jon Skeet Dec 17 '18 at 15:01

1 Answers1

1

Class A and Class B don't have a relationship in this case. Sounds like you might want to make an object of type BClass a property of AClass.

 public class AClass
 {
     public BClass BClass { get; set; }
 }

 public class BClass
 {
     public string name;
     public string id;
 }

public class Program
{
    public static void Main()
    {
        AClass newitem = new AClass();
        BClass myBClass = newitem.BClass;
    }
}
Dan Schnau
  • 1,505
  • 14
  • 17