2013-12-09 61 views
0

對不起我的英文不好。錯誤:無法將類型'字符串'隱式轉換爲字符串[]

我有問題,當談到從我的Windows窗體將值傳遞給類定義我做了。

我宣佈我的類定義7個陣列,企圖存儲來自所述窗口的形式傳遞的值。我的問題是,我經常收到這樣的錯誤

Cannot implicitly convert type 'string' to 'string[]'

我曾試圖讓這是給我一個工作的想法,但它似乎仍然對我的工作引發錯誤。

這裏是我的類定義:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace BankTransaction 
{ 

public class Transactions 
{ 
    String username, password; 
    decimal deposit, withdrawal, balance; 
    decimal percentInterest; 
    private decimal projectedInterest; 
    private String uname; 
    public string fName, lName, mInitial, pword, actType, uName; 
    const Int64 MAX_SIZE = Int64.MaxValue; 

    public static String[] firstName = new String [MAX_SIZE]; 
    public static String[] lastName = new String[MAX_SIZE]; 
    public static String[] mi = new String[MAX_SIZE]; 
    public static String[] pass = new String[MAX_SIZE]; 
    public static String[] userName = new String[MAX_SIZE]; 
    public static String[] accountType = new String[MAX_SIZE]; 
    public static decimal[] deposito = new decimal[MAX_SIZE]; 
    static int count = 0; 
    public static int that; 
    public static int number = 10001; 

    public void registerLoginDetails() 
    { 
     firstName[count] = fName; 
     lastName[count] = lName; 
     mi[count] = mInitial; 
     pass[count] = pword; 
     accountType[count] = actType; 
     userName[count] = uName; 
     deposito[count] = deposit; 
     count++; 
     number++; 
    } 

    public decimal computeProjectedInterest(decimal initialBalance, decimal numOfYears) 
    { 

     if (numOfYears < 1) 
     { 
      percentInterest = 0.00m; 
     } 

     if (numOfYears > 0 && numOfYears <= 3) 
     { 
      percentInterest = 0.02m; 
     } 

     if (numOfYears > 3 && numOfYears <= 5) 
     { 
      percentInterest = 0.05m; 
     } 

     if (numOfYears > 5 && numOfYears <=10) 
     { 
      percentInterest = 0.08m; 
     } 

     if (numOfYears > 10) 
     { 
      percentInterest = 0.12m; 
     } 

     projectedInterest = initialBalance * percentInterest * numOfYears; 
     return projectedInterest; 
    } 

    public decimal withdrawBalance() 
    { 
     return 0m; 
    } 

    public decimal depositBalance() 
    { 
     return 0m; 
    } 
} 
} 

這裏是我的說的WinForm代碼:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using BankTransaction; 

namespace LabExam1 
{ 
public partial class Registration : Form 
{ 

    const long size = Int64.MaxValue; 
    public static Transactions trans = new Transactions(); 
    int i = 0; 

    public void confirmRegistration() 
    { 

     Transactions.firstName = txtFname.Text; 
     Transactions.lastName = txtLname.Text; 
     Transactions.mi = txtMi.Text; 
     Transactions.pass = txtPass.Text; 
     Transactions.deposito = nudDeposit.Value; 
     Transactions.accountType = cboType.Text; 
     Transactions.userName = ""; 
     trans.registerLoginDetails(); 
     i++; 


     this.Hide(); 
     Login lo = new Login(); 
     lo.ShowDialog(); 
    } 

    public Registration() 
    { 
     InitializeComponent(); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     cboType.SelectedIndex = 0; 
    } 

    private void btnRegister_Click(object sender, EventArgs e) 
    { 
     if (txtFname.Text == "" || txtLname.Text == "" || txtMi.Text == "" || txtPass.Text == "") 
     { 
      MessageBox.Show("Please fill up all required fields!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
     } 
     else 
     { 
      if (cboType.SelectedIndex == 0) 
      { 
       confirmRegistration(); 
      } 

      if (cboType.SelectedIndex == 1) 
      { 
       if (nudDeposit.Value < 2500.00m) 
       { 
        MessageBox.Show("The initial deposit for your account type is insufficient", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
       } 

       else 
       { 
        confirmRegistration(); 
       } 
      } 

      if (cboType.SelectedIndex == 2) 
      { 
       if (nudDeposit.Value < 3000.00m) 
       { 
        MessageBox.Show("The initial deposit for your account type is insufficient", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
       } 

       else 
       { 
        confirmRegistration(); 
       } 
      } 
     } 


    } 

    private void btnExit_Click(object sender, EventArgs e) 
    { 
     Application.Exit(); 
    } 

    private void btnGotoLogin_Click(object sender, EventArgs e) 
    { 
     this.Hide(); 
     Login lo = new Login(); 
     lo.ShowDialog(); 
    } 

} 

}

在此先感謝大家會回答我的問題。

+0

那一行????? – 2013-12-09 22:53:14

+2

你會在哪一行發生異常? –

+12

真的7 Int64.MaxValue數組?我認爲你在這裏有一個更大的問題 – Steve

回答

2

當設置靜態對象的公共屬性(一個可怕的設計,順便說一句,但我假設你還在學習......)你試圖設置公共數組屬性而不是單個值屬性:

Transactions.firstName = txtFname.Text; 
    Transactions.lastName = txtLname.Text; 
    Transactions.mi = txtMi.Text; 
    Transactions.pass = txtPass.Text; 
    Transactions.deposito = nudDeposit.Value; 
    Transactions.accountType = cboType.Text; 
    Transactions.userName = ""; 

應該

Transactions.fName = txtFname.Text; 
    Transactions.lName = txtLname.Text; 
    Transactions.mInitial = txtMi.Text; 
    Transactions.pword = txtPass.Text; 
    Transactions.deposit = nudDeposit.Value; 
    Transactions.actType = cboType.Text; 
    Transactions.uName = ""; 

這解決了編譯錯誤,但不解決設計問題。如果這是一個可原諒的學習項目,但有幾個設計問題需要解決(可能在codereview)。

0

看看你的代碼

Transactions.firstName = txtFname.Text; 
Transactions.lastName = txtLname.Text; 
Transactions.mi = txtMi.Text; 
Transactions.pass = txtPass.Text; 
Transactions.deposito = nudDeposit.Value; 
Transactions.accountType = cboType.Text; 

你直接設置一個String數組與字符串變量,儘量使用索引。

Transactions.firstName[index] = txtFname.Text; 
    Transactions.lastName[index] = txtLname.Text; 
... 
相關問題