Click here to Skip to main content
14,692,401 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello,how to centralize that part of code with the use of a generic list.

DG.DataSource = null;
           var source = new BindingSource();
           source.DataSource = Lcls.lc;
           DG.DataSource = source;


that is my source code I insert and my problem is localized in MyData.cs file
MyData.cs
class MyData:clslistes<Object>
    {
 
        public void RemplirDataGridList(DataGridView DG,clslistes<Object> Lcls)
        {
            DG.DataSource = null;
            var source = new BindingSource();
            source.DataSource = Lcls.lc;
            DG.DataSource = source;
        }
    }

clslistes.cs

abstract class clslistes<t>
    {
       public static List<t> lc = new List<t>();
       public clslistes()
       {
 
       }

clsClient.cs
class clsClient:clslistes<clsClient>
    {
        private int _NumCl;
 
        public int Numero
        {
            get { return _NumCl; }
            set { _NumCl = value; }
        }
        private string _NomCl;
 
        public string GETSETNomCl
        {
            get { return _NomCl; }
            set { _NomCl = value; }
        }
        private string _telCl;
 
        public string GETSETTelCl
        {
            get { return _telCl; }
            set { _telCl = value; }
        }
        private string _adressCl;
 
        public string GETSETadressCl
        {
            get { return _adressCl; }
            set { _adressCl = value; }
        }
        private string _EmailCl;
 
        public string GETSETEmailCl
        {
            get { return _EmailCl; }
            set { _EmailCl = value; }
        }
        private string _VilleCl;
 
        public string GETSETvilleCl
        {
            get { return _VilleCl; }
            set { _VilleCl = value; }
        }
        private string _paysCl;
 
        public string GETSETpaysCl
        {
            get { return _paysCl; }
            set { _paysCl = value; }
        }
        public clsClient()
        {}
 
        public clsClient(int NumCl, string NomCl, string telCl, string adressCl, string EmailCl, string VilleCl, string paysCl)
        {
 
            this._NumCl = NumCl;
            this._NomCl = NomCl;
            this._adressCl = adressCl;
            this._telCl = telCl;
            this._EmailCl = EmailCl;
            this._VilleCl = VilleCl;
            this._paysCl = paysCl;
 
        }
 
        public bool rechercheClient(int num)
        {
            foreach (clsClient cl in lc)
            {
                if (cl.Numero == num)
                    return true;
 
            }
            return false;
 
        }
 
        public bool ajoutclient(clsClient cl)
        {
            if (this.rechercheClient(cl.Numero) == false)
            {
                lc.Add(cl);
                return true;
            }
 
            return false;
        }
 
        private int rechercheclientposit(int num)
        {
            clsClient cl = new clsClient();
            for (int i = 0; i < lc.Count; i++)
            {
                cl = (clsClient)lc[i];
                if (cl.Numero == num)
                {
                    return i;
 
                }
            }
            return -1;
 
        }
 
 
 
        public bool modifierclient(int numcl,clsClient Ncli)
        {
            if (this.rechercheclientposit(numcl) == -1)
                return false;
            else
            {
                lc[rechercheclientposit(numcl)] = Ncli;
                return true;
 
            }
 
 
        }
 
        public bool supprimerClient(int num)
        {
            if (this.rechercheclientposit(num) == -1)
                return false;
            else
            {
                lc.RemoveAt(rechercheclientposit(num));
                return true;
 
            }
 
        }
    }

knowing that the error is localized exactly in this line MyData.cs file

source.DataSource = Lcls.lc


What I have tried:

i try to change the input parametres of
clslistes<Object> Lcls
with
clslistes<T> Lcls
but it shows error

public void RemplirDataGridList(DataGridView DG,clslistes<Object> Lcls)
        {
            DG.DataSource = null;
            var source = new BindingSource();
            source.DataSource = Lcls.lc;
            DG.DataSource = source;
        }
Posted
Updated 20-Oct-16 0:11am
Comments
[no name] 19-Oct-16 21:29pm
   
List is already a generic class, why are you trying to create a generic class from a generic class?
johannesnestler 20-Oct-16 4:58am
   
just a comment: I'd strongly recomend you overthink your naming-conventions - maybe to something more .NET-ish? I get eye-cancer when I read your code ;)
Rahul VB 20-Oct-16 5:43am
   
:) hahaha i agree

1 solution

So which error? - what you do here makes no sense to me... - a generic type that represents a list (hard to know from your naming) that contains a generic list of the same type - is this what you wanted?

But anyway I guess that your problem is just the declaration of RemplirDataGridList - it has to be the same (t) - come on, really read the .NET naming conventions! - as you declared you clslistes instance so it's:

public void RemplirDataGridList<t>(DataGridView DG,clslistes<t> Lcls)


naming conventions for NET.
https://msdn.microsoft.com/en-us/library/ms229045(v=vs.110).aspx
(I'm not joking: programming is inventing and remembering names - from all years of my experience, this is the most important thing I would say to be successful in programming...)
   
v3
Comments
Rahul VB 20-Oct-16 5:47am
   
Nice, and thanks for sharing :
https://msdn.microsoft.com/en-us/library/ms229045(v=vs.110).aspx

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