C# 创建一个实例
C# 新手,所以这只是我在阅读一些教程后的猜测。
我有一个名为 PS3RemoteDevice 的类:
namespace PS3_BluMote
{
public class PS3RemoteDevice
{
并且从我的主窗体按钮我尝试这样做:
PS3RemoteDevice PS3R = new PS3RemoteDevice;
PS3R.Connect();
但是当然,这似乎不起作用。因为我是菜鸟,所以能提供一点帮助就太好了!
谢谢!
David
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();
}
}
等等等等......
New to C# so this is just me guessing at it after reading some tutiroals.
I have a class called PS3RemoteDevice:
namespace PS3_BluMote
{
public class PS3RemoteDevice
{
And from my main form button i try doing this:
PS3RemoteDevice PS3R = new PS3RemoteDevice;
PS3R.Connect();
But of course, that does not seem to work. A little help since i am a noob would be great!
Thanks!
David
PS3RemoteDevice.cs code
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 etc....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
假设您拥有其余的类代码,
PS3RemoteDevice PS3R = new PS3RemoteDevice();
将使用括号。Assuming you have the rest of the class code,
PS3RemoteDevice PS3R = new PS3RemoteDevice();
will work, with the parantheses.尝试
编辑(带参数):
try
EDIT (with params):
您的类只有一个带有三个参数的构造函数:
就像在任何其他方法调用中一样,当您使用
new
实例化对象时,您需要传递这些参数,例如:Your class only has a constructor that takes three parameters:
Just like in any other method call you need to pass those parameters when you instantiate an object with
new
, for example:当您创建对象的新实例时,您正在调用其构造函数方法(简称构造函数)。它与任何其他方法一样,但需要您调用它(如果已定义)。语法为
new MyObject(...此处的参数...);
。我搜索了您之前的问题,找到了您正在使用的代码的屏幕截图。您有一个带有参数的构造函数。这些参数未定义为可选,因此您必须提供它们。它们是
vendorId
、productId
和hibernationEnabled
。因此您的代码将是
new PS3RemoteDevice(0x054c, 0x0306, false);
。或者至少屏幕截图中的默认值是这样的。通过阅读您正在使用的代码的文档来找出要传递的正确值。When you create a new instances of an object, you are calling its constructor method (constructor for short). It is like any other method, but it is required that you call it, if it is defined. The syntax is
new MyObject(... parameters here ...);
.I searched through your previous questions and found a screenshot of the code you are using. You have a constructor that takes parameters. These parameters are not defined as optional, so you must provide them. These are
vendorId
,productId
, andhibernationEnabled
.So your code will be
new PS3RemoteDevice(0x054c, 0x0306, false);
. Or at least that's what the default values in the screenshot looked like. Figure out the right values to pass by reading the docs for the code you're using.