简体   繁体   English

如何将值赋给类内部的数组类变量

[英]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. 以上两个类位于两个不同的.cs文件中。我正在为OrderWs类创建对象,并尝试将值分配给另一个数组类中的complementingOutletIdField。

Need some logic to assign value to fulfillingOutletIdField variable which is in FulfillmentWS class. 需要一些逻辑来将值分配给FulfillmentWS类中的fulfillingOutletIdField变量。

Change private fields in FulfillmentWS to public properties. 将FulfillmentWS中的私有字段更改为公共属性。

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. 使用public属性与您的private字段进行交互。

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). 当您将fulfillingoutletIdField和其他类定义为私有时,由于保护级别而无法访问它,您可以使用protected或public(有关变量作用域的访问修饰符,请参见下面的链接)。

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) 访问修饰符(C#编程指南)

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

http://www.c-sharpcorner.com/UploadFile/84c85b/default-scope-of-aC-Sharp-class/ 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(); 这就是我为变量OrderWS分配值的方式WPObj = new OrderWS(); WPObj.fulfillmentList[1] = new FulfillmentWS(); WPObj.fulfillmentList [1] =新的FulfillmentWS(); WPObj.fulfillmentList[1].fulfillingOutletId = "1234"; 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. 您必须首先创建Array的新实例。

WPObj.fulfillmentList = new FulfillmentWS[1];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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