inheritance question

  • Thread starter Thread starter Craig Buchanan
  • Start date Start date
C

Craig Buchanan

I have three classes:

Core Assembly
Application - creates instances of User using Activator.CreateInstance
Item - marked MustInherit; has an Init method

UserPlugin Assembly
User - Inherits Item, has an Init method

I would like to be able to call the Init method from the Application where
the Item.Init would fire before the User.Init method. Moreover, I don't
want the Init method on either class to be visible outside of the Core
assembly.

I don't want to use the New sub, as i don't always want the logic in the
Init to be run when an instance is created. I would like to hide the New
method from public use, but the CreateInstance method seems to require one.

I would appreciate some advice on this. Thanks.

Craig Buchanan
 
See if this sample does what you want:

Namespace MyCompany.Core

Public MustInherit Class Item
Protected mblnWasInited As Boolean

Protected Sub New()

Dim CallID As Integer = MyCompany.Common.AppHelper.GetNextMethodCall

Trace.WriteLineIf(MyCompany.Common.AppHelper.TracingOn,
String.Format("-----{0}:Begin MyCompany.Core.Item:{1}", CallID, "New()"))
mblnWasInited = False
Trace.WriteLineIf(MyCompany.Common.AppHelper.TracingOn,
String.Format("-----{0}:End MyCompany.Core.Item:{1}", CallID, "New()"))

End Sub

Protected Friend Overridable Sub Init()

Dim CallID As Integer = MyCompany.Common.AppHelper.GetNextMethodCall

Trace.Indent()
Trace.WriteLineIf(MyCompany.Common.AppHelper.TracingOn,
String.Format("{0}:Begin MyCompany.Core.Item:{1}", CallID, "Init()"))
Init(False)
Trace.WriteLineIf(MyCompany.Common.AppHelper.TracingOn,
String.Format("{0}:End MyCompany.Core.Item:{1}", CallID, "Init()"))
Trace.Unindent()

End Sub

Protected Friend Overridable Sub Init(ByVal SetFlag As Boolean)

Dim CallID As Integer = MyCompany.Common.AppHelper.GetNextMethodCall

Trace.Indent()
Trace.WriteLineIf(MyCompany.Common.AppHelper.TracingOn,
String.Format("{0}:Begin MyCompany.Core.Item:{1}", CallID, "Init(bool)"))
If SetFlag Then mblnWasInited = SetFlag
Trace.WriteLineIf(MyCompany.Common.AppHelper.TracingOn,
String.Format("{0}:End MyCompany.Core.Item:{1}", CallID, "Init(bool)"))
Trace.Unindent()

End Sub

End Class

End Namespace

Namespace MyCompany.Plugins
Public Class User
Inherits MyCompany.Core.Item

Public Sub New()
MyBase.new() 'Mybase.new has to be called before any other line of
code in the constructor

Dim CallID As Integer = MyCompany.Common.AppHelper.GetNextMethodCall

Trace.WriteLineIf(MyCompany.Common.AppHelper.TracingOn,
String.Format("{0}:Begin MyCompany.Plugins.User:{1}", CallID, "New()"))
'
'
'
Trace.WriteLineIf(MyCompany.Common.AppHelper.TracingOn,
String.Format("{0}:End MyCompany.Plugins.User:{1}", CallID, "New()"))
End Sub

Friend Shadows Sub Init()

Dim CallID As Integer = MyCompany.Common.AppHelper.GetNextMethodCall

Trace.WriteLineIf(MyCompany.Common.AppHelper.TracingOn,
String.Format("{0}:Begin MyCompany.Plugins.User:{1}", CallID, "Init()"))
Trace.Indent()
Trace.WriteLineIf(MyCompany.Common.AppHelper.TracingOn,
String.Format("{0}:Begin mybase:{1}", CallID, "Init()"))
MyBase.Init()
Trace.WriteLineIf(MyCompany.Common.AppHelper.TracingOn,
String.Format("{0}:End mybase:{1}", CallID, "Init()"))
Trace.WriteLineIf(MyCompany.Common.AppHelper.TracingOn,
String.Format("{0}:Begin local init logic", CallID))
'
' Custom implementation
'
'
Trace.Indent()
Trace.WriteLineIf(MyCompany.Common.AppHelper.TracingOn,
String.Format("{0}:Begin setting init flag", CallID))
mblnWasInited = True
Trace.WriteLineIf(MyCompany.Common.AppHelper.TracingOn,
String.Format("{0}:End setting init flag", CallID))
Trace.Unindent()
'
Trace.WriteLineIf(MyCompany.Common.AppHelper.TracingOn,
String.Format("{0}:End local init logic", CallID))
Trace.Unindent()
Trace.WriteLineIf(MyCompany.Common.AppHelper.TracingOn,
String.Format("{0}:End MyCompany.Plugins.User:{1}", CallID, "Init()"))
End Sub

End Class
End Namespace

Namespace MyCompany.Common
Friend Class AppHelper
Private Shared mTracingOn As Boolean
Private Shared mCallID As Integer

Shared Sub New()
mTracingOn = True
mCallID = Integer.MinValue
End Sub

Public Shared ReadOnly Property TracingOn() As Boolean
Get
Return mTracingOn
End Get
End Property

Public Shared Function GetNextMethodCall() As Integer
mCallID += 1
Return mCallID
End Function

End Class
End Namespace
 
thanks, i'll take a look and let you know.

AMDRIT said:
See if this sample does what you want:

Namespace MyCompany.Core

Public MustInherit Class Item
Protected mblnWasInited As Boolean

Protected Sub New()

Dim CallID As Integer = MyCompany.Common.AppHelper.GetNextMethodCall

Trace.WriteLineIf(MyCompany.Common.AppHelper.TracingOn,
String.Format("-----{0}:Begin MyCompany.Core.Item:{1}", CallID, "New()"))
mblnWasInited = False
Trace.WriteLineIf(MyCompany.Common.AppHelper.TracingOn,
String.Format("-----{0}:End MyCompany.Core.Item:{1}", CallID, "New()"))

End Sub

Protected Friend Overridable Sub Init()

Dim CallID As Integer = MyCompany.Common.AppHelper.GetNextMethodCall

Trace.Indent()
Trace.WriteLineIf(MyCompany.Common.AppHelper.TracingOn,
String.Format("{0}:Begin MyCompany.Core.Item:{1}", CallID, "Init()"))
Init(False)
Trace.WriteLineIf(MyCompany.Common.AppHelper.TracingOn,
String.Format("{0}:End MyCompany.Core.Item:{1}", CallID, "Init()"))
Trace.Unindent()

End Sub

Protected Friend Overridable Sub Init(ByVal SetFlag As Boolean)

Dim CallID As Integer = MyCompany.Common.AppHelper.GetNextMethodCall

Trace.Indent()
Trace.WriteLineIf(MyCompany.Common.AppHelper.TracingOn,
String.Format("{0}:Begin MyCompany.Core.Item:{1}", CallID, "Init(bool)"))
If SetFlag Then mblnWasInited = SetFlag
Trace.WriteLineIf(MyCompany.Common.AppHelper.TracingOn,
String.Format("{0}:End MyCompany.Core.Item:{1}", CallID, "Init(bool)"))
Trace.Unindent()

End Sub

End Class

End Namespace

Namespace MyCompany.Plugins
Public Class User
Inherits MyCompany.Core.Item

Public Sub New()
MyBase.new() 'Mybase.new has to be called before any other line of
code in the constructor

Dim CallID As Integer = MyCompany.Common.AppHelper.GetNextMethodCall

Trace.WriteLineIf(MyCompany.Common.AppHelper.TracingOn,
String.Format("{0}:Begin MyCompany.Plugins.User:{1}", CallID, "New()"))
'
'
'
Trace.WriteLineIf(MyCompany.Common.AppHelper.TracingOn,
String.Format("{0}:End MyCompany.Plugins.User:{1}", CallID, "New()"))
End Sub

Friend Shadows Sub Init()

Dim CallID As Integer = MyCompany.Common.AppHelper.GetNextMethodCall

Trace.WriteLineIf(MyCompany.Common.AppHelper.TracingOn,
String.Format("{0}:Begin MyCompany.Plugins.User:{1}", CallID, "Init()"))
Trace.Indent()
Trace.WriteLineIf(MyCompany.Common.AppHelper.TracingOn,
String.Format("{0}:Begin mybase:{1}", CallID, "Init()"))
MyBase.Init()
Trace.WriteLineIf(MyCompany.Common.AppHelper.TracingOn,
String.Format("{0}:End mybase:{1}", CallID, "Init()"))
Trace.WriteLineIf(MyCompany.Common.AppHelper.TracingOn,
String.Format("{0}:Begin local init logic", CallID))
'
' Custom implementation
'
'
Trace.Indent()
Trace.WriteLineIf(MyCompany.Common.AppHelper.TracingOn,
String.Format("{0}:Begin setting init flag", CallID))
mblnWasInited = True
Trace.WriteLineIf(MyCompany.Common.AppHelper.TracingOn,
String.Format("{0}:End setting init flag", CallID))
Trace.Unindent()
'
Trace.WriteLineIf(MyCompany.Common.AppHelper.TracingOn,
String.Format("{0}:End local init logic", CallID))
Trace.Unindent()
Trace.WriteLineIf(MyCompany.Common.AppHelper.TracingOn,
String.Format("{0}:End MyCompany.Plugins.User:{1}", CallID, "Init()"))
End Sub

End Class
End Namespace

Namespace MyCompany.Common
Friend Class AppHelper
Private Shared mTracingOn As Boolean
Private Shared mCallID As Integer

Shared Sub New()
mTracingOn = True
mCallID = Integer.MinValue
End Sub

Public Shared ReadOnly Property TracingOn() As Boolean
Get
Return mTracingOn
End Get
End Property

Public Shared Function GetNextMethodCall() As Integer
mCallID += 1
Return mCallID
End Function

End Class
End Namespace
 
Back
Top