I am a beginner in C#.NET and we were tasked to create an online banking system in which transactions and login data are stored in an array of class. Although I am slowly getting a grasp of the whole array-of-class concept, it seems that I am stuck in saving the login data to an array of class. Here is my program:
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
{
Transactions trans = new Transactions();
int number = 10000;
int x = 0;
const int size = 100;
Transactions[] loginData = new Transactions[size];
public void saveLoginData()
{
loginData[x].Username = trans.createUserName(txtFname.Text, txtLname.Text, txtMi.Text);
loginData[x].Password = txtPass.Text;
x++;
}
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)
{
number++;
loginData[x] = new Transactions();
saveLoginData();
MessageBox.Show("Generated Username: " + loginData[x].Username + number + "\n" + "Please do not share this Information to anyone!");
this.Hide();
Transaction tr = new Transaction();
tr.ShowDialog();
}
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
{
number++;
loginData[x] = new Transactions();
saveLoginData();
MessageBox.Show("Generated Username: " + loginData[x].Username + number + "\n" + "Please do not share this Information to anyone!");
this.Hide();
Transaction tr = new Transaction();
tr.ShowDialog();
}
}
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
{
number++;
loginData[x] = new Transactions();
saveLoginData();
MessageBox.Show("Generated Username: " + loginData[x].Username + number + "\n" + "Please do not share this Information to anyone!");
this.Hide();
Transaction tr = new Transaction();
tr.ShowDialog();
}
}
}
}
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
I am getting an error on this part which throws the nullReferenceException:
MessageBox.Show("Generated Username: " + loginData[x].Username + number + "\n" + "Please do not share this Information to anyone!");
Here is my class definition:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BankTransaction
{
public class Transactions
{
String username, password;
#region forArrayOfClassLogin
public String Password
{
get { return password; }
set { password = value; }
}
public String Username
{
get { return username; }
set { username = value; }
}
#endregion
public String createUserName(String fname, String lname, String mi)
{
String firstString = fname[0].ToString();
String secondString = mi[0].ToString();
String thirdString = lname[0].ToString();
String uname = firstString + secondString + thirdString;
return uname;
}
public void setBalance(String type, decimal initialBalance)
{
}
public String getUserName()
{
return username;
}
public String setPassword(String pass)
{
return password = pass;
}
public String getPassword()
{
return password;
}
}
}
Any help would be greatly appreciated. Thanks in advance.