2013-10-14 43 views
-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 
+0

这是应该完成的:“invoiceNumber [0] .iValue + 1”? –

+1

传入数组时,您传递的是数组的元素而不是数组本身。 invoiceNumber [0]表示数组的第一个元素。 invoiceNumber是数组 – Sorceri

+0

System Down:这是计费系统。班级客户存储一系列发票号码。所以,在我的窗体中,当我加载帐单表单时。帐单表单访问客户类并将新的invoicenumber存储到数组中。所以我需要知道代码如何传递值数组invoiceNumber。 –

回答

0

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

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

这意味着你需要传递一个字符串的int一个全阵列和int另一个完整阵列。通过尝试执行此操作:

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

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

+0

这是我的情况。这是一个计费系统,在我的帐单上,当我添加新发票时。我必须将发票号码存储到已更新发票号码的现有客户。如果我声明正常的int类型,而不是数组。没有错误,它的工作完美。但是,我的情况是,客户必须有发票数量。请,你能给我示例代码如何解决我的问题。谢谢 –

+1

看来您需要了解如何正确使用类。您需要添加方法来添加其他发票号码和“customerpointer”值。您不断修建新客户,而不是修改现有客户。 – JNYRanger

+0

http://msdn.microsoft.com/en-us/library/vstudio/x9afc042.aspx – JNYRanger

1

你有什么

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

这是完全错误的,这就是为什么你的错误:名称“invoiceNumber和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; 

好吧,所以,你应该怎么做,但是,我真的认为你需要停下来深入研究类,数组,数据类型和面向对象,因为这将使你从一个主要头痛,当你进一步深入与您的程序路。