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
163 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

11 Experts available now in Live!

Get 1:1 Help Now