Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在VB.NET中拥有一个类的两个实例_Vb.net_Winforms_Class_Variables - Fatal编程技术网

如何在VB.NET中拥有一个类的两个实例

如何在VB.NET中拥有一个类的两个实例,vb.net,winforms,class,variables,Vb.net,Winforms,Class,Variables,我已经创建了一个类,希望创建这个类的两个独立实例。我已经能够在web应用程序中实现这一点,但我正在尝试在windows窗体中实现这一点,并且由于某种原因,当我创建类的第二个实例时,它包含来自第一个实例的变量数据 我试图将这两个实例创建为全局变量,因为我希望它们能够被单独的子例程访问。这两个实例被声明为: Public Class Form1 Dim oTree1_Animation As New clsAnimation() Dim oTree2_Animation As New

我已经创建了一个类,希望创建这个类的两个独立实例。我已经能够在web应用程序中实现这一点,但我正在尝试在windows窗体中实现这一点,并且由于某种原因,当我创建类的第二个实例时,它包含来自第一个实例的变量数据

我试图将这两个实例创建为全局变量,因为我希望它们能够被单独的子例程访问。这两个实例被声明为:

Public Class Form1
    Dim oTree1_Animation As New clsAnimation()
    Dim oTree2_Animation As New clsAnimation()
然后,我尝试在一个子例程中填充该实例,该子例程在MouseDown事件中触发:

Private Sub PictureBox1_MouseDown(sender As System.Object, e As System.EventArgs) Handles PictureBox1.MouseDown
    Dim oFrame As New clsFrame(2, {0, -32}, {0, 0})
    Dim timTimer As New Timer()
    oTree1_Animation.Initialise(2, 100, oFrame, PictureBox1, timTimer)
    oTree1_Animation.AnimationStart()
End Sub
然后以类似的方式填充第二个:

Private Sub PictureBox2_MouseDown(sender As System.Object, e As System.EventArgs) Handles PictureBox2.MouseDown
    Dim oFrame As New clsFrame(2, {0, -32}, {0, 0})
    Dim timTimer As New Timer()
    oTree2_Animation.Initialise(2, 100, oFrame, PictureBox2, timTimer)
    oTree2_Animation.AnimationStart()
End Sub
该类如下所示:

Public Class clsAnimation
    Public Event Tick As EventHandler

    Public Shared FrameCount As Integer
    Public Shared FrameInterval As Integer
    Public Shared CurrentFrame As Integer = 0
    Public Shared FrameSet As clsFrame
    Public Shared Image As PictureBox
    Public Shared WithEvents Timer As Timer

    ''' <summary>
    ''' Creates an empty instance of the Animation class
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub New()
    End Sub

    ''' <summary>
    ''' Creates a new instance of the Animation class and preloads it with variables
    ''' </summary>
    ''' <param name="iFrameCount">Number of frames in this animation as Integer</param>
    ''' <param name="iFrameInterval">Frame transition speed (milliseconds) as Integer</param>
    ''' <param name="clsFrameSet">Frame information as clsFrame</param>
    ''' <param name="imgImage">The picturebox that we're animating as PictureBox</param>
    ''' <remarks></remarks>
    Public Sub Initialise(ByVal iFrameCount As Integer, ByVal iFrameInterval As Integer, ByVal clsFrameSet As clsFrame, ByRef imgImage As PictureBox, ByRef timTimer As Timer)
        FrameCount = iFrameCount
        FrameInterval = iFrameInterval
        FrameSet = clsFrameSet
        Image = imgImage
        Timer = timTimer

        Timer.Interval = FrameInterval
    End Sub
公共类clsAnimation
公共事件勾选为EventHandler
公共共享帧数为整数
公共共享帧间隔为整数
公共共享CurrentFrame作为整数=0
作为clsFrame的公共共享框架集
作为PictureBox的公共共享图像
公共共享事件计时器作为计时器
''' 
''创建动画类的空实例
''' 
''' 
公共分新()
端接头
''' 
''创建动画类的新实例并用变量预加载它
''' 
''此动画中的帧数为整数
''帧转换速度(毫秒)为整数
''帧信息作为clsFrame
''我们制作成picturebox动画的picturebox
''' 
公共子初始化(ByVal iFrameCount为整数,ByVal iFrameInterval为整数,ByVal clsFrameSet为clsFrame,ByRef imgImage为PictureBox,ByRef timTimer为定时器)
FrameCount=iframecont
FrameInterval=iFrameInterval
FrameSet=clsFrameSet
图像=图像
定时器=定时定时器
Timer.Interval=FrameInterval
端接头

但是,oTree1_动画和oTree2_动画共享相同的变量。我不知道我是否遗漏了一些小的东西,或者我只是试图以一种不可能的方式使用类,对此的任何帮助都将不胜感激。

您必须从所有类变量中删除
Shared
<代码>共享表示该类的所有实例之间共享值

这通常称为“静态”(但出于某些原因,VB.NET使用了不同的术语)


有关Microsoft的解释,请参见(感谢benjineer)。

您必须从所有类变量中删除
Shared
<代码>共享表示该类的所有实例之间共享值

这通常称为“静态”(但出于某些原因,VB.NET使用了不同的术语)


请参阅Microsoft的解释(谢谢benjineer)。

谢谢,我知道我遗漏了一些东西。然而,我已经将它们设置为shared,因为我希望类中的子例程能够访问这些变量,我该怎么做呢?编辑:我停止了正在共享的子例程,现在一切都像我预期的那样工作。。感谢您的帮助。Microsoft的类解释方法可以访问所有实例变量,除非它们本身是共享的。谢谢,我知道我缺少了一些东西。然而,我已经将它们设置为shared,因为我希望类中的子例程能够访问这些变量,我该怎么做呢?编辑:我停止了正在共享的子例程,现在一切都像我预期的那样工作。。感谢您的帮助。Microsoft的类解释方法可以访问所有实例变量,除非它们本身是共享的。