Initialization order

B

Boni

Dear all,
what is initialization order of members in VB?
Sometimes following assertion fails. Why?
Thank you in advance,
Boni
class A
inherits class B
dim pen as new system.drawing.pen(.)


sub new(.)
mybase.new(.)
debug.assert(not pen is nothing,"ops")
end sub
end class
 
G

Guest

first the base class then the constructor , and then the members


so safest is to do this


sub new(.)
mybase.new(.)
dim pen as new system.drawing.pen(.)
debug.assert(not pen is nothing,"ops")
end sub


or

private pen as system.drawing.pen()
sub new(.)
mybase.new(.)
pen = new system.drawing.pen()
debug.assert(not pen is nothing,"ops")
end sub


ofcourse there are more possibility`s depending on your situation

michel posseth
 
B

Boni

Hi,
I have a strange situation. I have a class with many constructors,
and a private member pen=new pen.
The call to draw function happens, when constructors is completely
finished.
But pen is nothing. If I initialize pen in constructor it does not happen.
Any ideas?
Thank you very much for your help.
 
B

Boni

More code..



Friend Class A

Inherits B

Private m_pen As New System.Drawing.Pen(System.Drawing.Color.Beige) 'color
is dummy ! It is changed it time

Public Overrides Sub Draw()

Debug.Assert(Not Me.m_pen Is Nothing, "pen is nothing")

If Me.m_pen Is Nothing Then

Me.m_pen = New System.Drawing.Pen(System.Drawing.Color.Beige)

End If



End Sub

Public Sub New(ByVal id As intgere)

MyBase.New("aaaaa",id)

End Sub



Public Sub New()

MyBase.New()

End Sub

End Class
 
B

Boni

I am just decompiled the dll and now I am really confused. m_pen is created
not in all constructors!!!
I thought that if I initialize class member on the way like I did with pen,
it is declared in all constructors.
Please could somebody elaborate on this?
Public Sub New(....)

MyBase.New(..)

Return

End Sub

Public Sub New()

MyBase.New()

_m_pen = New Pen(Color.Beige)

Return

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top