作為 uni 作業的一部分,我需要在 C# 中創建一個自助結賬模擬并添加一個附加功能 - 在這種情況下是一個俱樂部卡積分系統。
我創建了一個成員類來保存點數,但是我試圖在單擊掃描會員卡按鈕時讓我的 winforms UI 顯示成員名稱。
GetName() 函式如下 -
class Members
{
// Attributes
protected int membershipID;
protected string firstName;
protected string lastName;
protected int clubcardPoints;
// Constructor
public Members(int membershipID, string firstName, string lastName, int clubcardPoints)
{
this.membershipID = membershipID;
this.firstName = firstName;
this.lastName = lastName;
this.clubcardPoints = clubcardPoints;
}
// Operations
public string GetName()
{
string name = firstName " " lastName;
return name;
}
...
}
這是 UserInterface.cs 中 UpdateDisplay 函式的一個片段。
void UpdateDisplay()
{
// DONE: use all the information we have to update the UI:
// - set whether buttons are enabled
// - set label texts
// - refresh the scanned products list box
if (selfCheckout.GetCurrentMember() != null)
{
lblMember.Text = Members.GetName();
}
...
}
不能使用Members.GetName(),因為它不是靜態函式,但不知道是否可以將其轉換為靜態函式。讓這個作業的最好方法是什么?
感謝任何幫助,我希望它是有道理的!對此很陌生,所以我不確定我的措辭是否正確。
uj5u.com熱心網友回復:
你可以打電話嗎 selfCheckout.GetCurrentMember().GetName()
uj5u.com熱心網友回復:
不能使用 Members.GetName() 因為它不是靜態函式
發生此錯誤GetName是因為方法是實體的成員。但是您將其稱為靜態成員。要將其稱為實體成員,您應該這樣做:
var currentMember = selfCheckout.GetCurrentMember(); //to make both check for null and call methods, you should firstly create variable with this object
if (currentMember != null)
{
lblMember.Text = currentMember.GetName(); // if current member is not null, call GetName
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/380921.html
