C# 创建一个实例

发布于 2024-11-25 07:15:57 字数 1633 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

↘人皮目录ツ 2024-12-02 07:15:57

假设您拥有其余的类代码,

PS3RemoteDevice PS3R = new PS3RemoteDevice(); 将使用括号。

Assuming you have the rest of the class code,

PS3RemoteDevice PS3R = new PS3RemoteDevice(); will work, with the parantheses.

拥抱影子 2024-12-02 07:15:57

尝试

PS3RemoteDevice PS3R = new PS3RemoteDevice();

编辑(带参数):

PS3RemoteDevice PS3R = new PS3RemoteDevice(someVendorID, some ProductID, someBoolHibernationEnabled);

try

PS3RemoteDevice PS3R = new PS3RemoteDevice();

EDIT (with params):

PS3RemoteDevice PS3R = new PS3RemoteDevice(someVendorID, some ProductID, someBoolHibernationEnabled);
原来是傀儡 2024-12-02 07:15:57

您的类只有一个带有三个参数的构造函数:

 public PS3RemoteDevice(int VendorID, int ProductID, bool HibernationEnabled) 
 { ... }

就像在任何其他方法调用中一样,当您使用 new 实例化对象时,您需要传递这些参数,例如:

int vendorId = 5;
int productId = 42;
PS3RemoteDevice PS3R = new PS3RemoteDevice(vendorId, productId, false);

Your class only has a constructor that takes three parameters:

 public PS3RemoteDevice(int VendorID, int ProductID, bool HibernationEnabled) 
 { ... }

Just like in any other method call you need to pass those parameters when you instantiate an object with new, for example:

int vendorId = 5;
int productId = 42;
PS3RemoteDevice PS3R = new PS3RemoteDevice(vendorId, productId, false);
比忠 2024-12-02 07:15:57

当您创建对象的新实例时,您正在调用其构造函数方法(简称构造函数)。它与任何其他方法一样,但需要您调用它(如果已定义)。语法为new MyObject(...此处的参数...);

我搜索了您之前的问题,找到了您正在使用的代码的屏幕截图。您有一个带有参数的构造函数。这些参数未定义为可选,因此您必须提供它们。它们是 vendorIdproductIdhibernationEnabled

因此您的代码将是 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, and hibernationEnabled.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文