Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
174 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 750 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Screencast - Getting to Know the Pipeline
Suggested Courses

971 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