0
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.

5 Answers 5

1

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; };
}
Sign up to request clarification or add additional context in comments.

1 Comment

I have defined the properties as well.. private Customer fulfillingCustomerField; public Customer FulFillingCustomer { get { return fulfillingCustomerField; } set { fulfillingCustomerField = value; } } private string fulfillingOutletIdField; public string FulFillingOutletId { get { return fulfillingOutletIdField; } set { fulfillingOutletIdField = value; } }
0

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-a-C-Sharp-class/

Comments

0

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;}
     }
}

Comments

0

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];

6 Comments

Thanks Adam. It worked !!!..Below is the code that I tried after your suggestion. WPObj.fulfillmentList = new FulfillmentWS[1]; WPObj.fulfillmentList[0].fulfillingOutletId = "1234"; Now i have one more error saying "Index Array out of bounds". What I need to specify here ?
Paste all your code becouse i don't see any problem here
not: WPObj.fulfillmentList[1].fulfillingOutletId = "1234"; but: WPObj.fulfillmentList[0].fulfillingOutletId = "1234"; We are counting from zero
But if I add [0] as index, i'm having "Object not reference... " error
Becouse you must first generate new object. OrderWS WPObj = new OrderWS(); WPObj.fulfillmentList = new FulfillmentWS[1]; WPObj.fulfillmentList[0] = new FulfillmentWS(); WPObj.fulfillmentList[0].fulfillingOutletId = "1234";
|
0

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";

5 Comments

Thanks For all your resonses.. But i have defined the get;set properties in my class as below private Customer fulfillingCustomerField; public Customer FulFillingCustomer { get { return fulfillingCustomerField; } set { fulfillingCustomerField = value; } } private string fulfillingOutletIdField; public string FulFillingOutletId { get { return fulfillingOutletIdField; } set { fulfillingOutletIdField = value; } }
I'm able to access. Im able to assign value to that variable as well. During runtime i've an error that i've mentioned above
you never mentioned any error. you just asked for logic.
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
First of All put this code in your question. and the error you are getting have nothing to do with your properties implementation. you are assigning a value to index 1 of array. but you haven't defined the array before accessing its index. I have updated my answer. check it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.