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
165 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
  • 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

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…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

816 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now