我试图将类属性的名称传递给方法中的参数。目前,我正在复制该方法并更改“杂物”属性和“ Customers [i]”属性。该方法大约需要4次,因此最好一次进行整洁。然后,我将使用一个模式弹出窗口来加载不同的表,具体取决于调用的客户类的属性。
如何在BindList方法中将Customer类属性用作参数?
我看过一些有关字典和功能的帖子,但不确定如何使用它们。
*注意:静态列表客户;
(另一种方法:Cust = CustomerList)
public class Customer
{
public string CustName { get; set; }
public string CustPhone { get; set; }
public string CustAddress { get; set; }
public string CustHistory { get; set; }
}
*也:参数只是示例,因为我不知道要放什么。
public void BindLists(sundries.prop sundparm, Customer.prop custparm)
{
SundriesRepo sundries = new SundriesRepo();
List<string> TempList = new List<string>();
TempList = sundries.sundparm;
lblEdit.Text = "History Edit";
List<string> Used = new List<string>();
List<string> NotUsed = new List<string>();
if (Patients.Count > 0)
{
for (int i = 0; i < Cust.Count; i++)
{
string result = Cust[i].custparm;
if (result != null)
{
Used.Add(result);
}
}
NotUsed = TempList.Except(Used).ToList();
}
listNotUsed.DataSource = NotUsed;
listNotUsed.DataBind();
listUsed.DataSource = Used;
listUsed.DataBind();
modPopExt1.Show();
}
,HTML是:
<asp:Panel runat="server" ID="modPan" CssClass="modPan">
<div id="modPanHeader">
<asp:Label runat="server" ID="lblEdit"></asp:Label>
</div>
<div id="divfloat" class="divfloat">
<div id="divList" style="float: left" class="float">
<asp:ListBox runat="server" ID="listNotUsed" CssClass="listBox"></asp:ListBox>
</div>
<div id="divPatient" style="float: right" class="float">
<asp:ListBox runat="server" ID="listUsed" CssClass="listBox"></asp:ListBox>
</div>
<div class="divModPanBtnClose">
<asp:Button runat="server" ID="modPanBtnClose" Text="Close"/>
</div>
</div>
</asp:Panel>
<asp:ModalPopupExtender runat="server" ID="modPopExt1" PopupControlID="modPan" CancelControlID="modPanBtnClose"
TargetControlID="lblEdit"></asp:ModalPopupExtender>
我无法编写一个在“字符串结果= Cust [i] .custparm;”处接受的参数。
答案 0 :(得分:0)
如果我理解正确,那么您在问是否可以将类的属性作为方法参数传递。
当然可以。
或者只是通过/使该方法期望一个Customer
实例。
var prop1 = null;
var prop2 =null;
var prop3 = null;
var prop4 = null;
//Which ever prop gets value
public void BindLists(string prop1 = null, string prop2= null, string prop3 =null, string prop4 =null)
{
//check which ever is not null
//Since you say one of them will be having the value.
enter rest code here
}
//calling side
Customer cust = new Customer();
BindLists(prop1 = value, prop2 = value, prop3=value, prop4 = value) //default will be null