Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Validating on Overridable method
Message
From
03/09/2011 03:41:41
 
 
To
01/09/2011 09:23:54
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
VB 9.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01522449
Message ID:
01522640
Views:
40
This message has been marked as the solution to the initial question of the thread.
>At the framework level, I have a method which is calling an Overridable method such as:
>
>
>        Private Function DataEntry() As Boolean
>
>...
>
>            ' Initialization code with row
>            If Not InitializeWithRow() Then
>                Return False
>            End If
>
>...
>
>            Return True
>        End Function
>
>        Public Overridable Function InitializeWithRow() As Boolean
>            Return True
>        End Function
>
>
>This is the main class. When the main class calls the Overridable method InitializeWithRow(), it has no way of knowing if the client application, which inherits from this class, has defined it or not. When the client application has defined it, it means a piece of code related to that method is present in the sub class such as:
>
>
>    Public Overrides Function InitializeWithRow() As Boolean
>
>        ' If the status is Voided or Closed
>        If oProcess.oApp.Inlist(oRow("NoStatus"), 7, 8) Then
>            AddDate("ContactDate", , False)
>        Else
>            AddDate("ContactDate", , False, True)
>        End If
>
>        Return True
>    End Function
>
>
>When this is the case, I would like the framework main class to validate for something before allowing the class to call the overrides method. But, I guess this is not possible.
>
>Any comment?

You could use Reflection. Simplified example:
Public Class BaseClass
	Public Function DataEntry() As Boolean
		Dim t As Type = Me.[GetType]()
		Dim minfo As MethodInfo = t.GetMethod("InitializeWithRow", BindingFlags.Instance Or BindingFlags.[Public])
		If minfo.DeclaringType.Name <> "BaseClass" AndAlso DoCheck() Then
			Return InitializeWithRow()
		End If
		Return True
	End Function

	Public Overridable Function InitializeWithRow() As Boolean
		Return True
	End Function

	Public Function DoCheck() As Boolean
		'Decide whether to call InitializeWithRow()
		Return True
	End Function
End Class
(Syntax not checked)
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform