|
-
Jul 13th, 2011, 04:09 AM
#1
Thread Starter
Addicted Member
Class Public Property or Sub/Function?
Hi guys
I was wondering if someone could explain to me the differences between these 2 ways of accessing a class property.
Access Way : 1 (To me this looks like im declaring the property of the class as a Global variable which can be changed anywhere
Code:
Dim username As String
Public Property user() As String
Get
Return username
End Get
Set(ByVal value As String)
username = value.ToUpperInvariant()
End Set
End Property
Access Way 2: (This to me looks like the more secure way of doing the same thing.)
Code:
Dim username as String
Public Sub setUsername(ByVal user as String)
username = user
End Sub
Public Function returnUserName() as String
return username
End Function
I would just like to know when and what is the differences between the 2 methods and what is the security risks of each if possible please.
Kind Regards
Barra.
-
Jul 13th, 2011, 04:13 AM
#2
Re: Class Public Property or Sub/Function?
Here is what M$ suggests.
-
Jul 13th, 2011, 04:18 AM
#3
Thread Starter
Addicted Member
Re: Class Public Property or Sub/Function?
Grimfort many thanks for the prompt reply, I have looked into the MSDN article about the differences but, I tend to struggle with all the technical terms they use lol Im quite new to classes so really all i need is someone to break that down into laymen,s terms
Like so :
Use the public property if you need to access the property directly globally
Use the Sub/Function if you need to secure it so theres only 1 way in and 1 way out of getting your property value.
I know these are probably wrong lol Its just an example 
Many Thanks
Barra.
-
Jul 13th, 2011, 04:31 AM
#4
Re: Class Public Property or Sub/Function?
Let me extract the bit that is interesting:
Code:
•Use a method when:
•The operation is a conversion, such as Object.ToString. <<<< So in your example, same as doing Return Val(username)
•The operation is expensive enough that you want to communicate to the user that they should consider caching the result. << so in your example, it could be going to a database to get the username based upon a saved userid
•Obtaining a property value using the get accessor would have an observable side effect. <<< so reading your username, might also clear the username from your local variable
•Calling the member twice in succession produces different results. <<< in your example, it might be the username AND the current datetime added onto the end which changes every time you call it
•The order of execution is important. Note that a type's properties should be able to be set and retrieved in any order. <<< so setting your username and password at the same time, might cause issue if you changed them seperately and they were used by something else at the same time on a different thread (like a forms timer)
The others are less interesting
....
Heres some examples:
Code:
Private username As String
Public Property user() As String
Get
Return username
End Get
Set(ByVal value As String)
username = value
End Set
End Property
Public ReadOnly Property user() As String
Get
Return username
End Get
End Property
Public ReadOnly Property user() As String
Get
Return username.ToUpper '<< not a conversation, as it is still a string
End Get
End Property
Or functions
Code:
Public Function userAsUpper() As String
Return username.ToUpper '<< it could be a property, there is hardly any point in arguing over it however :)
End Function
Public Sub SetUserDetails(TheUserName As String, ThePassWord As String)
Me.username = TheUserName
Me.password = ThePassWord
End Sub
Public Function userFormatted() As String
Return username & " - " & Now.ToString("dd MMM yyyy hh:mm:ss") '<< changes every time you call it
End Function
Last edited by Grimfort; Jul 13th, 2011 at 04:39 AM.
-
Jul 13th, 2011, 04:44 AM
#5
Thread Starter
Addicted Member
-
Jul 13th, 2011, 04:56 AM
#6
Re: Class Public Property or Sub/Function?
Yup, you might for example use the SetUserDetails to write both username and password at the same time, but only let the user read both back through readonly properties. This will force the caller to only user your method to update. To make that scenario more complete, it could be like this:
Code:
Private UserName As String
Private Password As String
'These properties will only have values when a login is succesful
Public ReadOnly Property user() As String
Get
Return username
End Get
End Property
Public ReadOnly Property pwd() As String
Get
Return Password
End Get
End Property
Public Function Login(UserName As String, Password As String) As Boolean
Try
'Go do a database login using the passed params
dbOjbect.User = UserName
dbObject.Pwd = Password
dbObject.DoConnection()
'Now we are connected we update the local vars so if anyone asks, we can tell them who is logged on
Me.UserName = UserName
Me.Password = Password
Return True
Catch ex As Exception
'Failed to login for x reason
Return False
End Try
End Function
Last edited by Grimfort; Jul 13th, 2011 at 05:01 AM.
-
Jul 13th, 2011, 05:16 AM
#7
Thread Starter
Addicted Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|