CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    Join Date
    Dec 2009
    Location
    Kathmandu,Nepal
    Posts
    168

    [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 rocky_upadhaya@hotmail.com


    I have attached my project jus below
    Last edited by rocky_upadhaya; January 20th, 2010 at 12:40 PM. Reason: A Hugely Updated Bank Program!!

  2. #2
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    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);
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  3. #3
    Join Date
    Dec 2009
    Location
    Kathmandu,Nepal
    Posts
    168

    Thumbs up 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
    Last edited by rocky_upadhaya; January 6th, 2010 at 07:27 AM.

  4. #4
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: Need Help on this Bank Program!

    Quote Originally Posted by rocky_upadhaya View Post
    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);

  5. #5
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Need Help on this Bank Program!

    oops I typed "asdf" in the amount text box so the program crashed at
    Code:
    z = Convert.ToInt32(Amount.Text);
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  6. #6
    Join Date
    Dec 2009
    Location
    Kathmandu,Nepal
    Posts
    168

    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();
    Last edited by rocky_upadhaya; December 30th, 2009 at 05:52 AM.

  7. #7
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    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

  8. #8
    Join Date
    Dec 2009
    Location
    Kathmandu,Nepal
    Posts
    168

    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.
    Last edited by rocky_upadhaya; January 2nd, 2010 at 12:10 PM.

  9. #9
    Join Date
    Dec 2009
    Location
    Kathmandu,Nepal
    Posts
    168

    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);

  10. #10
    Join Date
    Dec 2009
    Location
    Kathmandu,Nepal
    Posts
    168

    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??

  11. #11
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    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.
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  12. #12
    Join Date
    Dec 2009
    Location
    Kathmandu,Nepal
    Posts
    168

    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.

  13. #13
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: [RESOLVED] Need Help on this Bank Program!

    Quote Originally Posted by rocky_upadhaya View Post
    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.
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  14. #14
    Join Date
    Dec 2009
    Location
    Kathmandu,Nepal
    Posts
    168

    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.

  15. #15
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: [RESOLVED] Need Help on this Bank Program!

    Quote Originally Posted by rocky_upadhaya View Post
    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.
    Last edited by memeloo; January 4th, 2010 at 12:48 PM.
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured