CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2007
    Posts
    71

    how can I call value static field or method list type class

    If I have class with static field:
    Code:
    public class Cart
    {
            private static int quantity_all_books = 0; 
    
            public int id { get; private set; }
            public int quantity { get; private set; }
    
            public Cart(int x, int y)
            {
                quantity_all_books++;
    
                id = x;
                quantity = y;
            }
    }
    And I have list type of this class:
    Code:
    List<Cart> shopping = new List<Cart>();
    shopping.Add(new Cart(53, 2));
    shopping.Add(new Cart(44, 1));
    How can I check value static field quantity_all_books. I have tried make this field public, write method which return value of this field. But when I write "shopping." Visual Studio don't give me on the list this field or method.
    Last edited by abab; July 6th, 2009 at 03:44 AM.

  2. #2
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: how can I call method class list

    Have a static property e.g.

    Code:
    public class Cart
    {
            private static int quantity_all_books = 0; 
    
            public static int QuantityAllBooks
            {
                    get { return quantity_all_books; }
            }
    }
    You gain access to static methods not by specifying them on an instance, but this way :

    Code:
    int quantityAllBooks = Cart.QuantityAllBooks;
    Darwen.
    Last edited by darwen; July 6th, 2009 at 04:31 AM.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  3. #3
    Join Date
    Mar 2007
    Posts
    71

    Re: how can I call method class list

    thx very much

  4. #4
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: how can I call method class list

    Quote Originally Posted by abab View Post
    thx very much
    Excuse my question, but is there a specific reason to have this field quantity_all_books static ? Whats the reason behind that ?
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  5. #5
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: how can I call method class list

    Yes, it strange. I think that better approach would be to read Count property of a collection where instances of Cart are stored, because without a storage, it doesn't make sence to count them.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  6. #6
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: how can I call value static field or method list type class

    Code:
    public class Cart
    {
            private static int quantity_all_books = 0; // unnecessay here
           .....
    }
    What do you want to count ? Books ? Ok what do they have to do with Cart ? You are collecting Cart in a list so you have a Count property all the times So why not simple
    Code:
    List<Cart> shopping = new List<Cart>();
    shopping.Add(new Cart(53, 2));
    shopping.Add(new Cart(44, 1));
    // now counting
    int amount = shopping.Count;
    It's totally unusual design to have the amount of Chart classes as a propety of that Cart class itself.

    I would create a Carts class which contains all books. My rule is : Classes which ends with plural 's ' are collections of classes which have the same but singular name. Product, Products, Book, Books...
    This way
    Code:
    public class Carts{
        private  List<Cart> shopping;
      // Constuctor
      public Carts(){
          shopping = new List<Cart>();
      }
      // Properties
       public int Count{
          get { return shopping.Count;}
      }
      // methods
      public void Add(Cart cart){
          shopping.Add(cart);
      }
    }
    Overthing your design.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

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