当前上下文中不存在名称 "PostDevice.Connect"
The name "PostDevice.Connect" does not exist in the current context
我是 c# 的新手,所以我不太明白为什么会收到此错误。我正在使用 IngenicoPOS class 创建销售,但我想在 pos 操作 class 中创建销售操作,因此它与其他操作分开。但是,当我尝试连接 posDevice 时,我收到一个错误,其中显示 postDevice.Connect 在当前上下文中不存在。连接部分,我按照他们的document。现在,我认为我在创建新的 post 设备时做错了,或者因为我试图在 class 中创建连接。
完整代码:
class PosOperations
{
public PosOperations(string operationType, string paymentType, int amountToPay)
{
this.OperationType = operationType;
this.PaymentType = paymentType;
this.AmountToPay = amountToPay;
}
public string OperationType { get; }
public int AmountToPay { get; }
public string PaymentType { get; }
private const string PORT = "COM9";
private POS posDevice = new POS(PORT);
posDevice.Connect(); // Will return true if connection is made, otherwise will return false
}
你能帮帮我吗?我如何在另一个 class 中使用另一个 class 的方法?为什么在当前上下文中找不到这个?
类型(其中 class
是一种)只能包含 成员。您有一个名为 posDevice
的现场成员。您可以在声明该字段的同一行实例化该字段,但不允许 运行 使用该字段的任何其他语句。
这就是构造函数的用途。将字段的初始化以及您要进行的附加连接调用移至构造函数。
public PosOperations(string operationType, string paymentType, int amountToPay)
{
this.OperationType = operationType;
this.PaymentType = paymentType;
this.AmountToPay = amountToPay;
posDevice = new POS(PORT);
posDevice.Connect();
}
// ...
private POS posDevice;
现在,一些可选的建议供您稍后研究和实施:
- Make
posDevice
readonly
,因为现在只有构造函数应该分配变量
- 考虑
Connect()
的 return 值。当它 return 为 false 时做点什么,比如抛出异常。
- 执行
IDisposable
以在处理 PosOperations
实例时关闭连接。
我是 c# 的新手,所以我不太明白为什么会收到此错误。我正在使用 IngenicoPOS class 创建销售,但我想在 pos 操作 class 中创建销售操作,因此它与其他操作分开。但是,当我尝试连接 posDevice 时,我收到一个错误,其中显示 postDevice.Connect 在当前上下文中不存在。连接部分,我按照他们的document。现在,我认为我在创建新的 post 设备时做错了,或者因为我试图在 class 中创建连接。 完整代码:
class PosOperations
{
public PosOperations(string operationType, string paymentType, int amountToPay)
{
this.OperationType = operationType;
this.PaymentType = paymentType;
this.AmountToPay = amountToPay;
}
public string OperationType { get; }
public int AmountToPay { get; }
public string PaymentType { get; }
private const string PORT = "COM9";
private POS posDevice = new POS(PORT);
posDevice.Connect(); // Will return true if connection is made, otherwise will return false
}
你能帮帮我吗?我如何在另一个 class 中使用另一个 class 的方法?为什么在当前上下文中找不到这个?
类型(其中 class
是一种)只能包含 成员。您有一个名为 posDevice
的现场成员。您可以在声明该字段的同一行实例化该字段,但不允许 运行 使用该字段的任何其他语句。
这就是构造函数的用途。将字段的初始化以及您要进行的附加连接调用移至构造函数。
public PosOperations(string operationType, string paymentType, int amountToPay)
{
this.OperationType = operationType;
this.PaymentType = paymentType;
this.AmountToPay = amountToPay;
posDevice = new POS(PORT);
posDevice.Connect();
}
// ...
private POS posDevice;
现在,一些可选的建议供您稍后研究和实施:
- Make
posDevice
readonly
,因为现在只有构造函数应该分配变量 - 考虑
Connect()
的 return 值。当它 return 为 false 时做点什么,比如抛出异常。 - 执行
IDisposable
以在处理PosOperations
实例时关闭连接。