dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: Problem with a list printing its elements

  1. #1
    Join Date
    Oct 2010
    Posts
    14

    Problem with a list printing its elements

    Code:
    List<Bank> transactions = new List<Bank>();
                            transactions.Add(new Bank(dollarFinal, userName, tType, tCat));
                            Console.WriteLine("Your transaction information is: " + transactions);
    I have this code here in part of my program. I am needing it to print the elements in the list but when I do this it just prints a line with the name of the List but not what is actually in the list.

    Bank is an object I got made to store items that the user inputs.

  2. #2
    Join Date
    Oct 2010
    Posts
    12

    Re: Problem with a list printing its elements

    are you perhaps looking for something like?

    Bank_Obj transactions = new Bank_Obj();
    foreach (Bank_Obj i in Bank_Obj.list)
    {
    Console.WriteLine("Transaction Data: " + i.ac);
    }


    It's assuming you have a static public list in the data object declared but its just an example.

    Hope it helps.

  3. #3
    Join Date
    Oct 2010
    Posts
    14

    Re: Problem with a list printing its elements

    I was trying something like that.

    I had

    Code:
    foreach (Object Bank in transactions)
                            {
                                Console.WriteLine(transactions);
                            }
    But that did not work either. Does the same thing.

    This is what the output shows

    "System.Collections.Generic.List '[ProjectName.Bank]"

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Problem with a list printing its elements

    Quote Originally Posted by hiddenjob View Post
    I was trying something like that.

    I had

    Code:
    foreach (Object Bank in transactions)
                            {
                                Console.WriteLine(transactions);
                            }
    But that did not work either. Does the same thing.

    This is what the output shows

    "System.Collections.Generic.List '[ProjectName.Bank]"
    That is hardly the same thing as the example above. There is a difference between calling List.ToString() and List[index].ToString().

  5. #5
    Join Date
    Oct 2010
    Posts
    14

    Re: Problem with a list printing its elements

    Sorry my bad.

    Just tried the following. It works a little better but still is not printing the right thing.

    Code:
     for (int i = 0; i < transactions.Count; i++)
                            {
                                Console.Write("{0} ", transactions[i].ToString());
                            }
    
                            Console.WriteLine("\n");
                            Console.WriteLine("transactions.Capacity: {0}",
                               transactions.Capacity);

  6. #6
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Problem with a list printing its elements

    Not sure why you care about Capacity, and you have yet to define what is "the right thing".

  7. #7
    Join Date
    Oct 2010
    Posts
    14

    Re: Problem with a list printing its elements

    The right thing is what the values of the variables are that are stored in the Bank class.

    I have a Bank object storing info like

    username, amount, transaction type, category

    Those 4 are part of the Bank object

    Code:
    List<Bank> transactions = new List<Bank>();
                            transactions.Add(new Bank(dollarFinal, userName, tType, tCat));
    When I print the transactions List i need it to print those 4 variables that are saved in the Bank.

  8. #8
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Problem with a list printing its elements

    Well, the it's the same problem. You are calling Bank.ToString(), not Bank.Username, Bank.Amount, etc. It is doing what you tell it to do, think about it for a bit.

  9. #9
    Join Date
    Oct 2010
    Posts
    14

    Re: Problem with a list printing its elements

    Quote Originally Posted by BigEd781 View Post
    Well, the it's the same problem. You are calling Bank.ToString(), not Bank.Username, Bank.Amount, etc. It is doing what you tell it to do, think about it for a bit.
    I am not quite sure what you are saying.

    I get the not calling.ToString()

    but how can I call the Bank objects while they are stored in a List called transactions

    I cannot simply just print the List with console.writeline

    and I cannot do anything like transactions.username

    Been thinking about it and its going over my head it seems.

  10. #10
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Problem with a list printing its elements

    Yes, you are calling ToString(), you are just calling it implicitly. It helps to know what is actually going on in your code. Once again, this stuff isn't magic. Calling List<Bank>.ToString() is different than Bank.ToString() and is again different than int.ToString().

  11. #11
    Join Date
    Oct 2010
    Posts
    14

    Re: Problem with a list printing its elements

    I got that. I removed all tostring references in all forms.

    So what do you suggest I do to print the bank variables while stored in a list.

    And i am new to c#. This is my very first program that i am writing in this and the first time ive programmed in any form in almost 2 years so what you find to be very obvious may not be to me.

    But i do highly appreciate your help and for taking the time to post here

  12. #12
    Join Date
    Oct 2010
    Posts
    12

    Re: Problem with a list printing its elements

    Apologies it took so long to reply, I have an irregular sleep pattern

    Lets take this line by line.

    Presumptions:

    You have a class declared "Bank" which is just a glorified list of the variables that the bank object contains. In that same Bank Object is declared a static public list, something like:

    Code:
    public static List<Bank_Data> list = new List<Bank_Data>();
    
    static public List<Bank_Data> AddClientData(string facturaid, string fecha, string client_name, string concept, string total, string observation, bool isclient)
    {
      list.Add(new Bank_Data(facturaid, fecha, client_name, concept, total, observation, isclient));
      return list;
    }
    
    //ignore the variable names, they are just mock names for this example. I don't mind helping point the way but I don't spoon feed.
    Then where ever you want to call it you would do something like this.

    Code:
    Bank_Obj transactions = new Bank_Obj(); // you can do this if you want to open a blankobject of 
    //the type and populate it, useful if your planning on doing comparisons or want to store it for 
    //something. Ive put this in as an example.
    
    foreach (Bank_Obj i in Bank_Obj.list)
    {
      Console.WriteLine("Transaction Data: " + i.total); 
    }
    What you are in effect doing here is I think confusing what a "foreach" actually does. You are declaring in effect in the above statement that "i" which is just a made up variable on the fly is of the type Bank_Obj and it needs to get this object from the Bank_Obj.list.

    So basically you are telling it that you want to check for a certain type and where that type can be found. then it will iterate through that list for you. you will find that within the scope that the "i" will have the attributes of your Bank_Obj class.

    I'm sure I am not explaining this terribly well, so check on google for a tutorial if that is the case.

    I hope this helps.
    Last edited by kabben; October 12th, 2010 at 05:33 AM. Reason: forgot some code tags

  13. #13
    Join Date
    Oct 2010
    Posts
    14

    Re: Problem with a list printing its elements

    I actually got it to work late last night. I am an in incredibly slow programmer lol.

    But I redid my Bank class to have get and set methods and then i have a print method as well that goes through and prints the different methods in the bank object to do what i wanted.

    I will post my code later this week after i have added the finishing touches and turned it in.

    Thanks for your help. Leaving feedback

  14. #14
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Problem with a list printing its elements

    You don't need a Print method, just override ToString. That's what it is there for.

  15. #15
    Join Date
    Oct 2010
    Posts
    14

    Re: Problem with a list printing its elements

    Well i ended up doing this basically.

    Code:
    public void Print()
        {
            Console.WriteLine("Bank Information");
            Console.WriteLine("********************");
            foreach (Bank Bank in this)
            {
                if (Bank is Bank)
                {
                    Console.WriteLine(Bank.Name + "\t\t" + Bank.Value + "\t\t" + Bank.Type + "\t\t" + Bank.Category + "\t\t" + Bank.Date);
                }
            }
        }

Page 1 of 2 12 LastLast

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)