473,396 Members | 1,608 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Inheritence problem

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

Nov 20 '05 #1
7 1026

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.....
"M O J O" <mojo@_no_spam_delete_this_newwebsolutions.dk> schrieb im
Newsbeitrag news:ef**************@TK2MSFTNGP09.phx.gbl...
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

Nov 20 '05 #2
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 O J O" <mojo@_no_spam_delete_this_newwebsolutions.dk> schrieb im
Newsbeitrag news:ef**************@TK2MSFTNGP09.phx.gbl... 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

Nov 20 '05 #3
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


Captain Chaos wrote:
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.....
"M O J O" <mojo@_no_spam_delete_this_newwebsolutions.dk> schrieb im
Newsbeitrag news:ef**************@TK2MSFTNGP09.phx.gbl...
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



Nov 20 '05 #4
Ups .... I meant ...

Public Class Customor
Inherits MyBaseClass
Friend Shared Shadows _tableName As String = "customor"
End Class
....of course.
M O J O
M O J O wrote:
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


Nov 20 '05 #5
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.

Nov 20 '05 #6
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 :o)
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 :o)
End Sub

Public Shared Sub DeleteRow()
' do a lof of sql stuff like "DELETE FROM " & _tableName ...
' & " WHERE bla.bla."
' and 1000 more lines :o)
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! :o))

Thanks!!!! :o)

M O J O

Peter Huang wrote:
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.


Nov 20 '05 #7
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.
--------------------

Nov 20 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: John | last post by:
Hi, I am trying to create a class heirarchy similar to the following: // base interface class ICar { public: virtual void start() = 0; }; // add members to that interface, but retain base...
8
by: Digital Puer | last post by:
I made the following table to help me (re)learn inheritence basics. Can someone check if it's correct? This table requires a courier-like font. Java C++ ---- ...
5
by: john bailo | last post by:
For a c# web application, I created a user control that includes a form in control. The idea is, on the main Page, when the user clicks Submit from the master form, that makes the user control...
0
by: Jan Elbæk | last post by:
Hi, I would like to make a base form in my project - which (almost) all forms must inherit from. The baseform must have some visible elements (a toolbar, a topaligned panel and a picturebox and...
16
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class...
7
by: preetam | last post by:
Hi, This question is more towards design than towards c++ details. By looking at books on design patterns and various google threads on the same topic, I see that composition is favoured to...
11
by: Vincent van Beveren | last post by:
Hi everyone, I have the following code using inheritence The important part is the function getName(). The result should alert 'John Doe': function First(name) { this.name = name; ...
7
by: vj | last post by:
Hello Group, I am C++/OOP newbie and was working on a project when i came accross this puzzleing problem with inheritence. C++ Code ================== class Parent {
5
by: Neelesh Bodas | last post by:
This might be slightly off-topic. Many books on C++ consider multiple inheritence as an "advanced" concept. Bruce Eckel says in TICPP, volume 2 that "there was (and still is) a lot of...
4
by: arnaudk | last post by:
I have two unrelated types of data elements (objects) which I want to hold in two related types of containers, one for each element type. But this seems contradictory - for consistency, it seems that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.