0

hi I have created a ViewModel Class like this

public class ClassA
{
    public string A { get; set; }
    public string B { get; set; }
    public List<ClassC> c { get; set; }
    public List<ClassD> d { get; set; }
}
public class ClassC
{
    public string A1 { get; set; }
    public string B1 { get; set; }
}
public class ClassD
{
    public string A1 { get; set; }
    public string B1 { get; set; }
}

then i have adding values like this in MainClass

public ClassA MainClass()
    {
        ClassA CA = new ClassA();
        CA.A = "abcd";
        CA.B = "efgh";

        ClassC CC = new ClassC();
        CC.A1 = "qwer";
        CC.B1 = "rewq";
        CA.c.Add(CC);

        ClassD CD = new ClassD();
        CD.A2 = "lkjh";
        CD.B2 = "aqws";
        return CA;
    }

so i am getting error at

CA.c.Add(CC);

and the error was "object reference not set to an instance of an object"

whatever return ViewModel that one I going to compare with actual models, so how could I do this.

mjwills
  • 23,389
  • 6
  • 40
  • 63
Amarnath
  • 113
  • 2
  • 11
  • 1
    public class ClassA { public string A { get; set; } public string B { get; set; } public List c { get; set; } public List d { get; set; } public ClassA() { c=new List(); d=new List(); } } – go.. Sep 26 '18 at 13:19
  • you need new instances for array – go.. Sep 26 '18 at 13:19
  • @snnbrn yes i need new instance for creating new array and thanks for help – Amarnath Sep 26 '18 at 13:29

0 Answers0