-1

在不同的类中声明数组变量时如何为其赋值?以下是更容易理解我的问题的示例代码:-

// Below is a class customer that has three parameters: 
// One string parameter and Two int array parameter

public class Customer
{
    public string invoiceFormat { get; set; }
    public int [] invoiceNumber { get; set; }
    public int [] customerPointer { get; set; }

    public Customer(
        string invoiceFormat, 
        int[] invoiceNumber, 
        int[] customerPointer) 
    {
        this.invoiceFormat = invoiceFormat;
        this.invoiceNumber = invoiceNumber;
        this.customerPointer = customerPointer;
    }
}

// How to assign value for invoiceNumber or customerPointer array in 
// different windows form?
// The following codes is executed in windowsform 1

public static int iValue=0;
public static Customer []c = new Customer [9999];

c[iValue] = new Customer(textBox16.Text, invoiceNumber[0].iValue + 1, 
                         customerPointer[0].iValue);

// I have an error that the name 'invoiceNumber and customerPointer' 
// does not exist inthe current context
4

2 回答 2

1

你有什么

c[iValue] = new Customer(textBox16.Text, invoiceNumber[0].iValue + 1, customerPointer[0].iValue);

这是完全错误的,这就是您收到错误的原因:当前上下文中不存在名称“invoiceNumber and customerPointer”

您永远不会为 invoiceNumber 或 CustomerPointers 声明任何数组。这两个都是您班级的成员,我认为您对此感到困惑。我什至不打算猜测 invoiceNumber[0].iValue +1 是什么,因为 int 没有成员,它是一种数据类型

所以要解决这个问题,我们会做一些事情,比如

        //create some arrays
        int[] invoicesNums = new int[]{1,2,3,4,5};
        int[] customerPtrs = new int[]{1,2,3,4,5};
        //create a new customer
        Customer customer = new Customer("some invoice format", invoicesNums, customerPtrs);

        //add the customer to the first element in the static array
        Form1.c[0] = customer;

好的,这就是你应该怎么做的,但是,我真的认为你需要停下来更深入地研究类、数组、数据类型和 OOP,因为当你在你的程序中走得更远时,这将使你免于头疼.

于 2013-10-15T04:06:37.567 回答
0

您正在尝试使用尚不存在的值。看看你的构造函数:

public Customer(string invoiceFormat, int[] invoiceNumber, int[] customerPointer) 
{
            this.invoiceFormat = invoiceFormat;
            this.invoiceNumber = invoiceNumber;
            this.customerPointer = customerPointer;
 }

这意味着您需要传递一个字符串、一个 FULL 数组 int和另一个 COMPLETE 数组int。通过尝试这样做:

c[iValue] = new Customer(textBox16.Text, invoiceNumber[0].iValue + 1, customerPointer[0].iValue);

您正在调用尚不存在的变量。想想构造函数这个词。这将创建对象并初始化内部变量。 invoiceNumber[]并且customerPointer[]永远不会通过传递另一个数组参数来分配。这就是您收到该错误消息的原因。如果您使用构造函数初始化这些数组,然后传递 singleinvoiceNumber和 single customerPointer,然后将它们添加到初始化的数组中,那么这将起作用。但是,听起来您的内部值不应该是数组,那么您就可int以为每个参数传递一个值。

于 2013-10-14T23:18:24.667 回答