在VB OOP中为对象的属性赋值 [英] Assigning value to properties of an object in VB OOP
问题描述
我有一个课程,其中包含一些字段和属性,
问题是当我尝试为我的变量赋值我失败了,当我追踪它时,我看到的是我的代码的字段没有取任何值,
这是我的对象的代码名为'MenuLogItem'< br $> b $ b ...
Hi,
I'm Having a class which holds some fields and properties in it,
The problem is when I try to assign value to its variables I fail, and all I see when I trace it is that the fields of my code have not taken any values,
Here is the code of my object named 'MenuLogItem'
...
Public Class MenuLogItem
#Region "Fields"
Private _menuName As String
Private _formEntrance As DateTime
#End Region
#Region "Methods"
''' <summary>
''' Creates an object of MenuLogItem Class Which holds the information of the formEntrance
''' </summary>
''' <param name="menuName"></param>
''' <param name="EntranceDateTime"></param>
''' <remarks></remarks>
Public Sub New(newMenuName As String,
newEntranceDateTime As DateTime)
menuName = newMenuName
formEntrance = newEntranceDateTime
End Sub
''' <summary>
''' Stores the object of form entrance log into the database
''' </summary>
''' <remarks></remarks>
Public Sub Store()
'Me.menuName
'Me.formEntrance
'TODO write the code for saving the information
System.Windows.Forms.MessageBox.Show(Me.menuName.ToString() + vbCrLf +
Me.formEntrance.ToString() + vbCrLf +
"Stored.")
End Sub
#End Region
#Region "Properties"
''' <summary>
''' menuName Set Get
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property menuName() As String
Get
Return _menuName
End Get
Set(value As String)
_menuName = menuName
End Set
End Property
''' <summary>
''' formEntrance Get Set
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property formEntrance() As DateTime
Get
Return _formEntrance
End Get
Set(value As DateTime)
_formEntrance = formEntrance
End Set
End Property
#End Region
End Class
这是用于创建和填充此类对象的代码
...
Here is the code which is being used to create and fill one object of this class
...
Dim lastClick As New MenuLogItem(Me.value, DateTime.UtcNow)
可以说Me.vlaue是已经分配了menuItem7并且dateTime是Utc DateTime,
但是当我跟踪它并且光标在下面的行上时
...
Lets say the Me.vlaue is already assigned with "menuItem7" and the dateTime is the Utc DateTime,
But when I trace it and the cursor goes on the line below
...
Public Sub New(newMenuName As String,
newEntranceDateTime As DateTime)
menuName = newMenuName
formEntrance = newEntranceDateTime
End Sub
并通过它,我仍然没有看看我期望哪个是与当前值相关的新对象(属性没有得到任何值)。
问候,
我尝试了什么:
我还在努力,但我还没有没有得到任何答案。
and passes it, I still don't see what I expect which is the new object to be istanciated with the current values (the properties do not get any values).
Regards,
What I have tried:
I'm still working on it, but yet I haven't received any answer.
推荐答案
Public Property menuName() As String
Get
Return Me._menuName
End Get
Set(value As String)
'WRONG _menuName = menuName
Me._menuName = value
End Set
End Property
当我尝试为其变量赋值时我失败了
when I try to assign value to its variables I fail
那是因为变量可见性/范围。请阅读:
Microsoft .NET中的变量和方法范围 [ ^ ]
Visual Basic中的范围 [ ^ ]
您已申报 _menuName
作为私有成员,这意味着该变量仅在您的类中可见。
That's becuase of variable visibility/scope. Please, read this:
Variable and Method Scope in Microsoft .NET[^]
Scope in Visual Basic[^]
You have declared _menuName
as private member, which means that variable is visible only inside your class.
Private _menuName As String
如果要访问它,则必须将其可见性更改为public,或者必须定义方法/属性。你选择了一个属性,但它的setter试图访问你在类构造函数中声明的变量!请参阅 Gegniani [ ^ ]的回答。
我在类构造函数中发现的另一个问题:
If you want to access to it, you have to change its visibility to public or you have to define method/property. You've chosen a property, but its setter is trying to access to variable you've declared in class constructor! Please, see the Gegniani[^]'s answer.
Another issue i have found inside class constructor:
Public Sub New(newMenuName As String,
newEntranceDateTime As DateTime)
_menuName = newMenuName 'WRONG: menuName = newMenuName
_formEntrance = newEntranceDateTime 'WRONG: formEntrance = newEntranceDateTime
End Sub
您应该避免在类构造函数中设置属性。而不是它,使用内部变量。
请阅读:
构造函数设计 [ ^ ]
Visual Basic .NET中面向对象的编程 [ ^ ]
这篇关于在VB OOP中为对象的属性赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!