当我不能使函数本身成为静态时如何调用非静态函数
How to call a non-static function when I cannot make the function itself static
作为 uni 作业的一部分,我需要在 C# 中创建一个自助结账模拟并添加一个附加功能 - 在本例中是一个 clubcard 积分系统。
我创建了一个会员 class 来持有积分,但我试图让我的 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() 不能用,因为它不是静态函数,但不知道能不能转成静态函数。实现此功能的最佳方法是什么?
感谢任何帮助,我希望它有意义!对此很陌生,所以我不确定我的措辞是否正确。
你能打电话吗selfCheckout.GetCurrentMember().GetName()
Members.GetName() can't be used because it isn't a static function
发生此错误是因为 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
}
它不起作用,因为您不是在引用对象而是在引用实例,您可以将成员 class 设置为静态或先获取当前成员。
作为 uni 作业的一部分,我需要在 C# 中创建一个自助结账模拟并添加一个附加功能 - 在本例中是一个 clubcard 积分系统。
我创建了一个会员 class 来持有积分,但我试图让我的 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() 不能用,因为它不是静态函数,但不知道能不能转成静态函数。实现此功能的最佳方法是什么?
感谢任何帮助,我希望它有意义!对此很陌生,所以我不确定我的措辞是否正确。
你能打电话吗selfCheckout.GetCurrentMember().GetName()
Members.GetName() can't be used because it isn't a static function
发生此错误是因为 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
}
它不起作用,因为您不是在引用对象而是在引用实例,您可以将成员 class 设置为静态或先获取当前成员。