Hi Mojo,
Thanks for your quickly reply!
If you want to let the shared function in the parent class to access the
shared shadowed variable as follows:
Public Class MyBaseClass
Friend Shared _tableName As String
Public Shared Function GetSqlString As String
Return("SELECT * FROM " & _tableName)
End Sub
End Class
Public Class Customor
Friend Shared Shadows _tableName As String = "customor"
End Class
Public Sub Main
Dim cust As New Customor
' Below I want a MesBog to show "SELECT * FROM customor", but
' it doesn't. Instead it shows "SELECT * FROM ".
MsgBox(cust.GetSqlString)
End Sub
The MsgBox will show "SELECT * FROM", since the GetSqlString() will not
access variable in the derived class. You're not going to be able to get
obtain values in the base class that are declared within derived classes.
This is not how inheritance and specialization of classes is supposed to
work.
Can you tell me why you need to use the Shared Function and Shared
Variable? Do you want to maintain one copy in the memory?
If so, you may try to change the _tableName directly, since there is ONLY
one copy in the memory. You may take a look at the code below.
Module Module1
Public Class MyBaseClass
Friend Shared _tableName As String
Public Shared Function GetSqlString() As String
Return ("SELECT * FROM " & _tableName)
End Function
End Class
Public Class Customor
Inherits MyBaseClass
Public Sub New()
_tableName = "Customor"
End Sub
'Also you may try to write a shared function to achieve your aim. But you
need to call it explicitly.
End Class
Public Class Emplyee
Inherits MyBaseClass
Public Sub New()
_tableName = "Emplyee"
End Sub
'Also you may try to write a shared function to achieve your aim. But you
need to call it explicitly.
End Class
Sub Main()
Dim cu As New Customor
'In such case based on my test, there will be only one copy in the
memory
'cu.New() will change the _tableName to "Customor"
Console.WriteLine(Customor.GetSqlString)
Console.WriteLine(Emplyee.GetSqlString)
'The two lines will all write SELECT * FROM Customor
Dim em As New Emplyee
'In such case based on my test, there will be only one copy in the
memory
'cu.New() will change the _tableName to "Emplyee"
Console.WriteLine(Customor.GetSqlString)
Console.WriteLine(Emplyee.GetSqlString)
'The two lines will all write SELECT * FROM Emplyee
End Sub
End Module
Otherwise I think you may only try not to use the Shared Function and
Shared Variable. Here goes my demo code, you may have a look.
Public Class PolicyBase
Protected _tableName As String
Public Sub New()
_tableName = Me.GetType().Name()
End Sub
Public Function AddRow() As String
Return "INSERT INTO " + _tableName + " VALUES"
End Function
Public Function UpdateRow() As String
Return "UPDATE " + _tableName + " SET ... WHERE ..."
End Function
Public Function DeleteRow() As String
Return "DELETE FROM " + _tableName + " WHERE ..."
End Function
End Class
Public Class CarPolicy
Inherits PolicyBase
End Class
Public Class DogPolicy
Inherits PolicyBase
Public Sub New()
_tableName = "dogpolicy"
End Sub
End Class
Public Sub Main()
Dim CarPolicy As New CarPolicy
Dim Dogpolicy As New DogPolicy
Console.WriteLine(CarPolicy.AddRow)
'Print INSERT INTO CarPolicy VALUES
Console.WriteLine(Dogpolicy.DeleteRow)
'DELETE FROM dogpolicy WHERE ...
End Sub
If you have any concern on this issue, please post here.
Best regards,
Perter Huang
Microsoft Online Partner Support
Get Secure! -
www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------