|
-
Oct 19th, 2003, 12:18 PM
#1
Thread Starter
Hyperactive Member
PROTECTED variable question
Question: when to declare a variable as "PROTECTED" .
I created a class called as "Employee" and declared a variable as
Protected mFirstName.
After I instantiated an object of the type "Employee" class, I can't use the variable.
The error is "Employee.mFirstname is not accessible in this context because it is 'Protected'. It works if i change the scope of the mFirstName to 'Public'.
But according to the definitions I read in the books, 'Protected' variables can be accessed from Class or derived classes.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim clsBasam As clsFHLBEmployee
clsBasam = New clsFHLBEmployee("Basam Nath")
MsgBox("The Firsname of the basam is " & clsBasam.mFirstname)
End Sub
Public Class clsFHLBEmployee
Protected mFirstname As String
Public Property FirstName() As String
Get
Return mFirstname
End Get
Set(ByVal Value As String)
mFirstname = Value
End Set
End Property
Public Sub New(ByVal firstname As String)
'MsgBox("The constructor of the clsFHLBEmployee")
Me.FirstName = firstname
End Sub
-
Oct 19th, 2003, 12:54 PM
#2
PowerPoster
Your answer lies in your post:
'Protected' variables can be accessed from Class or derived classes.
This means that your protected variable is available within the class it is declared, or if another class inherits the class, it can be accessed from there. No where else.
What you are doing in your code is instantiating an object of type employee, but you can't access a protected variable that way.
Code:
Public Class clsFHLBEmployee
Protected mFirstname As String
Public Property FirstName() As String
Get
Return mFirstname
End Get
Set(ByVal Value As String)
mFirstname = Value
End Set
End Property
Public Sub New(ByVal firstname As String)
'MsgBox("The constructor of the clsFHLBEmployee")
Me.FirstName = firstname
End Sub
End Class
Public Class clsInheritedEmployee
Inherits clsFHLBEmployee
'This class can access the protected member because it inherits your employee class.
End Class
Since you have a property that exposes the variable anyway, why not just use it?
MsgBox("The Firsname of the basam is " & clsBasam.FirstName)
-
Oct 19th, 2003, 02:20 PM
#3
Thread Starter
Hyperactive Member
thank you so much....so there is a difference between
Now I realized that there is a difference between 'instantiate' and 'inherit'.
I was under the impression that when you instantiate an object (of a particular class), it will inherit all the members of the class. There is truth to some extent in that statement. You will inherit all the public members only when you instantiate. But not members declared as 'PROTECTED'.
If you want to use a 'PROTECTED' member from a base class you can't use that member by instantiating an object of that class but by Inheriting from the base class.
correct?
thanks for your replies...
nath
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
|