Get And Set Public Property's

Hello, I got a question that I think is fairly dumb but I just cant figure it out. I have a class called users and in that class I have inserted some public properties that I would like all of my forms to be able to access, note: Im developing in Pocket PC. So my class looks like this


'Stores information about the user that is currently logged on.
Imports System


Public Class User


    'Internal Fields

    Dim m_uID As Integer
    Dim m_uUserName, m_uFullName, m_uEmail, m_uPassword As String


    'Properties

    Public Sub SetUserInfo(ByVal sUserID As Integer, ByVal sUserName As String, ByVal sUserFullName As String, ByVal sUserEmail As String, ByVal sUserPassword As String)
        UserID = sUserID
        UserName = sUserName
        FullName = sUserFullName
        EMail = sUserEmail
        Password = sUserPassword
    End Sub

    Public Property UserID() As Integer
        Get
            Return m_uID
        End Get
        Set(ByVal Value As Integer)
            m_uID = Value
        End Set
    End Property

    Public Property UserName() As String
        Get
            Return m_uUserName
        End Get
        Set(ByVal Value As String)
            m_uUserName = Value
        End Set
    End Property

    Public Property FullName() As String
        Get
            Return m_uFullName

        End Get
        Set(ByVal Value As String)
            m_uFullName = Value
        End Set
    End Property

    Public Property EMail() As String
        Get
            Return m_uEmail
        End Get
        Set(ByVal Value As String)
            m_uEmail = Value
        End Set
    End Property

    Public Property Password() As String
        Get
            Return m_uPassword
        End Get
        Set(ByVal Value As String)
            m_uPassword = Value
        End Set
    End Property

    Public Function clear()
        m_uID = 0
        m_uUserName = String.Empty
        m_uFullName = String.Empty
        m_uEmail = String.Empty
        m_uPassword = String.Empty

    End Function


Now I have a function that calls a webservice and get's a dataset sent back, I then*space
space*down the dataset and call upon the above class to store my information but I'm doing something wrong cause I'm not able to access the values and I'm not getting an error message either. Any help would be appreciated. thanks.


Public Class Authentication

    Dim AuthService As New AuthService.AuthService
    Dim user As New User


    Public Function AuthenticateUser(ByVal userName As String, ByVal userPassword As String) As Boolean

        Try
            Dim dsUserInfo As DataSet
            dsUserInfo = AuthService.AuthenticateUser(userName, userPassword)
            If dsUserInfo.Tables.Count = 0 Then
                Return False
            Else
                'The provided userName and Password were validated by the web Service
                'and it has returned us additional user information, let use the user class
                'to store these variable; so that if any other forms require them they are
                'easily avaliable.
                'Dim User As New User
                Dim uID As Integer
                Dim uFullName As String
                Dim uEMail As String

                With dsUserInfo.Tables(0).Rows(0)
                    uID = (.Item("UserID"))
                    uFullName = CStr(.Item("UserFullName"))
                    uEMail = CStr(.Item("UserEmail"))
                End With
                user.SetUserInfo(uID, userName, uFullName, uEMail, userPassword)
                Return True
            End If
        Catch ex As Exception

        End Try

    End Function

End Class





Answer this question

Get And Set Public Property's

  • Michael Wright

    Ok after some research I learned a tad bit more about this set and get features (very useful) and here is my new code which works at setting the default values but when I want to update them it does not work.
    The calling class is like this.

    Public Class Authentication

        Dim AuthService As New AuthService.AuthService



        Public Function AuthenticateUser(ByVal userName As String, ByVal userPassword As String) As Boolean

            Try
                Dim dsUserInfo As DataSet
                dsUserInfo = AuthService.AuthenticateUser(userName, userPassword)
                If dsUserInfo.Tables.Count = 0 Then
                    Return False
                Else
                    'The provided userName and Password were validated by the web Service
                    'and it has returned us additional user information, let use the user class
                    'to store these variable; so that if any other forms require them they are
                    'easily avaliable.
                    Dim uID As Integer
                    Dim uFullName As String
                    Dim uEMail As String

                    With dsUserInfo.Tables(0).Rows(0)
                        uID = Convert.ToInt32(.Item("UserID"))
                        uFullName = CStr(.Item("UserFullName"))
                        uEMail = CStr(.Item("UserEmail"))
                    End With
                    Dim m_User As New User
                    m_User.SetUserInfo(uID, userName, uFullName, uEMail, userPassword)
                    Return True
                End If
            Catch ex As Exception
                MessageBox.Show(ex.Message.ToString)
            End Try

        End Function

    End Class



    And the receiving class that is to set the values looks like this



    Imports System


    Public Class User


        'Internal Fields

        Private m_uID As Integer
        Private m_uUserName As String
        Private m_uFullName As String
        Private m_uEmail As String
        Private m_uPassword As String



        'Properties
        Public Sub SetUserInfo(Optional ByVal sUserID As Integer = 0, Optional ByVal sUserName As String = "jdoe", Optional ByVal sUserFullName As String = "john Doe", Optional ByVal sUserEmail As String = "someone@gSolutions.com", Optional ByVal sUserPassword As String = "password")
            m_uID = sUserID
            m_uUserName = sUserName
            m_uFullName = sUserFullName
            m_uEmail = sUserEmail
            m_uPassword = sUserPassword
        End Sub

        Public Sub New()
            SetUserInfo()
        End Sub

        Public Property UserID() As Integer
            Get
                Return m_uID
            End Get
            Set(ByVal Value As Integer)
                m_uID = Value
            End Set
        End Property

        Public Property UserName() As String
            Get
                Return m_uUserName
            End Get
            Set(ByVal Value As String)
                m_uUserName = Value
            End Set
        End Property

        Public Property FullName() As String
            Get
                Return m_uFullName

            End Get
            Set(ByVal Value As String)
                m_uFullName = Value
            End Set
        End Property

        Public Property EMail() As String
            Get
                Return m_uEmail
            End Get
            Set(ByVal Value As String)
                m_uEmail = Value
            End Set
        End Property

        Public Property Password() As String
            Get
                Return m_uPassword
            End Get
            Set(ByVal Value As String)
                m_uPassword = Value
            End Set
        End Property

        Public Function clear()
            m_uID = 0
            m_uUserName = String.Empty
            m_uFullName = String.Empty
            m_uEmail = String.Empty
            m_uPassword = String.Empty

        End Function



    End Class



    Still no luck with saving these values; so that they may be consumed by other forms. I do apreacite you the reader taking your time and any input is greatly appreciated. 

  • Bruno Of France

    First off I would like to thank you for helping me out. I read your comments and they make some sence about me creating a new instance for the user class you are absolutely right; what is happening is that on my frmMain im creating the first instance of the user class. Then when the user click on the login button the authenticate class is called which after receiving a valid dataset from the web service creates another instance of the user class, then a true boolean value is returned to the frmMain which then pull the user.fullName value. below is the code.

    FrmMain login button click event.

    frameactions.WaitCursor(True)
            AuthSuccessful = Authentication.AuthenticateUser(Me.txtUserName.Text, Me.txtUserPassword.Text)
            If AuthSuccessful = True Then
                frameactions.WaitCursor(False)
                Me.lblMessages.Text = "User: " & m_user.FullName & " logged in"
                ChangeOnlineStatus("online")
                Me.MenuItem_SignOut.Enabled = True
                Me.MenuItem_MyStatus.Enabled = True
                Me.MenuItem_Deliveries.Enabled = True
                frameactions.showPnl(SplashPanel, True)
            Else
                frameactions.WaitCursor(False)
                frameactions.ShowlblMsg(lblLoginMsg, "Invalid Sign in name or Password")
                frameactions.showPnl(LoginPanel, True)
            End If


    Authenticate class


    Public Class Authentication

        Dim AuthService As New AuthService.AuthService



        Public Function AuthenticateUser(ByVal userName As String, ByVal userPassword As String) As Boolean

            Dim dsUserInfo As DataSet
            dsUserInfo = AuthService.AuthenticateUser(userName, userPassword)

            If dsUserInfo.Tables(0).Rows.Count = 0 Then
                Return False
            Else
                'The provided userName and Password were validated by the web Service
                'and it has returned us additional user information, let use the user class
                'to store these variable; so that if any other forms require them they are
                'easily avaliable.
                Dim uID As Integer
                Dim uFullName, uEmail As String

                With dsUserInfo.Tables(0).Rows(0)
                    uID = Convert.ToInt32(.Item("UserID"))
                    uFullName = CStr(.Item("UserFullName"))
                    uEMail = CStr(.Item("UserEmail"))
                End With

                Dim user As New User(uID, userName, uFullName, uEmail, userPassword)

                Return True
            End If


        End Function

    End Class


    and finally the user class


    Imports System


    Public Class User


        'Internal Fields
        Dim m_uID As Integer
        Dim m_uUserName As String
        Dim m_uFullName As String
        Dim m_uEmail As String
        Dim m_uPassword As String

        'Properties
        Public Sub SetUserInfo(Optional ByVal sUserID As Integer = 1, Optional ByVal sUserName As String = "martinge", Optional ByVal sUserFullName As String = "John Doe", Optional ByVal sUserEmail As String = "martinge@symbol.com", Optional ByVal sUserPassword As String = "newlife04")

            m_uID = sUserID
            m_uUserName = sUserName
            m_uFullName = sUserFullName
            m_uEmail = sUserEmail
            m_uPassword = sUserPassword

        End Sub


        Public Sub New()
            SetUserInfo()
        End Sub

        Public Sub New(ByVal sUserID As Integer, ByVal sUserName As String, ByVal sUserFullName As String, ByVal sUserEmail As String, ByVal sUserPassword As String)

            UserID = sUserID
            UserName = sUserName
            FullName = sUserFullName
            EMail = sUserEmail
            Password = sUserPassword

        End Sub

        Public Property UserID() As Integer
            Get
                Return m_uID
            End Get
            Set(ByVal Value As Integer)
                m_uID = Value
            End Set
        End Property

        Public Property UserName() As String
            Get
                Return m_uUserName
            End Get
            Set(ByVal Value As String)
                m_uUserName = Value
            End Set
        End Property

        Public Property FullName() As String
            Get
                Return m_uFullName

            End Get
            Set(ByVal Value As String)
                m_uFullName = Value
            End Set
        End Property

        Public Property EMail() As String
            Get
                Return m_uEmail
            End Get
            Set(ByVal Value As String)
                m_uEmail = Value
            End Set
        End Property

        Public Property Password() As String
            Get
                Return m_uPassword
            End Get
            Set(ByVal Value As String)
                m_uPassword = Value
            End Set
        End Property

        Public Function clear()
            m_uID = 0
            m_uUserName = String.Empty
            m_uFullName = String.Empty
            m_uEmail = String.Empty
            m_uPassword = String.Empty

        End Function



    End Class


    now my question is should i declare the user class global and then create the instance in the frm main, if so then would I be able to access the values of user from anywhere whithin the application  Thanks for your help. have a good day.

  • sernst

    First off since you have a try statement there and you are doing nothing with the exception that is thrown then what error message would you ever expect

    msgbox(ex.tostring) 

    would be useful.

    Now the other issue is I don't see where you are trying to use the user variable other then setting it. It should set fine unless you got some nulls in the table that is returned.


  • duckling

    Yes, in the frm. That way you can use that information again

  • Nigel Watling

    Ok, I don't think anything is wrong with your user class. I don't think anything is wrong with your way of setting the properties.

    The problem lies in a couple different area's.

    1) Scoping, you see since you created the user object within the function, then when the function is done your instance of that class goes bye bye.

    2) Still haven't seen how you are trying to read the values from the User class  So I don't understand how you say they are not getting set. THough I'm thinking something is a foot here. I hate assuming something but here I go. You are creating another instance nad then trying to get the values (somewhere else I assume). This will not work, it's like when you setup a String in a sub routine, you can't use that same string in another sub routine unless it's passed as an argument. And when you create new instances, you are creating a new copy of that class in memory. There for it won't have any data. You need to have User declared more global to the class you intend to use it in.

    Private m_User as new User Though I see another problem is you don't give access to the class that calls your authenticate class to the User variable. Doing up a property for m_user would really be helpful. Though keep in mind scoping, once the authentication class you are calling from some other class, if it goes out of scope then boom so does the m_user. So you really need something on the class that call's the authentication class to store the m_user there globally. I hope this is all making sense.


  • Get And Set Public Property's