C#的新手,所以这只是我在阅读了一些tutiroals后猜测它。
我有一个名为PS3RemoteDevice的类:
namespace PS3_BluMote
{
public class PS3RemoteDevice
{
从我的主窗体按钮我尝试这样做:
PS3RemoteDevice PS3R = new PS3RemoteDevice;
PS3R.Connect();
但当然,这似乎不起作用。因为我是一个菜鸟,所以有点帮助会很棒!
谢谢!
大卫
PS3RemoteDevice.cs代码
using System;
using System.Collections.Generic;
using System.Timers;
using HIDLibrary;
namespace PS3_BluMote
{
public class PS3RemoteDevice
{
public event EventHandler<ButtonData> ButtonAction;
public event EventHandler<EventArgs> Connected;
public event EventHandler<EventArgs> Disconnected;
private HidDevice HidRemote;
private Timer TimerFindRemote;
private Timer TimerHibernation;
private int _vendorID = 0x054c;
private int _productID = 0x0306;
private int _batteryLife = 100;
private bool _hibernationEnabled = false;
public PS3RemoteDevice(int VendorID, int ProductID, bool HibernationEnabled)
{
if (HibernationEnabled)
{
TimerHibernation = new Timer();
TimerHibernation.Interval = 60000;
TimerHibernation.Elapsed += new ElapsedEventHandler(TimerHibernation_Elapsed);
}
_vendorID = VendorID;
_productID = ProductID;
_hibernationEnabled = HibernationEnabled;
}
public void Connect()
{
if (!FindRemote())
{
StartRemoteFindTimer();
}
}
Etc等......
答案 0 :(得分:3)
假设你有其余的类代码,
PS3RemoteDevice PS3R = new PS3RemoteDevice();
将与parantheses一起使用。
答案 1 :(得分:3)
试
PS3RemoteDevice PS3R = new PS3RemoteDevice();
编辑(使用params):
PS3RemoteDevice PS3R = new PS3RemoteDevice(someVendorID, some ProductID, someBoolHibernationEnabled);
答案 2 :(得分:1)
您的类只有一个带有三个参数的构造函数:
public PS3RemoteDevice(int VendorID, int ProductID, bool HibernationEnabled)
{ ... }
就像在任何其他方法调用中一样,当您使用new
实例化对象时,需要传递这些参数,例如:
int vendorId = 5;
int productId = 42;
PS3RemoteDevice PS3R = new PS3RemoteDevice(vendorId, productId, false);
答案 3 :(得分:1)
当您创建对象的新实例时,您正在调用其构造函数方法(简称构造函数)。它就像任何其他方法一样,但如果已定义,则需要调用它。语法为new MyObject(... parameters here ...);
。
我搜索了您之前的问题并找到了a screenshot of the code you are using。你有一个带参数的构造函数。这些参数未定义为可选参数,因此您必须提供它们。这些是vendorId
,productId
和hibernationEnabled
。
因此,您的代码将为new PS3RemoteDevice(0x054c, 0x0306, false);
。或者至少这是截图中的默认值。通过阅读您正在使用的代码的文档来确定要传递的正确值。