简体   繁体   中英

How to assign value to an array class variable which is inside a class

public partial class OrderWS
{
    private FulfillmentWS[] fulfillmentListField;
}

public partial class FulfillmentWS
{
    private Customer fulfillingCustomerField;

    private string fulfillingOutletIdField;
}

Above two classes are in two different .cs files.I'm creating object for OrderWs class and trying to assign value to fulfillingOutletIdField which is in another array class.

Need some logic to assign value to fulfillingOutletIdField variable which is in FulfillmentWS class.

Change private fields in FulfillmentWS to public properties.

public partial class OrderWS
{
    private FulfillmentWS[] fulfillmentListField;
}

public partial class FulfillmentWS
{
    public Customer fulfillingCustomerField { get; set; };

    public string fulfillingOutletIdField { get; set; };
}

Do you know about Properties ?

public partial class FulfillmentWS 
{
    private Customer fulfillingCustomerField;
    public Customer FulFillingCustomer
    {
        get { return fulfillingCustomerField; }
        set { fulfillingCustomerField = value; }
    }

    private string fulfillingOutletIdField;
    public string FulFillingOutletId
    {
        get { return fulfillingOutletIdField; }
        set { fulfillingOutletIdField = value; }
    }
}

Use public Properties to interact with your private fields.

Update: Use Constructor to init array:

public partial class OrderWS
{
   private FulfillmentWS[] fulfillmentListField;
   public OrderWS(int length)
   {
       fulfillmentListField = new FulfillmentWS[length];
   }
}

Then

OrderWS WPObj = new OrderWS(2); // Array length must be >= 2 if you want to access index 1.
WPObj.fulfillmentList[1] = new FulfillmentWS();     
WPObj.fulfillmentList[1].fulfillingOutletId = "1234";

As you define fulfillingoutletIdField and other class as a private, it is not accessible due to protection level, either you can use protected or public (see below link for access modifiers for the variable scope).

Just change that thing and you can use it anywhere as above suggested.

public partial class FulfillmentWS
{
    public Customer fulfillingCustomerField { };

    public string fulfillingOutletIdField { };
}

Access Modifiers (C# Programming Guide)

https://msdn.microsoft.com/en-us/library/ms973875.aspx

http://www.c-sharpcorner.com/UploadFile/84c85b/default-scope-of-aC-Sharp-class/

You can create a function:

public partial class OrderWS
{
     private FulfillmentWS[] fulfillmentListField;
     public void function()
     {
         ...
     }
}

Or you can use accessors get set:

public partial class OrderWS
{
     private FulfillmentWS[] fulfillmentListField;
     public FulfillmentWS[] _fulfillmentListField
     {
         get{return fulfillmentListField;}
         set{fulfillmentListField = value;}
     }
}

This is the way i'm assigning values to the variables OrderWS WPObj = new OrderWS(); WPObj.fulfillmentList[1] = new FulfillmentWS(); WPObj.fulfillmentList[1].fulfillingOutletId = "1234";

I've an error at second line, saying Object reference not set to an instance of an object

You must create new instanceo of Array first.

WPObj.fulfillmentList = new FulfillmentWS[1];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM