how to set value in abstract class c#

I’m trying assign value AbstractArAdjustmentLine list property but getting error “Object reference not set” Below 2 class are generated through dll, I can’t change the class structure.

public class ArAdjustmentCreate : AbstractArAdjustment
{
    public ArAdjustmentCreate(string controlId = null);

    public override void WriteXml(ref IaXmlWriter xml);
}



public abstract class AbstractArAdjustment: AbstractFunction
    {
        public List<AbstractArAdjustmentLine> Lines;
        public DateTime? GlPostingDate;
        public DateTime TransactionDate;
        public string CustomerId;
    }

public abstract class AbstractArAdjustmentLine : IXmlObject
    {
        public string WarehouseId;
        public string EmployeeId;
        protected AbstractArAdjustmentLine();     
    }

// Creating instance of ArAdjustmentCreate

ArAdjustmentCreate arAdjustmentCreate = new ArAdjustmentCreate()
            {                
                CustomerId = "23",
                TransactionDate = DateTime.Now,
                GlPostingDate = DateTime.Now,                    
            };    
            AbstractArAdjustmentLine arAdjustmentLine=null;                            
            arAdjustmentLine.WarehouseId = "788"; // getting error Object refference not set
            arAdjustmentLine.EmployeeId = "100";            
            arAdjustmentCreate.Lines.Add(arAdjustmentLine);

how to set value in AbstractArAdjustmentLine abstract class?

Answer

You set AbstractArAdjustmentLine arAdjustmentLine = null;, probably because you realized you can’t instantiate an abstract class, but you need an instance to set the properties. The only practical way you can use an abstract class is by inheriting in a subtype:

abstract class A { }
class B : A { }

// Works:
A a = new B();

// Works:
B b = new B();

// Does not work:
A c = new A();

See null keyword and abstract in the docs, and What is a NullReferenceException, and how do I fix it? from this site.

Leave a Reply

Your email address will not be published. Required fields are marked *