I'm new to object oriented programming. I have two classes in class-modules:
我是面向对象编程的新手。我在类模块中有两个类:
ClassA
ClassA的
Public Time As Double
ClassB
ClassB的
Public Sub Progress()
ClassAObject.Time = 5
End Sub
This is the code running in the subroutine
这是在子例程中运行的代码
Public Sub Progressing()
Dim ClassAObject As New ClassA
Dim ClassBObject As New ClassB
Call ClassBObject.Progress
End Sub
The subroutine ClassBObject.Progress
seem to not be able to access ObjectA
. I think how I refer to the object might be wrong. How do I fix this?
子例程ClassBObject.Progress似乎无法访问ObjectA。我认为我如何引用该对象可能是错误的。我该如何解决?
2 个解决方案
#1
3
Class B does not know of class A. There are many ways to do this, but one way is to make the .Progress()
sub take an input.
B类不知道A类。有很多方法可以做到这一点,但一种方法是使.Progress()子接受输入。
Code in ClassA:
ClassA中的代码:
Public Time As Double
Code in ClassB:
ClassB中的代码:
Public Sub Progress(ClassAObject As ClassA)
'Assuming that you have a .Time property in ClassAObject
ClassAObject.Time = 5
End Sub
Code in Main:
主要代码:
Sub Main()
Dim ClassAObject As New ClassA
Dim ClassBObject As New ClassB
ClassBObject.Progress ClassAObject
End Sub
#2
2
You could pass an instance of ClassA
to the Progress
method. So for example:
您可以将ClassA的实例传递给Progress方法。例如:
Change Progress
method to:
将进度方法更改为:
Public Sub Progress(a As ClassA)
a.Time = 5
End Sub
And call it like this:
并称之为:
Public Sub Progressing()
Dim ClassAObject As New ClassA
Dim ClassBObject As New ClassB
Call ClassBObject.Progress(ClassAObject) ' <--- pass instance of ClassA
End Sub
I would recommend giving your classes and objects more descriptive names, even if you're just practicing. It will make it easier for you and others to read and understand your code. For example, Dim apple As New Fruit
is always easier to understand than Dim ClassAObject As New ClassA
.
我建议给你的课程和对象更具描述性的名字,即使你只是练习。它将使您和其他人更容易阅读和理解您的代码。例如,Dim apple As New Fruit总是比Dim ClassAObject As New ClassA更容易理解。
#1
3
Class B does not know of class A. There are many ways to do this, but one way is to make the .Progress()
sub take an input.
B类不知道A类。有很多方法可以做到这一点,但一种方法是使.Progress()子接受输入。
Code in ClassA:
ClassA中的代码:
Public Time As Double
Code in ClassB:
ClassB中的代码:
Public Sub Progress(ClassAObject As ClassA)
'Assuming that you have a .Time property in ClassAObject
ClassAObject.Time = 5
End Sub
Code in Main:
主要代码:
Sub Main()
Dim ClassAObject As New ClassA
Dim ClassBObject As New ClassB
ClassBObject.Progress ClassAObject
End Sub
#2
2
You could pass an instance of ClassA
to the Progress
method. So for example:
您可以将ClassA的实例传递给Progress方法。例如:
Change Progress
method to:
将进度方法更改为:
Public Sub Progress(a As ClassA)
a.Time = 5
End Sub
And call it like this:
并称之为:
Public Sub Progressing()
Dim ClassAObject As New ClassA
Dim ClassBObject As New ClassB
Call ClassBObject.Progress(ClassAObject) ' <--- pass instance of ClassA
End Sub
I would recommend giving your classes and objects more descriptive names, even if you're just practicing. It will make it easier for you and others to read and understand your code. For example, Dim apple As New Fruit
is always easier to understand than Dim ClassAObject As New ClassA
.
我建议给你的课程和对象更具描述性的名字,即使你只是练习。它将使您和其他人更容易阅读和理解您的代码。例如,Dim apple As New Fruit总是比Dim ClassAObject As New ClassA更容易理解。