Solved

Need to inherit a class such that variables in the base class are determined in the inherited class

Posted on 2006-06-24
7
170 Views
Last Modified: 2010-04-23
The best way to explain this is with this watered-down example:

public class BaseClass
    public function getEncoded(Arg1 as string, Arg2 as string) as string
          return BCODE + ";" + Arg1 + ";" + Arg2
    end function
end class

public class Class1
     inherits BaseClass
     public const BCODE as string = "AABBCC"
end class


How do I do this?  (the example above gives an error, since BCODE doesn't exist in the base class)
0
Comment
Question by:stev0931
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 

Author Comment

by:stev0931
ID: 16976698
I know that in C++, you can declare something as virtual so that it knows it will be able to find it when the time comes.  Maybe there is an equivelant in VB.NET?
0
 

Author Comment

by:stev0931
ID: 16976730
Another thought: maybe there is a way to do this using interfaces?
0
 
LVL 13

Accepted Solution

by:
iHadi earned 250 total points
ID: 16976838

public class BaseClass
    public function getEncoded(BCODE as string, Arg1 as string, Arg2 as string) as string
          return BCODE + ";" + Arg1 + ";" + Arg2
    end function
end class

public class Class1
     inherits BaseClass
     public const BCODE as string = "AABBCC"
end class
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:stev0931
ID: 16976849
Thanks!
0
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 16976869

To avoid having to pass it as a parameter on the function, you could declare an overridable property BCODE on the base class.
Inheriting from it, you just had to override it and set you value.
On you example you use a Constant... you can create a ReadOnly property to simulate that.

Example:

    Public Overridable ReadOnly Property BCODE() As String
        Get
            Return String.Empty
        End Get
    End Property

    Public Function getEncoded(ByVal BCODE As String, ByVal Arg1 As String, ByVal Arg2 As String) As String
        Return BCODE + ";" + Arg1 + ";" + Arg2
    End Function

End Class

Public Class inheritedClass
    Inherits baseClass

    Public Overrides ReadOnly Property BCODE() As String
        Get
            Return "AABBCC"
        End Get
    End Property



Alex :p

0
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 16976872
Sorry... Copy/Paste error...
I left the BCODE parameter on the getEncoded finction...

This is the correct one:


    Public Overridable ReadOnly Property BCODE() As String
        Get
            Return String.Empty
        End Get
    End Property

    Public Function getEncoded(ByVal Arg1 As String, ByVal Arg2 As String) As String
        Return BCODE + ";" + Arg1 + ";" + Arg2
    End Function

End Class

Public Class inheritedClass
    Inherits baseClass

    Public Overrides ReadOnly Property BCODE() As String
        Get
            Return "AABBCC"
        End Get
    End Property
0
 

Author Comment

by:stev0931
ID: 16976890
Thanks Alex!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question