使用通用列表 [英] Using of the generic list
问题描述
你好,如何使用通用列表集中这部分代码。
DG.DataSource = 空跨度>;
var source = new BindingSource();
source.DataSource = Lcls.lc;
DG.DataSource = source;
这是我插入的源代码,我的问题已在MyData.cs文件中本地化
MyData.cs
class MyData:clslistes< Object>
{
public void RemplirDataGridList(DataGridView DG,clslistes<对象> Lcls)
{
DG.DataSource = null ;
var source = new BindingSource();
source.DataSource = Lcls.lc;
DG.DataSource = source;
}
}
clslistes.cs
abstract class clslistes< t>
{
public static 列表< 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)
{
这 ._ NumCl = NumCl;
this ._ NomCl = NomCl;
此 ._ adressCl = adressCl;
this ._ telCl = telCl;
this ._ EmailCl = EmailCl;
this ._ VilleCl = VilleCl;
此 ._ paysCl = paysCl;
}
public bool rechercheClient(< span class =code-keyword> int num)
{
foreach (clsClient cl in lc)
{
if (cl.Numero == num)
返回 true ;
}
返回 false ;
}
public bool ajoutclient(clsClient cl)
{
if ( this .rechercheClient(cl.Numero)= = false )
{
lc.Add(cl);
return true ;
}
返回 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 我;
}
}
return -1;
}
public bool modifierclient( int numcl,clsClient Ncli)
{
if ( this .rechercheclientposit(numcl)== -1)
return 假跨度>;
else
{
lc [rechercheclientposit(numcl)] = Ncli;
return true ;
}
}
public bool supprimerClient( int num)
{
if ( this .rechercheclientposit(num)== -1)
return 假跨度>;
else
{
lc.RemoveAt(rechercheclientposit(num));
return true ;
}
}
}
知道错误已完全本地化在此行MyData中。 cs文件
source.DataSource = Lcls.lc
我尝试过:
i尝试更改
clslistes<的输入参数;对象>具有 clslistes< T>的Lcls
Lcls
但它显示错误
public void RemplirDataGridList(DataGridView DG,clslistes< Object> Lcls)
{
DG.DataSource = 空跨度>;
var source = new BindingSource();
source.DataSource = Lcls.lc;
DG.DataSource = source;
}
那么哪个错误? - 你在这里做的事对我来说没有意义...... - 一个代表一个列表(很难从你的命名中知道)的通用类型,它包含一个相同类型的通用列表 - 这就是你想要的吗?
但无论如何我猜你的问题只是RemplirDataGridList的声明 - 它必须是相同的(t
) - 来吧,真正阅读.NET命名约定! - 当你声明你clslistes
实例时它是:
public void RemplirDataGridList< t>(DataGridView DG,clslistes< t> Lcls)
NET的命名约定。
https://msdn.microsoft.com/en-us/library/ms229045(v=vs.110 ).aspx
(我不是在开玩笑:编程是在发明和记住名字 - 从我的经验来看,这是我在编程方面取得成功最重要的事情......)
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;
}
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 youclslistes
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...)
这篇关于使用通用列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!