469,963 Members | 2,184 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,963 developers. It's quick & easy.

inheritance question

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
Apr 28 '06 #1
2 971
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.Traci ngOn,
String.Format("-----{0}:Begin MyCompany.Core.Item:{1}", CallID, "New()"))
mblnWasInited = False
Trace.WriteLineIf(MyCompany.Common.AppHelper.Traci ngOn,
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.Traci ngOn,
String.Format("{0}:Begin MyCompany.Core.Item:{1}", CallID, "Init()"))
Init(False)
Trace.WriteLineIf(MyCompany.Common.AppHelper.Traci ngOn,
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.Traci ngOn,
String.Format("{0}:Begin MyCompany.Core.Item:{1}", CallID, "Init(bool)"))
If SetFlag Then mblnWasInited = SetFlag
Trace.WriteLineIf(MyCompany.Common.AppHelper.Traci ngOn,
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.Traci ngOn,
String.Format("{0}:Begin MyCompany.Plugins.User:{1}", CallID, "New()"))
'
'
'
Trace.WriteLineIf(MyCompany.Common.AppHelper.Traci ngOn,
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.Traci ngOn,
String.Format("{0}:Begin MyCompany.Plugins.User:{1}", CallID, "Init()"))
Trace.Indent()
Trace.WriteLineIf(MyCompany.Common.AppHelper.Traci ngOn,
String.Format("{0}:Begin mybase:{1}", CallID, "Init()"))
MyBase.Init()
Trace.WriteLineIf(MyCompany.Common.AppHelper.Traci ngOn,
String.Format("{0}:End mybase:{1}", CallID, "Init()"))
Trace.WriteLineIf(MyCompany.Common.AppHelper.Traci ngOn,
String.Format("{0}:Begin local init logic", CallID))
'
' Custom implementation
'
'
Trace.Indent()
Trace.WriteLineIf(MyCompany.Common.AppHelper.Traci ngOn,
String.Format("{0}:Begin setting init flag", CallID))
mblnWasInited = True
Trace.WriteLineIf(MyCompany.Common.AppHelper.Traci ngOn,
String.Format("{0}:End setting init flag", CallID))
Trace.Unindent()
'
Trace.WriteLineIf(MyCompany.Common.AppHelper.Traci ngOn,
String.Format("{0}:End local init logic", CallID))
Trace.Unindent()
Trace.WriteLineIf(MyCompany.Common.AppHelper.Traci ngOn,
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

"Craig Buchanan" <na**@company.com> wrote in message
news:Ox*************@TK2MSFTNGP02.phx.gbl...
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

Apr 28 '06 #2
thanks, i'll take a look and let you know.

"AMDRIT" <am****@hotmail.com> wrote in message
news:eC**************@TK2MSFTNGP03.phx.gbl...
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.Traci ngOn,
String.Format("-----{0}:Begin MyCompany.Core.Item:{1}", CallID, "New()"))
mblnWasInited = False
Trace.WriteLineIf(MyCompany.Common.AppHelper.Traci ngOn,
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.Traci ngOn,
String.Format("{0}:Begin MyCompany.Core.Item:{1}", CallID, "Init()"))
Init(False)
Trace.WriteLineIf(MyCompany.Common.AppHelper.Traci ngOn,
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.Traci ngOn,
String.Format("{0}:Begin MyCompany.Core.Item:{1}", CallID, "Init(bool)"))
If SetFlag Then mblnWasInited = SetFlag
Trace.WriteLineIf(MyCompany.Common.AppHelper.Traci ngOn,
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.Traci ngOn,
String.Format("{0}:Begin MyCompany.Plugins.User:{1}", CallID, "New()"))
'
'
'
Trace.WriteLineIf(MyCompany.Common.AppHelper.Traci ngOn,
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.Traci ngOn,
String.Format("{0}:Begin MyCompany.Plugins.User:{1}", CallID, "Init()"))
Trace.Indent()
Trace.WriteLineIf(MyCompany.Common.AppHelper.Traci ngOn,
String.Format("{0}:Begin mybase:{1}", CallID, "Init()"))
MyBase.Init()
Trace.WriteLineIf(MyCompany.Common.AppHelper.Traci ngOn,
String.Format("{0}:End mybase:{1}", CallID, "Init()"))
Trace.WriteLineIf(MyCompany.Common.AppHelper.Traci ngOn,
String.Format("{0}:Begin local init logic", CallID))
'
' Custom implementation
'
'
Trace.Indent()
Trace.WriteLineIf(MyCompany.Common.AppHelper.Traci ngOn,
String.Format("{0}:Begin setting init flag", CallID))
mblnWasInited = True
Trace.WriteLineIf(MyCompany.Common.AppHelper.Traci ngOn,
String.Format("{0}:End setting init flag", CallID))
Trace.Unindent()
'
Trace.WriteLineIf(MyCompany.Common.AppHelper.Traci ngOn,
String.Format("{0}:End local init logic", CallID))
Trace.Unindent()
Trace.WriteLineIf(MyCompany.Common.AppHelper.Traci ngOn,
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

"Craig Buchanan" <na**@company.com> wrote in message
news:Ox*************@TK2MSFTNGP02.phx.gbl...
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


May 1 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by KK | last post: by
4 posts views Thread by Dave Theese | last post: by
8 posts views Thread by __PPS__ | last post: by
22 posts views Thread by Matthew Louden | last post: by
45 posts views Thread by Ben Blank | last post: by
5 posts views Thread by Noah Roberts | last post: by
3 posts views Thread by RSH | last post: by
8 posts views Thread by RSH | last post: by
1 post views Thread by rainxy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.