在将Windows窗体中的值传递给我所做的类定义时,我遇到了问题。
我已经在我的类定义中声明了七个数组,试图存储从所述窗口表单传递的值。我的问题是我经常遇到像
这样的错误 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 :(得分:2)
设置静态Transactions
对象的公共属性时(一个可怕的设计,BTW,但我假设你还在学习......)你试图设置公共数组属性而不是个体价值属性:
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上)。
答案 1 :(得分: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变量直接设置String数组,请尝试使用索引。
Transactions.firstName[index] = txtFname.Text;
Transactions.lastName[index] = txtLname.Text;
...