By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,934 Members | 1,527 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,934 IT Pros & Developers. It's quick & easy.

inheritance question

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.