Click here to Skip to main content
15,897,315 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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'
...

VB.NET
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
...
VB.NET
Dim lastClick As New MenuLogItem(Me.value, DateTime.UtcNow)

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
...
VB.NET
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.
Posted
Updated 14-Aug-16 22:16pm

VB.NET
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
 
Share this answer
 
Comments
m.r.m.40 15-Aug-16 1:12am    
Yes, it worked,
Thanks,

What a mistake.
Maciej Los 15-Aug-16 4:38am    
5ed!
m.r.m.40 wrote:
when I try to assign value to its variables I fail


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.
VB.NET
Private _menuName As String

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:
VB.NET
Public Sub New(newMenuName As String,
               newEntranceDateTime As DateTime)
    _menuName = newMenuName 'WRONG: menuName = newMenuName
    _formEntrance = newEntranceDateTime 'WRONG: formEntrance = newEntranceDateTime
End Sub


You should avoid setting a property inside class constructor. Instead of it, use internal variables.
Please, read this:
Constructor Design[^]
Object-Oriented Programming in Visual Basic .NET[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900