Thursday, May 17th 2012, 6:52pm UTC+2

You are not logged in.

  • Login
  • Register

A1880

Intermediate

Posts: 500

Location: Hanover, Germany

Occupation: Software Engineer

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

Posts: 277

Location: Germany

Occupation: software engineer

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

Posts: 277

Location: Germany

Occupation: software engineer

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

Posts: 500

Location: Hanover, Germany

Occupation: Software Engineer

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