Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Properties...How to setup private setting but public getting

Status
Not open for further replies.

gharabed

Programmer
Sep 7, 2001
251
US
I have a class that has three private attributes:

empID, LastName, FirstName

I want to set the LastName and FirstName attributes based on a database query of the empID value that is set and don't want anything outside the class to be able to set the LastName and firstName attributes. But I want any calling code to be able to get the ID, LastName, or FirstName attributes. I was going to set these up as properties but I'm not sure how I would set up the properties such that the ID attribute could be set/read by any caller (public), but the LastName, FirstName values could only be retrieved. Again, the setting of the LastName and FirstName attributes would be set when the empID value is set...I am assuming through Set call to the empID property. Hopefully this makes sense. How would I set up something like this in my class?

 
Sounds to me list I would set up a class with three private variables. Each of these variables would have a public property exposed. The Name properties would be flagged as read only and the ID property would gather and populate the private variables for the name properties.

Something Like:

Code:
Private _ID As Integer
Private _FirstName As String
Private _LastName As String

    Public Property Id() As Integer
        Get
            Return _Id
        End Get
        Set(ByVal value As Integer)
            _Id = value
            ' method to gather data
            ' _LastName = from method somehow
            ' _FirstName = from method somehow
        End Set
    End Property

    Public ReadOnly Property FirstName() As String
        Get
            Return _FirstName
        End Get
    End Property

    Public ReadOnly Property LastName() As String
        Get
            Return _LastName
        End Get
    End Property

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
I'm no expert, but here's something to think about:

Public Class Class1
Private thisVariable As Object ''' Internal Object

''' Public Access = ReadOnly
Public ReadOnly Property PublicAccessable() As Object
Get
Return thisVariable
End Get
End Property

''' Private Read/Write Access
Private Property PrivateAccessable() As Object
Get
Return thisVariable
End Get
Set(ByVal Value As Object)
thisVariable = Value
End Set
End Property

''' Public Access = ReadOnly
Public Function ReadOnlyVariable() As Object
Return thisVariable
End Function

''' Setting the object w/ Public readonly access
Private Sub Set_thisVariable()
Dim obj As Object
thisVariable = obj
End Sub

''' Method versions (Get/Set)
Public Function Variable() As Object
Return thisVariable
End Function

Private Sub Variable(ByVal Value As Object)
thisVariable = Value
End Sub

End Class
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top