OOP newbie

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • east7
    New Member
    • Jul 2009
    • 7

    OOP newbie

    Hi All,

    I'm using C# 2.0. I want to produce code like this:
    Code:
     int cnt = Products("Car").Color("Red").Count();

    to get total cars that has color red.

    How do i design my classes.
    I know i can do it using ADO.NET classes, but i want to know how to do it purely in object oriented style.

    Many thanks.
    e7
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Are you trying to get the number of cars that match this as a query to a database? If you had a database of cars, how many are a 'car' (not 'truck') and are 'red' (instead of 'blue')

    Database How-to parts 1 and 2
    Database tutorial Part 1
    Database tutorial Part 2

    Comment

    • Curtis Rutland
      Recognized Expert Specialist
      • Apr 2008
      • 3264

      #3
      If you were using C# 3.0, there would be a very easy way to do this with LINQ, but in 2.0 there is nothing to do exactly what you're suggesting here.

      Assuming we're not talking about database stuff here, the OOP way of doing this is to create classes with the proper Properties and Methods to do what you want.

      Here's a sample program of what you want to do. I think it should work in C# 2.0. This requires adding a reference to System.Drawing.

      Code:
      using System;
      using System.Collections.Generic;
      using System.Drawing;
      
      namespace TermsHelper
      {
          class Program
          {
              static void Main(string[] args)
              {
                  ProductCollection products = new ProductCollection();
                  products.ProductList.Add(new Product() { ProductColor = Color.Red, ProductType = "Car" });
                  products.ProductList.Add(new Product() { ProductColor = Color.Red, ProductType = "Car" });
                  products.ProductList.Add(new Product() { ProductColor = Color.Blue, ProductType = "Car" });
                  products.ProductList.Add(new Product() { ProductColor = Color.Green, ProductType = "Truck" });
      
                  int res = products.SearchByType("Car").SearchByColor(Color.Red).ProductList.Count;
      
                  Console.Read();
              }
          }
      
          public class ProductCollection
          {
              private List<Product> _ProductList;
              public List<Product> ProductList { get { return _ProductList; } set { _ProductList = value; } }
      
              public ProductCollection() { this.ProductList = new List<Product>(); }
      
              public ProductCollection SearchByType(string type)
              {
                  List<Product> result = this.ProductList.FindAll(delegate(Product p)
                  {
                      return p.ProductType.Equals(type);
                  });
      
                  ProductCollection c = new ProductCollection() { ProductList = result };
                  return c;
              }
      
              public ProductCollection SearchByColor(Color color)
              {
                  List<Product> result = this.ProductList.FindAll(delegate(Product p)
                  {
                      return p.ProductColor.Equals(color);
                  });
                  ProductCollection c = new ProductCollection() { ProductList = result };
                  return c;
              }
          }
      
          public class Product
          {
              private string _ProductType;
              public string ProductType { get { return _ProductType; } set { _ProductType = value; } }
              private Color _ProductColor;
              public Color ProductColor { get { return _ProductColor; } set { _ProductColor = value; } }
          }
      }

      Comment

      • Curtis Rutland
        Recognized Expert Specialist
        • Apr 2008
        • 3264

        #4
        This, by the way, is how much easier and simpler it is if you can work with C# 3.0:

        Code:
        using System;
        using System.Linq;
        using System.Collections.Generic;
        using System.Drawing;
        
        namespace TermsHelper
        {
            class Program
            {
                static void Main(string[] args)
                {
                    List<Product> products = new List<Product>();
                    products.Add(new Product() { ProductColor = Color.Red, ProductType = "Car" });
                    products.Add(new Product() { ProductColor = Color.Red, ProductType = "Car" });
                    products.Add(new Product() { ProductColor = Color.Blue, ProductType = "Car" });
                    products.Add(new Product() { ProductColor = Color.Green, ProductType = "Truck" });
        
                    int res = products.Where(x => x.ProductColor == Color.Red && x.ProductType == "Car").Count();
        
                    Console.Read();
                }
            }
        
            public class Product
            {
                public string ProductType { get; set; }
                public Color ProductColor { get; set; }
            }
        }

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          Ok. That's cool. I love being a part of this site. I wish I had more time to keep up on the newer technologies coming out. This is something I'll *make* time for. I can see where it will come in handy on a project I have coming up.

          Is there an equally easy way (in 3.0) to actually get back the matching objects instead of just the count?

          Comment

          • Curtis Rutland
            Recognized Expert Specialist
            • Apr 2008
            • 3264

            #6
            Code:
            List<Product> res = products.Where(x => x.ProductType == "Car" && x.ProductColor == Color.Red).ToList();
            LINQ is amazingly cool, and incredibly useful, for DB stuff, XML stuff, and collections already in memory.

            Just another example off the top of my head, how about if you want a list of all the distinct types of products that happen to be red?
            Code:
            List<string> types = products.Where(x => x.ProductColor == Color.Red).Select(x => x.ProductType).Distinct().ToList();
            The lambda ( => ) is a shortcut for anonymous methods. To expand on that, these two snippets are exactly the same. The second is a very useful shorthand.
            Code:
            List<Product> result = this.ProductList.FindAll(delegate(Product p)
            {
                return p.ProductType.Equals(type);
            });
            
            List<Product> result = this.ProductList.FindAll(x => x.ProductType.Equals(type));

            Comment

            • tlhintoq
              Recognized Expert Specialist
              • Mar 2008
              • 3532

              #7
              That really is cool. I need to spend more time reading up on such things. I spend all my day coding. The end result being that I do things the way I've always done them. I had no idea that I could basically query a List<> as if it were a DB. This has great potential for the next application that my employer wants, but he is lothe to use databases because he doesn't want someone else's application in the middle of our workflow. He's old-school assembly language coder.

              Comment

              • Curtis Rutland
                Recognized Expert Specialist
                • Apr 2008
                • 3264

                #8
                Yeah, when I learned it, I started asking myself "why oh why didn't I learn this sooner! It makes my coding life sooooooo much easier!"

                LINQ stands for "Language INtegrated Query" and can be used for a lot of things. Anything that implements IEnumerable, for example.

                But I think we've hijacked this thread far enough. I'd be happy to go on in another thread, if you want to know more about it. There's so many cool features.

                Comment

                • tlhintoq
                  Recognized Expert Specialist
                  • Mar 2008
                  • 3532

                  #9
                  Rather than a discussion thread, had you considered an 'insights' article? Those two examples: Getting the count of matching objects and getting the matching objects themselves would probably get 90% of the newbies like me and the O.P. off to a great start.

                  PS: I wasn't trying to hijack the thread, but rather expand in a direction that I figured the O.P. and others would naturally go to next. I apologize if it seemed I did what I complain so often about.

                  Comment

                  • Curtis Rutland
                    Recognized Expert Specialist
                    • Apr 2008
                    • 3264

                    #10
                    Definitely considered it, probably will use this thread, and this other thread as the basis, once I get a little bit of spare time.

                    Comment

                    • east7
                      New Member
                      • Jul 2009
                      • 7

                      #11
                      insertAlias,

                      thanks to you, figured it out now.

                      e7.

                      Comment

                      Working...