You are not logged in.

Dear visitor, welcome to Jabaco - Community. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

A1880

Intermediate

  • "A1880" is male
  • "A1880" started this thread

Posts: 500

Date of registration: Jan 1st 2009

Location: Hanover, Germany

Occupation: Software Engineer

Hobbies: Hilbert Curves

  • Send private message

1

Tuesday, April 13th 2010, 9:20pm

call the constructor of a base class in a derived sub-class

Is it possible to call the constructor of a base class in a derived sub-class in Jabaco?
In Java this would be done using "super()".

In general I found it difficult to write parametrized constructors in Jabaco.
It might be easier to live with parameter-less constructors and move the initialization stuff
to some init() method.

Greetings

A1880

OlimilO

Intermediate

  • "OlimilO" is male

Posts: 277

Date of registration: Jan 18th 2009

Location: Germany

Occupation: software engineer

  • Send private message

2

Tuesday, April 13th 2010, 11:00pm

Hi,

you could create the default constructor on your own.

class MyBase

Jabaco Source

1
2
3
4
5
6
7
8
9
10
11
12
Option Explicit
Dim myInt As Integer
'default constructor
Public Sub MyBase()
    myInt = 123
End Sub
Public Sub MyBase(i As Integer)
   myInt = i
End Sub
Public Function ToString() As String
    ToString = CStr(myInt)
End Function


class Derived superclass: MyBase

Jabaco Source

1
2
3
4
5
6
7
8
9
10
11
12
13
Option Explicit
Dim myName As String
'default constructor
Public Sub Derived
    Base.myInt = 456
    myName = "derived"
End Sub
Public Sub Derived(aName As String)
    MyName = aName
End Sub
Public Function ToString() As String
    ToString = Base.ToString & " - " & CStr(MyName)
End Function




regards

OlimilO

OlimilO

Intermediate

  • "OlimilO" is male

Posts: 277

Date of registration: Jan 18th 2009

Location: Germany

Occupation: software engineer

  • Send private message

3

Tuesday, April 13th 2010, 11:02pm

example

example

Jabaco Source

1
2
3
4
5
6
7
8
9
10
11
Dim B As New MyBase   
   MsgBox B.ToString
   
   B = New MyBase(789)
   MsgBox B.ToString
   
   Dim D As New Derived
   MsgBox D.ToString
   
   D = New Derived("things")
   MsgBox D.ToString




regards

OlimilO

A1880

Intermediate

  • "A1880" is male
  • "A1880" started this thread

Posts: 500

Date of registration: Jan 1st 2009

Location: Hanover, Germany

Occupation: Software Engineer

Hobbies: Hilbert Curves

  • Send private message

4

Wednesday, April 14th 2010, 9:00am

Yes, that solves the problem! Thanks for your example.

It is not sufficient to define a "Class_Initialize" in the base class or to leav out the default constructor at all.
You have to define your own default constructor. Otherwise Jabaco does not allow you to define constructors of derived classes.

But it is still not possible to call the constructor of a base class.
In your example, you circumvented this limitation by a simple "Base.MyInt = 456".
But for more complex base constructors it would be nice to write "super(456)".

Greetings

A1880

Rate this thread
WoltLab Burning Board