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
164 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Showdialog 8 29
Can Amazon AppStream 2.0 be used for VB.NET and VB6 applications? 2 79
Receiving a string from a WebService Push 21 37
Get Top Visible Row of Datagridview 6 27
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…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
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.

867 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

21 Experts available now in Live!

Get 1:1 Help Now