Inheritence problem

M

M O J O

Hi,

I have a baseclass and several clases that inhereits from this
baseclass. In the inherited class I need to change a variable that is
used in the base class, but I cant figure out how.

Here's an example....

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

And to test the classes...

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


I now realize that shadowing _tableName in my Customor class is only
visilbe to the Customor class, but how can I somehow tell the baseclass
to user the overwrited _tableName variable?

Any simple solution/suggestions?

Thanks!!!!!!!

M O J O
 
C

Captain Chaos

To access the BaseClass use the KeyWord "BaseClass" in your code

Example:
BaseClass.Color= blue

And only use the KeyWord "shared" if you realy know what you are doing,
otherwise you will get into deep trouble.....
If you have tried to solve the Problem with "shared" then put it out.....
 
C

Captain Chaos

visilbe to the Customor class, but how can I somehow tell the baseclass
to user the overwrited _tableName variable?

Normaly this problem doesn't occur.

Don't use shared...... Maybe it has someting to to with it, even if it's not
it's not necessary.

Maybe make a Property out of Tablename to set the Tabelname with Baseclass
Keyword.

But Normaly that's all not neccesary and the correct _Tablename is used.
 
M

M O J O

Hi,

Thank you for answering my post.

My class is only using shared subs, so if I use BaseClass.SomeThing, I
must change my class to a singleton right (so BaseClass._tableName can
be initialized in the Sub New event)? .... I can't do like this...

Public Class Customor
Inherits MyBaseClass
BaseClass _tableName = "customor"
..
End Class

Thanks!

M O J O
 
M

M O J O

Ups .... I meant ...

Public Class Customor
Inherits MyBaseClass
Friend Shared Shadows _tableName As String = "customor"
End Class


....of course.


M O J O
 
P

Peter Huang

Hi Mojo,

Thanks for using Microsoft MSDN Managed Newsgroup. My name is Peter, and I
will be assisting you on this issue.

First of all, I would like to confirm my understanding of your issue.
From your description, I understand that you wants to define a shared
variable and a shared function(the function will access the shared
variable) in a baseclass. and then inherits a class from baseclass, in your
inheritant class, you want to change the shared variable in baseclass.
Have I fully understood you? If there is anything I misunderstood, please
feel free to let me know.

I think you may try to shadows the GetSqlString function.
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
Friend Shared Shadows _tableName As String = "Customor"
Public Shared Shadows Function GetSqlString() As String
Return ("SELECT * FROM " & _tableName)
End Function
End Class
Sub Main()
Dim cs As New Customor
Console.WriteLine(Customor.GetSqlString)
End Sub
End Module

Please Apply My Suggestion Above And Let Me Know If It Helps Resolve Your
Problem.

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.
 
M

M O J O

Hi Peter,

Thank you for answering my post.

If I use shadows, I need to rewrite the whole base subs.

Maybe the GetSqlString was a bad example. Take this similar example
instead...

Public Class PolicyBase
Friend Shared _tableName As String

Public Shared Sub AddRow()
' do a lof of sql stuff like "INSERT INTO " & _tableName & ...
' " (sometext) VALUES ('Just a test')"
' and 1000 more lines :blush:)
End Sub

Public Shared Sub UpdateRow()
' do a lof of sql stuff like "UPDATE " & _tableName & ...
' " SET sometext = 'Just a test' WHERE bla.bla."
' and 1000 more lines :blush:)
End Sub

Public Shared Sub DeleteRow()
' do a lof of sql stuff like "DELETE FROM " & _tableName ...
' & " WHERE bla.bla."
' and 1000 more lines :blush:)
End Sub
End Class


Public Class CarPolicy
Inherits PolicyBase

' Here I need to tell my base class, that _tableName = "car_policy",
' but how?
End Class


Public Class DogPolicy
Inherits PolicyBase

' Here I need to tell my base class, that _tableName = "dog_policy",
' but how?
End Class


Module Module1

Public Sub Main()
Dim CarPolicy As CarPolicy
Dim Dogpolicy As DogPolicy

' Next I want to add a row to my carpolicy without having to
' specify here, that the table to use is "car_policy"
CarPolicy.AddRow()

' Next I want to delete a row to my dogpolicy without having to
' specify here, that the table to use is "dog_policy"
Dogpolicy.DeleteRow()
End Sub

End Module


My problem is, that since I use shared subs in my baseclass, I never get
to set the _tableName from within my inhertied classes.

Boy it's hard to explain in english ... I would prefer explaining myself
in danish! :blush:))

Thanks!!!! :blush:)

M O J O
 
P

Peter Huang

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.
--------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top