CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2011
    Posts
    3

    need some help in creating classes

    I'm new to OOP
    what I'm trying to do is use the naive bayesian alogrithm on the iris database
    each flower has 5 attributes:
    sepal length, width, petal length & width and the flowertype
    here's my class code so far:
    public class flower
    {
    double slength;
    double swidth;
    double plength;
    double pwidth;
    string ftype;
    }

    i understand that if i declare a flower of a certain type i can access its attribute using x.slength=1.2;
    what im trying to do is create an Array of flowers so i did the following:
    flower[] list = new flower[90];
    how can i access the 31st flowers petal length???
    thanks in advance

  2. #2
    Join Date
    Dec 2011
    Posts
    3

    Re: need some help in creating classes

    apparently the problem was declaring the attributes as public variables
    after ive done that i still cant access them and c# throws a nullvalueexception or something like that

  3. #3
    Join Date
    Jun 2011
    Location
    .NET4.0 / VS 2010
    Posts
    70

    Re: need some help in creating classes

    How are you assigning the values to each flower? You are creating the list but how are you populating it what code are you using?

    Simple example:
    Code:
    Flower x;
    Random rnd = new Random();
    
    // Create an empty list
    List<Flower> flowers = new List<Flower>(10);
    
    // Populate list with randomly created numbers
    for (int i = 0; i < 10; i++)
    {
       // Set x as a new instance for new flower
       x = new Flower();
    
       // Create random number and convert to string to set as name for flower
       x.ftype = rnd.Next().ToString();
    
      // Add the new flower to the list 
       flowers.Add(x);
    }
    
    // Write the name of all the Flower objects found in the list
    foreach (Flower f in flowers)
    {
       Console.WriteLine(f.ftype);
    }
    
    // Wait for user input before continuing
    Console.ReadKey();
    Post more of your code and I'm sure someone with more experience will chime in and help you out.

    Also when you post your code use
    [code]
    // Place code inside
    [/code]

  4. #4
    Join Date
    Jan 2010
    Posts
    1,133

    Re: need some help in creating classes

    The indices in C# arrays, lists, and other collections are zero-based; 0 is the first elem, 1 is the second, 2 is the third... n-1 is the n-th.

    To get the 31st flower, you would use an indexer:
    // assuming that flowers is an array
    flowers[30].plength = 1.2;

    Note - this is the same as:
    flower temp = flowers[30];
    temp.plength = 1.2;


    To iterate through the whole collection - you'd either use the foreach statement, or the for loop - Google can help you with that, as well as the answer posted by Ubiquitous.

    BTW, when it comes to C# naming conventions, you would usually use PascalCasing for type names (which include classes) - that is, start each word in the name with a capital letter.
    Last edited by TheGreatCthulhu; December 19th, 2011 at 07:45 AM.

  5. #5
    Join Date
    Dec 2011
    Location
    .Net 4.0
    Posts
    39

    Re: need some help in creating classes

    Code:
    public Form1()
            {
    
                InitializeComponent();
    
                Flower flower1 = new Flower(1, 1, 1, 1, "Daisy");
                MessageBox.Show("flower1: \n\n FlowerType=" + flower1.FlowerType);
    
                // This is SETTING the value (really, re-setting).
                flower1.FlowerType="Sunflower";
                // The reference to FlowerType below is just GETTING its value.
                MessageBox.Show("flower1: \n\n FlowerType=" + flower1.FlowerType);
    
                Flower flower32=new Flower(4,3,2,1,"Violet")
                //  Just use the MessageBox code above, and you can substitute for flower32.
                
            }
    
    
    
            public class Flower
            {
                            
                public Flower(double slength, double swidth, double plength, double pwidth, string ftype)
                //  You could name the above parameters whatever you want, but just know that they are to correspond with your properties in the proper order.    
                {
                    //  Here, you are saying that THIS NEW FLOWER has the values that you passed in the parameters above.
                    this.SepalLength = slength;
                    this.SepalWidth = swidth;
                    this.PedalLength = plength;
                    this.PedalWidth = pwidth;
                    this.FlowerType = ftype;
                }
    
                //  Now you have to have set and get methods for each.
                //  You can set the property with SET.
                //  You can get the value assigned to a property with GET.
                //  A GET is where you just want the value, as in double number1=flower1.slength (you are getting the slength and assigning it to number)
                //  A SET is where you assign the value, as in flower1.slength=2.34
    
                //  Note:   You should name your properties better, like below.   
                //  This way, you reference by something not so ambiguous, like flower1.FlowerType
                public double SepalLength { get; set; }
                    
                public double SepalWidth { get; set; }
               
                public double PedalLength { get; set; }
               
                public double PedalWidth { get; set; }
               
                public string FlowerType { get; set; }
    
            }
    Last edited by JeffInTexas; December 20th, 2011 at 08:38 PM.

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