Hi

I like some beginnerhelp in C# and the basics of OOP. I have coded a bit but need some further guidance to continue.

This is an application with the purpose for my own training in capsulation, Inheritance and Polymorphism. Also see the attached image of user interface.

I need some help with example code to do the followed things.

As a first step I like to do is to use the input fields Nickname, Sort and Housing and number of legs to populate a listbox. The next steps will be to change and delete those values, but now I will be happy if I can get some help with the ADD issue.


This version contain only one abstract class as shown, but later I will have some more.


My questions and need for some helping code:
1. How can I populate a listbox with columns with values from the input ?
2. How can I use the abstract class to acheive that

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Assignment1
{
    public partial class Form1 : Form
    {

        private void Form1_Load(object sender, System.EventArgs e)
        {

        }

        //Dropdown
        public enum Sort
        {
            Horse,
            Dog,
            Wolf,
            Giraff
        }

        // Dropdown
        public enum HousingType
        { 
            Stable,
            Cage,
            Indoor,
            Outdoor
        }

        // Constructor
        public Form1()
        {
            InitializeComponent();
            cmbSort.DataSource = Enum.GetValues(typeof(Sort));
            cmbHousing.DataSource = Enum.GetValues(typeof(HousingType));
        }
    }


    // Abstract class (Animaldetails)
    public abstract class Animal
    {
        // Constructor
        public Animal()
        {
            this.m_idNumber = 0;
            this.m_name = "";
            this.m_numberOfLegs = 0;
        }

        // Private fields
        private int m_idNumber;
        private string m_name;
        private int m_numberOfLegs;

        // Public values
        public int idNumber
        {
            get { return this.m_idNumber; }
            set { this.m_idNumber = value; }
        }

        public string name
        {
            get { return this.m_name; }
            set { this.m_name = value; }
        }

        public int numberOfLegs
        {
            get { return this.m_numberOfLegs; }
            set { this.m_numberOfLegs = value; }
        }
    }


}

>1. How can I populate a listbox with columns with values from the
input ?

Create a collection of Aminal objects. For multi column list - use
ListView Control.
> 2. How can I use the abstract class to acheive that?

Create subclasses of Animal.

An abstract class is a class you can't instantiate.
Why is that?
Well if I come to you and say: "I'm an animal." (sometimes I'm a beast really...) I think you would say what kind of animal are you?
To work with a class of type Animal you have to derive a class from it. Like so (start a console app and put this in)

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Insect MyInsect = new Insect();
            Console.WriteLine(MyInsect.numberOfLegs);
            // this will not work when you compile:    Animal MyAnimal = new Animal();
            Console.ReadKey(); //hold console on screen until a keypress
        }

        public class Insect : Animal
        {
            public Insect() // constructor
            {
                base.numberOfLegs = 6;
            } 
            // other properties or methods of the Insect class comme here
        }

        public abstract class Animal
        {
            // Constructor
            public Animal()
            {
                this.m_idNumber = 0;
                this.m_name = "";
                this.m_numberOfLegs = 0;
            }

            // Private fields
            private int m_idNumber;
            private string m_name;
            private int m_numberOfLegs;

            // Public values
            public int idNumber
            {
                get { return this.m_idNumber; }
                set { this.m_idNumber = value; }
            }

            public string name
            {
                get { return this.m_name; }
                set { this.m_name = value; }
            }

            public int numberOfLegs
            {
                get { return this.m_numberOfLegs; }
                set { this.m_numberOfLegs = value; }
            }
        }

    }
}

A ListBox has an Items collection.
Use it like this:
MyListBox.Items.Add("some text");

ddanbe and adatapost gave you good advice and I hope you come back with more specific questions. If you're developing an abstract class you tend to have a design in your mind for multiple classes that will inherit from it. In this case "animals" might be better off as "livingBeing" or something similar, and then you could implement "animal" and "insect" from your base abstract class. You could also add code to the base abstract class for adding a row to the listbox with the predefined columns.

Ask more specific questions and upload your project if you still need help. It saves a lot of time when we are answering your question.

Thanks for all of the quick responses.

I will get back soon with an updated code as suggested and with more specific questions about the parts of the app.

//Tobbek

I did a few improvements about the abstract base class and added 2 derived classes.

The application is a part of a workshop, where a suggested applicqation could be
a animal register, not my invention. The usage is to gain skills in the OOP
thinking.

As you can see in the attached image of the user interface, I like to add, change
and delete posts in the listbox. At this moment I can just add values in one
column. BUT I also like use the abstract and the derived classes when manage
the values. This feels like science fiction right now... maybe you guys can
clear the fog a bit.


The abstract class Animal is used to encapsulate all the things that animals have in common.
The following attributes should be implemented in the class.

Animal’s name
ID number, provided by the program
Number of legs
Housing (stable, cage, outdoor, indoor). I will use enum for this data.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Assignment1
{
    public partial class Form1 : Form
    {

        private void Form1_Load(object sender, System.EventArgs e)
        {
            
        }

        //Dropdown
        public enum Sort
        {
            Horse,
            Dog,
            Wolf,
            Giraff
        }

        // Dropdown
        public enum HousingType
        { 
            Stable,
            Cage,
            Indoor,
            Outdoor
        }

        // Constructor
        public Form1()
        {
            InitializeComponent();
            cmbSort.DataSource = Enum.GetValues(typeof(Sort));
            cmbHousing.DataSource = Enum.GetValues(typeof(HousingType));
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            lstListvalues.Items.Add(txtNickName.Text);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            lstListvalues.Items.Clear();
        }

    }

    // Abstract class (Animaldetails)
    public abstract class Animal
    {
        // Constructor
        public Animal()
        {
            this.m_idNumber = 0;
            this.m_name = "";
            this.m_numberOfLegs = 0;
        }

        // Private fields
        private int m_idNumber;
        private string m_name;
        private int m_numberOfLegs;

        // Public values
        public int idNumber
        {
            get { return this.m_idNumber; }
            set { this.m_idNumber = value; }
        }

        public string name
        {
            get { return this.m_name; }
            set { this.m_name = value; }
        }

        public int numberOfLegs
        {
            get { return this.m_numberOfLegs; }
            set { this.m_numberOfLegs = value; }
        }

        
    }


    public class PlantEater : Animal
    {
        //Constructor
        public PlantEater()
        {
            this.idNumber = 1;
            this.name = "Bug";
            this.numberOfLegs = 6;
        }   
    }

    public class MeatEater : Animal
    {
        //Constructor
        public MeatEater()
        {
            this.idNumber = 2;
            this.name = "Scooby Doo";
            this.numberOfLegs = 4;
        }

    }



}

I have attached examples of the user interface (how it should look like) and a class diagram.

I like to start with the listview or listbox control. How can i add columns. Headers is not necessary. Whick one is best for the job?
I havent find a working way to just add lstListValues.Columns("bla bla");

In the listbox control this creates only rows, no columns:
lstListValues.Items.Add(txtNickName.Text);

In the listview control this creates only columns, no rows:
lstListValues.Items.Add(txtNickName.Text);

So are you looking for help - how to use listview control?

Yes...

I uses the listview now, and make it view the values in rows, but how to add more columns to it ?

lstListAnimals.View = View.List;
lstListAnimals.Items.Add(txtNickName.Text);

Use following code:

ListView1.View = View.Details
        ListView1.Columns.Add("Col1")
        ListView1.Columns.Add("Col1")
        ListView1.Columns.Add("Col1")
        ListView1.Columns.Add("Col1")

         ' A row contains four col vlaue.
        Dim item1 As New ListViewItem(New String() {"AA", "BB", "CC", "DDD"})
        ListView1.Items.Add(item1)
        ' Another way to add four values
        item1 = New ListViewItem("One")
        item1.SubItems.Add("A")
        item1.SubItems.Add("B")
        item1.SubItems.Add("C")
        item1.SubItems.Add("D")
        ListView1.Items.Add(item1)

According to your class design scheme the MeatEater and PlantEater classes should also be made abstract.
Both ListView and ListBox have some sort of column facility(look it up!)
Perhaps string concatenation is also an option here?

The columns looks fine, but I may missing something here, how do I setup values to be shown in the different columns ?

lstListAnimals.View = View.Details;

lstListAnimals.Columns.Add("Col1");
lstListAnimals.Columns.Add("Col2");
lstListAnimals.Columns.Add("Col3");
lstListAnimals.Columns.Add("Col4");

lstListAnimals.Items.Add(txtNickName.Text);
lstListAnimals.Items.Add(txtNumberOfLegs.Text);

Yes ddanbe

The derived classes Meateater and planteater is now abstract as well, this is what I have done so far. I have no returning values from these yet. Still thinking how to do do things :-|

public abstract class PlantEater : Animal
{
    //Constructor
    public PlantEater()
    {
        this.idNumber = 1;
        this.name = "Bug";
        this.numberOfLegs = 6;
    }   
}

public abstract class MeatEater : Animal
{
    //Constructor
    public MeatEater()
    {
        this.idNumber = 2;
        this.name = "Scooby Doo";
        this.numberOfLegs = 4;
    }
}

I am sorry I have posted VB.NET code.

ListView1.View = View.Details
        ListView1.Columns.Add("Col1");
        ListView1.Columns.Add("Col1");
        ListView1.Columns.Add("Col1");
        ListView1.Columns.Add("Col1");

         ' A row contains four col vlaue.
        ListViewItem item1=new  ListViewItem(new String[] {"AA", "BB", "CC", "DDD"});
        ListView1.Items.Add(item1);
        ' Another way to add four values
        item1 = new ListViewItem("One");
        item1.SubItems.Add("A");
        item1.SubItems.Add("B");
        item1.SubItems.Add("C");
        item1.SubItems.Add("D");
        ListView1.Items.Add(item1);

Thanks
The columns looks good, but new columns is repeated together with new post as shown in the attachment

lstListAnimals.View = View.Details;

lstListAnimals.Columns.Add("Nickname");
lstListAnimals.Columns.Add("Legs");       
lstListAnimals.Columns.Add("Col3");
lstListAnimals.Columns.Add("Col4");

ListViewItem item1 = new ListViewItem(new String[] { txtNickName.Text, txtNumberOfLegs.Text, "CC", "DDD" });
lstListAnimals.Items.Add(item1);

The columns repeats when new posts been added to the listview as shown in my previous post. Is there anyway to set this, in code or something else

The only thing I can think of is that you are adding columns in the same place you are adding items. (hence duplicates) You should add columns only once.

Okidoki

I got the point, dont know how to change it though.

This is all of the current testcode. With use of base class, derived class and so on as supposed.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Assignment1
{
    public partial class iAnimal : Form
    {

        private void Form1_Load(object sender, System.EventArgs e)
        {
            
        }

        //Dropdown
        public enum Sort
        {
            Horse,
            Dog,
            Wolfe,
            Giraffe
        }

        // Dropdown
        public enum HousingType
        { 
            Stable,
            Cage,
            Indoor,
            Outdoor
        }

        // Constructor
        public iAnimal()
        {
            InitializeComponent();
            cmbSort.DataSource = Enum.GetValues(typeof(Sort));
            cmbHousing.DataSource = Enum.GetValues(typeof(HousingType));
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            MeatEater getMeat = new MeatEater();
            string iNickname = txtNickName.Text;


            lstListAnimals.View = View.Details;

            lstListAnimals.Columns.Add("Nickname");
            lstListAnimals.Columns.Add("Legs");       
            lstListAnimals.Columns.Add("Housing");
            lstListAnimals.Columns.Add("Category");
            lstListAnimals.Columns.Add("Sort");



            ListViewItem item1 = new ListViewItem(new String[] { getMeat.NickName(iNickname), txtNumberOfLegs.Text, "CC", "DDD", "EEEE" });
            lstListAnimals.Items.Add(item1);

       }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            lstListAnimals.Items.Clear();
        }

    }


    // Abstract class (Animaldetails)
    public abstract class Animal
    {

        // Private fields
        private int m_idNumber;
        private string m_name;
        private int m_numberOfLegs;

        // Constructor
        public Animal()
        {
            this.m_idNumber = 0;
            this.m_name = "";
            this.m_numberOfLegs = 0;
        }

        // Public values
        public int idNumber
        {
            get { return this.m_idNumber; }
            set { this.m_idNumber = value; }
        }

        public string name
        {
            get { return this.m_name; }
            set { this.m_name = value; }
        }

        public int numberOfLegs
        {
            get { return this.m_numberOfLegs; }
            set { this.m_numberOfLegs = value; }
        }

    }


    public abstract class PlantEater : Animal
    {
        //Constructor
        public PlantEater()
        {
            this.idNumber = 1;
            this.name = "Bug";
            this.numberOfLegs = 6;
        }   
    }

    public class MeatEater : Animal
    {
        //Constructor
        public MeatEater()
        {
            this.idNumber = 2;
            this.name = "Scooby Doo";
            this.numberOfLegs = 4;
        }

        public string NickName(string nickname)
        {
            this.name = nickname;
            return this.name;
        }
    }

}

Can't you see this for yourself?
Everytime you click your Add button, this code will get executed:

lstListAnimals.Columns.Add("Nickname");
            lstListAnimals.Columns.Add("Legs");       
            lstListAnimals.Columns.Add("Housing");
            lstListAnimals.Columns.Add("Category");
            lstListAnimals.Columns.Add("Sort");

Execute this code only once, somewhere else, in a contructor,a load event handler...

Thanks

The ctor did the trick

Stupid me :-)

Am I on the right track when using the base and derived classes or am I lost forever. A few things doesnt really work yet such as the change and delete row things. With the fixed column it looks pretty good at the moment.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Assignment1
{
    public partial class iAnimal : Form
    {
        

        private void Form1_Load(object sender, System.EventArgs e)
        {
            
        }

        //Dropdown
        public enum Sort
        {
            Horse,
            Dog,
            Wolfe,
            Giraffe
        }

        // Dropdown
        public enum HousingType
        { 
            Stable,
            Cage,
            Indoor,
            Outdoor
        }

        // Constructor
        public iAnimal()
        {
            InitializeComponent();
            cmbSort.DataSource = Enum.GetValues(typeof(Sort));
            cmbHousing.DataSource = Enum.GetValues(typeof(HousingType));

            // Listview header
            lstListAnimals.View = View.Details;
            lstListAnimals.Columns.Add("Nickname");
            lstListAnimals.Columns.Add("Legs");
            lstListAnimals.Columns.Add("Housing");
            lstListAnimals.Columns.Add("Category");
            lstListAnimals.Columns.Add("Sort");
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (rbnMeatEater.Checked.Equals(true))
            {
                Dog getCategory = new Dog();
                ListViewItem item1 = new ListViewItem(new String[] { getCategory.NickName(txtNickName.Text), txtNumberOfLegs.Text, cmbHousing.SelectedItem.ToString(), "Meat eater", cmbSort.SelectedItem.ToString() });
                lstListAnimals.Items.Add(item1);
            }
            if (rbnPlantEater.Checked.Equals(true))
            {
                Horse getCategory = new Horse();
                ListViewItem item1 = new ListViewItem(new String[] { getCategory.NickName(txtNickName.Text), txtNumberOfLegs.Text, cmbHousing.SelectedItem.ToString(), "Plant eater", cmbSort.SelectedItem.ToString() });
                lstListAnimals.Items.Add(item1);
            }

       }

        // Clear the listview
        private void btnDelete_Click(object sender, EventArgs e)
        {
            lstListAnimals.Items.Clear();
        }

        // Exit the application
        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

    }


    // Abstract base class (Animaldetails)
    public abstract class Animal
    {

        // Private fields
        private int m_idNumber;
        private string m_name;
        private int m_numberOfLegs;

        // Constructor
        public Animal()
        {
            this.m_idNumber = 0;
            this.m_name = "";
            this.m_numberOfLegs = 0;
        }

        // Public values
        public int idNumber
        {
            get { return this.m_idNumber; }
            set { this.m_idNumber = value; }
        }

        public string name
        {
            get { return this.m_name; }
            set { this.m_name = value; }
        }

        public int numberOfLegs
        {
            get { return this.m_numberOfLegs; }
            set { this.m_numberOfLegs = value; }
        }

    }

    // Abstract derived class from Animal
    public abstract class PlantEater : Animal
    {
        //Constructor
        public PlantEater()
        {
            this.idNumber = 0;
            this.name = "";
            this.numberOfLegs = 0;
        }

    }

    // Derived class from PlantEater
    public class Horse : PlantEater
    { 
           //Constructor
        public Horse()
        {
            this.idNumber = 002;
            this.name = "";
            this.numberOfLegs = 4;
        }

        public int IDNumber
        {
            get { return this.idNumber; }
            set { this.idNumber = value; }
        }

        public string NickName(string nickname)
        {
            this.name = nickname;
            return this.name;
        }

        public int NumberOfLegs
        {
            get { return this.numberOfLegs; }
            set { this.numberOfLegs = value; }
        }
    
    }

    // Abstract derived class from Animal
    public abstract class MeatEater : Animal
    {
        //Constructor
        public MeatEater()
        {
            this.idNumber = 0;
            this.name = "";
            this.numberOfLegs = 0;
        }

    }

    // Derived class from MeatEater
    public class Dog : MeatEater
    {
        //Constructor
        public Dog()
        {
            this.idNumber = 001;
            this.name = "";
            this.numberOfLegs = 4;
        }

        public int IDNumber
        {
            get { return this.idNumber; }
            set { this.idNumber = value; }
        }

        public string NickName(string nickname)
        {
            this.name = nickname;
            return this.name;
        }

        public int NumberOfLegs
        {
            get { return this.numberOfLegs; }
            set { this.numberOfLegs = value; }
        }
    
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.