Click here to Skip to main content
15,891,777 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
First of all, I'd like to thank all persons who read and try to help me.
I've been working on a simple -obsolete- Mobile Company to add a line and save the information of a client in a Data Grid, so I've written the following:

I've defined an line and defined the fields, constructors, and properties, with useful methods:


line.cs
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MobileCompany
{
  public  abstract class line
    {
      protected   int Id;
       protected string mobilenumber;
       protected string customername;
       static  int tmp = 0 ;

        public int Identity
        {
            get { return Id; }
        }

        public string MobileNumber 
        {
            set
            {
                if (value != null)
                { mobilenumber = value; }
            }

            get
            {
                return mobilenumber;
            }

        }

        public string CustomerName
    {
        set 
        {
            if (value != null)
            {
                customername = value;
            }
        }

        get 
        {
            return customername;
        }
    }


        public line()
        {
            tmp++;
            Id = tmp;
 
        }

        public line (string customername,string mobilenumber)
        {
            tmp++;
            Id = tmp;
            this.customername = customername;
            this.mobilenumber = mobilenumber;

        }

        //Methods Area

        public override string ToString()
        {
            return "Identity :\t"+Id+"\nCustomer Name :\t"+customername+"\nMoblie Number :\t"+mobilenumber;
        }

        public abstract decimal BillAmount(); //this is abstract, can't be known till we create a known (concrete) object
    }
}


Then, I've added -as requested- a subscribed line that inherits from line.cs :

SubscriberLine.cs

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MobileCompany
{
    class SubcriberLine : line 
    {
        int mm;
        int mf;

        //Propreties Area

        public int MoblietoMobile
        {
            set 
            {
                if (value > 0)
                    mm = value;
            }
            get
            { return mm; }

        }

        public int Mobiletofixed
        {
            set
            {
                if (value > 0)
                    mf = value;
            }

            get
            { return mf; }
        }

        //Constructors Area

    public SubcriberLine(string name , string number,int mm, int mf): base( name, number )
    {
        this.mm = mm;
        this.mf = mf;
    }

    public SubcriberLine()
    {
        // TODO: Complete member initialization
    }

        //Methods Area
    public override string ToString()
    {
        return "Identity :\t" + base.Id + "\nCustomer Name :\t" + base.customername + "\nMoblie Number :\t" + base.mobilenumber +
            "Totall Calling minutes :\nPhone Calls to Mobile:\t" + mm + "\tMobile Calls Charge:\t" + mobilecalls_cost()
            + "\nPhone Calls to Landlines:\t" + mf + "\tland Calls Charge:\t" + landcalls_cost() + "\nTotal Tax :\t" + BillAmount();

    }

    public decimal mobilecalls_cost()
    {
        return mm * 4;
    }
    public decimal landcalls_cost()
    {
        return mm * 6;
    }
    public override decimal BillAmount()
    {
        return mobilecalls_cost() + landcalls_cost();
    }

    }
}


Last, but not least, I've encoded the Company:

Company.cs
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;


namespace MobileCompany
{
 public   class Company
    {
         ArrayList data = new ArrayList();

        //Method: add line Object

        public void Addline(line l)
        {
            data.Add(l);

        }

        public Company()
        {

        }


        //Method: getline by index

        public line getLine(int index)
        {
            return data[index] as line;
        }




        public line this[int index]
        {
            get 
            {
                return data[index] as line;
            }
        }


        public int Count()
        {
            return data.Count;
        }



    }

  }

Here I've finished writing the important coding for my application.
I've headed next to the windows form application; it consists of two forms only.
The first form is simple, asks for client's name, number, mobile calls, and land calls; by Pressing buttom1 "Add line", a new line object is created.
It's simplified as follows:

Form1.cs
C#
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;
using System.Collections;
namespace MobileCompany
{
    public partial class Form1 : Form
    {
        //At the start of the Application, we Create a New Company

        public  static  Company MTN = new Company();
       
        
        public Form1()
        {
            InitializeComponent();



        }
        

        private void button1_Click(object sender, EventArgs e)
        {
           //After Entering data into the fields, we Create an Cellphone Line Object 

                SubcriberLine s = new SubcriberLine(txtName.Text,txtNumber.Text,Convert.ToInt32(txtMobile.Text),Convert.ToInt32(txtFixed.Text));

            //We Add the Cellphone Line Object to the Company's DataBase, Our Arraylist in our company MTN

                MTN.Addline(s);

            //confront the MTN Worker that the Adding process has been finished !

                MessageBox.Show("new CellPhone Line has been created !");
            
            



        }


        private void button2_Click(object sender, EventArgs e)
        {
            //In order to show all lines in Our company, we need to SHOW new table, new Page , or a new FORM

            Form2 f = new Form2();
            f.Show();
        }

     }
    }


Now, if you noticed, the functionality of buttom2 "Show All Lines" is Making another instance of Form called Form2, and simply showing it.
Here, I've Placed Only a DataGridView in Form2, wrote some code to ensure the DataGridView shows what is written, by retrieving data from the object :

Form2.cs

C#
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 MobileCompany
{
    public partial class Form2 :Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < Form1.MTN.Count(); i++)
            {
                dataGridView1.Rows.Add(Form1.MTN.getLine(i).Identity,Form1.MTN.getLine(i).CustomerName,Form1.MTN.getLine(i).MobileNumber,Form1.MTN.getLine(i).BillAmount());
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}



Now, I've finished a pile of coding, unfortunately, the data about an added client won't be shown on the DataGridView.
Can you please Help me find a solution?
I'd be pleased to hear comments about how my code is too.
Appreciate any contributor's help, as long as we're under the same Coding roof.
Posted
Comments
Shahin Khorshidnia 9-May-12 14:13pm    
I didn't read the code carefully. Sorry I am not in the mood right now, but: why don't you use binding? I mean create a collection of the object (For example a List) and bind it to DataGirdView. If you want to edit the DataGridView, edit the List istead of it then re-bind it and it'll work fine.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900