[RESOLVED] Need Help on this Bank Program!
Hi! I am a UG student of Computer Engineering and It would be nice if someone could help me on this C# project that i want to create just for educational purpose. I have a average C and C++ programming knowledge. But I am new to the C# or .Net for that matter.
I have rough or pitty good idea on the concept behind this program because I have done program in C++ that does same thin except the fact that it runs on console or DOS. But I am finding hard on C# and I am stuck on the very first step that is creating a account and assigning it to the array of object.
Till now there are 2 window form on my Program. First with 'create new Account Button' and when user clicks on that button another form shows up with textbox for inputing NAME, Password and intial Amount.
The Code Posted above Compiles fine on VS2008 Professional Edition . But When i Insert Information and click on I accept it always comes with warning that says.. "Object reference not set to an instance of an object."
I would be very thankful if someone could solve my problem. My email is [email protected]
I have attached my project jus below
Re: Need Help on this Bank Program!
I get a different error. can you paste the code part where you get the error?
btw. this is a bad habit:
Code:
string x, y; int z;
x = Nam.Text; y = Pass.Text; z = Convert.ToInt32(Amount.Text);
easier to debug and to understand is one statement pro line:
Code:
string x;
string y;
int z;
x = Nam.Text;
y = Pass.Text;
z = Convert.ToInt32(Amount.Text);
Re: Need Help on this Bank Program!
Thank You for your Kind suggestion. I knew the convention but I was too careless and I will try to rectify it. As u said it gave another type of error please specify it.
When I run the program and click on "Create Account" it opens the form2. But when I fill up the form and click I accept my program suddenly comes out and highlights on the statement
Re: Need Help on this Bank Program!
Quote:
Originally Posted by
rocky_upadhaya
The Code Posted above Compiles fine on VS2008 Professional Edition . But When i Insert Information and click on I accept it always comes with warning that says.. "Object reference not set to an instance of an object."
The problem is in the last line.
Code:
Bank[] b1 = new Bank[100];
b1[1].create(x,y,z);
The Bank object in array b[1] is not initialized and thus null. Initialze first
Code:
Bank[] b1 = new Bank[100];
b1[1] = new Bank();
b1[1].create(x, y, z);
But, it would be much better to use an List<T> here instead of an array. Now, you create an array of length 100. Disadvantages:
- You need to check where you need to save an new account (at what index).
- If only 2 accounts are made, why need an array of length 100
- What happens if there are more than 100 accounts?
Change it something like
Code:
List<Bank> b1 = new List<Bank>();
Bank b = new Bank();
b.create(x,y,z);
b1.Add(b);
Also, you now have an 'create' method in de Bank class. Replace this method by using the constructor
Code:
public class Bank {
string name, pass;
Int64 bal; int accid;
public Bank(string a, string b, int c) {
name = a; pass = b; bal = c; accid = global.getr + 1000;
global.getr = global.getr + 1;
}
}
Creating a new Bank looks like the next
Code:
Bank b = new Bank(x,y,z);
Re: Need Help on this Bank Program!
oops :D I typed "asdf" :p in the amount text box so the program crashed at
Code:
z = Convert.ToInt32(Amount.Text);
Re: Need Help on this Bank Program!
Thank You dannystommen and memeloo. Both of Your help is really appreciated and was really helpful. As You have noticed that my Program is far from Over and I am really new to this language so I will really look forward for further Help. As for now now I am learning to add the account detail feature on this program and I am in dilemma about how to retrive the information from object b. I want to use Object index as account number with small changes (example ....accountid=index+1000;) and show the detail if user inputs account id so that bb[index].view();
Re: Need Help on this Bank Program!
Here, because I was bored.
Code:
public static class Bank {
private static List<Account> AccountList = new List<Account>();
private static int account_id = 1;
public static void CreateAccount(string name, int amount) {
Account a = new Account(account_id, name, amount);
account_id++; //or create some other method to create an unique id
AccountList.Add(a);
}
public static Account GetAccount(int id) {
//use Linq to select, will return null if not found
return (from a in AccountList
where a.Account_ID == id
select a).FirstOrDefault();
}
}
public class Account {
public int Account_ID { get; set; }
public string Name { get; set; }
public int Amount { get; set; }
public Account(int id, string name, int amount) {
Account_ID = id;
Name = name;
Amount = amount;
}
}
Now, from any form, you can call CreateAccount or GetAccount
Re: [RESOLVED] Need Help on this Bank Program!
dear dannystommen! thanks for your support . I was not able to work on this program lately dew to this new year. But now that I have used your code I cannot Call CreateAccount method from another form. I know thar it is completely illogical problem but my compiler says CreateAccount function does not exist in the current context. I pearsonally am very buzzed after seeing this problem. I may also be too noob to be facing this problem.
Code:
string a1, b1; int c1;
a1 = Nam.Text;
b1 = Pass.Text;
c1 = Convert.ToInt32(Amnt.Text);
CreateAccount(a1, b1, c1);
MessageBox.Show("CREATED");
this.Close();
and BTW my IntilliSence of VS is also not giving GetAccount in its list so it may also have same problem coz CreateAccount method was also not listed on that list.
Re: [RESOLVED] Need Help on this Bank Program!
hehe! Like I said I was too noob to be getting This Problem.
The answer to this Problem is
Bank.CreateAccount(a1,b1,c1);
;) :) ;) :) ;) :) ;) :)
Re: [RESOLVED] Need Help on this Bank Program!
Hello! Can anyone please Help me Showing detail of an object . The Linq method of dannystommen is working perfect but I cant figure out the mechanism. I have done some studying on that but still no result.
Code:
int id1;
id1 = Convert.ToInt32(id_ask.Text);
richTextBox1.Text = Bank.GetAccount(id1).ToString();
I dont think this code is right can anyone help me to rectify it??
Re: [RESOLVED] Need Help on this Bank Program!
what you wanted to do, was probably to use one of the Account's properties:
Code:
public int Account_ID { get; set; }
public string Name { get; set; }
public int Amount { get; set; }
Code:
richTextBox1.Text = Bank.GetAccount(id1).Account_ID.ToString();
or
Code:
richTextBox1.Text = Bank.GetAccount(id1).Name;
or
Code:
richTextBox1.Text = Bank.GetAccount(id1).Amount.ToString();
or you wanted to concatenate all three values or you'd better write a helper method that does this internally.
Re: [RESOLVED] Need Help on this Bank Program!
Please Now can Any one Help me figure out the method to delete the object if id passed is matched...line in this example...
Code:
public static Account1 DeleteAccount(int id, string pass)
{
return (from a in AccountList where a.Account_ID == id && a.Pass == pass select a).FirstOrDefault();
}
I want to delete only the object whose id and password is matched.
BTW I am also having some problem working with forms. I have 1 main form which loads at first. and when I Click on the Open Account Button It Opens another form. But I want to hide The main form if I open the child form. I can use this.Hide(); but what it does is it only hides the main form and if I close the child form the Program does not ends because main form is only hidden.
Secondly I kept on my child form with back button which must lead me to the initial form. But here is the problem Instead of taking me back to initial form it opens new initial form so that there are 2 main form opened at same time.
I have some knowledge of Java and it can be solved my changing modal and non-modal settings. So Suggest me a solution to all these solution.
Re: [RESOLVED] Need Help on this Bank Program!
Quote:
Originally Posted by
rocky_upadhaya
Code:
public static Account1 DeleteAccount(int id, string pass)
{
return (from a in AccountList where a.Account_ID == id && a.Pass == pass select a).FirstOrDefault();
}
what's the type of the AccountList? (you are not deleting here anything, only selecting)
as fas as the second problem is concerned some code would be helpful.
Re: [RESOLVED] Need Help on this Bank Program!
AccountList is of Type 'Account' class like in code private static List<Account> AccountList = new List<Account>();
You can also take refrence Of reply no 7 of dannystommen above. DeleteAccount is a function of Account1 type that I later creater.
As you have also asked for the refrence of second problem. Please Download and run Bank.zip There is a main form with buttons. If user clicks on Open Account Button new form opens. What I want to do is open that new form on top of existing form.
Re: [RESOLVED] Need Help on this Bank Program!
Quote:
Originally Posted by
rocky_upadhaya
You can also take refrence Of reply no 7 of dannystommen above. DeleteAccount is a function of Account1 type that I later creater.
ok, I was not sure if you actully use this code.
the first problem can be easly solved:
Code:
public static bool DeleteAccount(int id, string pass)
{
var accountToDelete =
from a in AccountList
where a.Account_ID == id && a.Pass == pass
select a;
return AccountList.Remove(accountToDelete);
}
I didn't test it, but is should work and you shoudn't write LINQ queries in one line. it's hard to understand and to find any bugs.