473,404 Members | 2,174 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

how to do late binding with Option Explicit On

Sam
Hi,
Here's my class's constructor:

Public Sub New(ByVal frmParent As Form, ByVal curRow As Integer)
MyBase.New()
End Sub

I have a member variable that I want to initialize with frmParent. Then
I would call methods on it. The issue is that frmParent can be any form
of my applications and therefore I have to do:

Directcast(myMemberVar,FormA).MethodOfFormA

DirectCast(myMemberVar, FormB).MethodOfFormB

How can I avoid doing this ?? How can I simply set the type of my
member variable to the type of frmParent ?

Thx

Nov 21 '05 #1
7 1564
Sam
I meant Option Strict On, not Option Explicit On.... sorry about that !

Nov 21 '05 #2
"Sam" <sa**************@voila.fr> schrieb:
Here's my class's constructor:

Public Sub New(ByVal frmParent As Form, ByVal curRow As Integer)
MyBase.New()
End Sub

I have a member variable that I want to initialize with frmParent. Then
I would call methods on it. The issue is that frmParent can be any form
of my applications and therefore I have to do:

Directcast(myMemberVar,FormA).MethodOfFormA

DirectCast(myMemberVar, FormB).MethodOfFormB


Do the methods have different names? I assume they should have the same
name:

\\\
Public Class BaseForm
Inherits Form

End Class

Public Class FormA
Inherits BaseForm

Public Sub Foo()
MsgBox("'FormA.Foo'")
End Sub
End Class

Public Class FormB
Inherits BaseForm

Public Sub Foo()
MsgBox("'FormB.Foo'")
End Sub
End Class
///

Usage:

\\\

' Use VB.NET's handy 'CallByName' function.
Public Sub Test1(ByVal Form As BaseForm)
CallByName(Form, "Foo", CallType.Method.Method)
End Sub

' Use .NET's reflection.
Public Sub Test2(ByVal Form As BaseForm)
Form.GetType().InvokeMember( _
"Foo", _
BindingFlags.Instance Or BindingFlags.InvokeMethod Or
BindingFlags.Public, _
Nothing, _
Form, _
Nothing _
)
End Sub
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #3
Sam
No, they do have different name and they are not related at all. THey
do completely different things.

Nov 21 '05 #4
"Sam" <sa**************@voila.fr> schrieb:
No, they do have different name and they are not related at all. THey
do completely different things.


Mhm... Then I am curious why you do not use 'DirectCast'...

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #5


Even with totally unrelated functions / forms the Herfried's reflection
example above works fine, so that hopefully answers your original
question.

I am curious though as to how the class knows which function(s) to call
on the form.

By using late binding we avoid having to declare the form's runtime
type at design time but seem to buy an amount of runtime complication
e.g.

class Form1
inherits System.Windows.Forms.Form
...
public sub foo()
' do something
end sub

end class

class Form2
inherits System.Windows.Forms.Form
...
public sub bar()
' do something else
end sub

end class
class Controller '??

public sub new ( frm as System.Windows.Forms.Form, rowVal as
integer )
_form = frm
_row = rowVal
end sub

public sub DoSomething()

' at this point, how do we know which function we are trying to
invoke on the form

end sub

end class
What is the general shape of the program on which you are working?

Alan.

Nov 21 '05 #6
Same,
In addition to the other comments:

How do you know to call FormA.MethodOfFormA verses FormB.MethodOfFormB?

I would use Polymorphism to know which to call. Remember that Polymorphism
is one of the major tenants of OO.

In order to use Polymorphism each form would have the "same" method to call,
such as frmParent.SomeMethod. Where FormA & FormB would offer their own
implementation of SomeMethod. In other words FormA.SomeMethod does something
specific to FormA, while FormB.SomeMethod does something specific to FormB.
However SomeMethod has some common expected behavior, such as Save Record.

CallByName would be useful if you know the specific name, but you cannot
require a base class or interface. I would consider using a Custom Attribute
to identify the method that needs to be called, using Reflection to find the
method with the attribute, & then using Reflection to invoke the method.
However there may be performance considerations to consider with either
CallByName or Reflection.
Here are examples of using either a base class or an Interface to define
SomeMethod.

Something like:

Public Class BaseForm
Inherits System.Windows.Forms.Form

Public Overridable Sub SomeMethod()
End Sub

End Class

Public Class FormA
Inherits BaseForm

Public Overrides Sub SomeMethod()
' Code specific to FormA
End Sub

End Class

Public Class FormB
Inherits BaseForm

Public Overrides Sub SomeMethod()
' Code specific to FormB
End Sub

End Class

| Public Sub New(ByVal frmParent As BaseForm, ByVal curRow As Integer)
| MyBase.New()
frmParent.SomeMethod()
| End Sub
Alternatively you can use Interfaces:
Public Interface ISomeMethodable

Sub SomeMethod()

End Interface

Public Class FormA
Inherits System.Windows.Forms.Form
Implements ISomeMethodable

Public Sub MethodOfFormA() Implements ISomeMethodable.SomeMethod
' Code specific to FormA
End Sub

End Class

Public Class FormB
Inherits System.Windows.Forms.Form
Implements ISomeMethodable

Public Sub MethodOfFormB() Implements ISomeMethodable.SomeMethod
' Code specific to FormB
End Sub

End Class

| Public Sub New(ByVal frmParent As ISomeMethodable, ByVal curRow As
Integer)
| MyBase.New()
frmParent.SomeMethod()
| End Sub

In either case (base class or interface) you can simply pass an instance of
FormA or FormB to the constructor:

Dim a As FormA
Dim b As FormB

Dim c As New SomeObject(a, 0)
Dim d As New SomeObject(b, 0)
The advantage of the BaseForm is that you know frmParent is still a Form.

The advantage of ISomeMethodable is that any Type can implement the
interface, it may not be a Form, however each implementer can have its own
name for the implemented method.

Of course you can do something like:

| Public Sub New(ByVal frmParent As Form, ByVal curRow As Integer)
| MyBase.New()
If TypeOf frmParent Is ISomeMethodable Then
Dim callee As ISomeMethodable = DirectCast(frmParent,
ISomeMethodable)
callee.SomeMethod()
End If
| End Sub

Which allows your constructor to still require a Form, however it will call
SomeMethod for those objects that implement ISomeMethodable...
Hope this helps
Jay
"Sam" <sa**************@voila.fr> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
| Hi,
| Here's my class's constructor:
|
| Public Sub New(ByVal frmParent As Form, ByVal curRow As Integer)
| MyBase.New()
| End Sub
|
| I have a member variable that I want to initialize with frmParent. Then
| I would call methods on it. The issue is that frmParent can be any form
| of my applications and therefore I have to do:
|
| Directcast(myMemberVar,FormA).MethodOfFormA
|
| DirectCast(myMemberVar, FormB).MethodOfFormB
|
| How can I avoid doing this ?? How can I simply set the type of my
| member variable to the type of frmParent ?
|
| Thx
|
Nov 21 '05 #7
Sam,
Here's an example of using a CustomAttribute & Reflection to invoke the
method:

<AttributeUsage(AttributeTargets.Method)> _
Public Class SomeMethodAttribute
Inherits Attribute

End Class

Public Class FormA
Inherits System.Windows.Forms.Form

<SomeMethod()> _
Public Sub MethodOfFormA()
' Code specific to FormA
Debug.WriteLine("MethodOfFormA", "FormA")
End Sub

End Class

Public Class FormB
Inherits System.Windows.Forms.Form

<SomeMethod()> _
Public Sub MethodOfFormB()
' Code specific to FormB
Debug.WriteLine("MethodOfFormB", "FormB")
End Sub

End Class

Public Sub New(ByVal frmParent As Form, ByVal curRow As Integer)
MyBase.New()

Dim theType As Type = frmParent.GetType()
For Each method As System.Reflection.MethodInfo In
theType.GetMethods()
Dim theAttributes() As Attribute =
DirectCast(method.GetCustomAttributes(GetType(Some MethodAttribute), False),
Attribute())
If theAttributes.Length > 0 Then
method.Invoke(frmParent, Nothing)
End If
Next

End Sub

NOTE: The above code will invoke *all* the methods in frmParent that have
the SomeMethodAttribute on them.

The advantage of using the above is that it allows the caller to only know
about the SomeMethodAttribute, and avoids coupling to each specific Form
type.

Hope this helps
Jay

"Sam" <sa**************@voila.fr> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
| Hi,
| Here's my class's constructor:
|
| Public Sub New(ByVal frmParent As Form, ByVal curRow As Integer)
| MyBase.New()
| End Sub
|
| I have a member variable that I want to initialize with frmParent. Then
| I would call methods on it. The issue is that frmParent can be any form
| of my applications and therefore I have to do:
|
| Directcast(myMemberVar,FormA).MethodOfFormA
|
| DirectCast(myMemberVar, FormB).MethodOfFormB
|
| How can I avoid doing this ?? How can I simply set the type of my
| member variable to the type of frmParent ?
|
| Thx
|
Nov 21 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Scott English | last post by:
I am writing an C# program. I call a method on a COM object that returns Object. I don't know the type of the object (and reflection just says its a __ComObject), but I know there is supposed to...
4
by: KC | last post by:
I'm trying to export data to Excel, and I found an example routine at microsoft, but I get an "Option Strict On disallows late binding." at 'oBook = oExcel.Workbooks.Add'. Without turning Option...
5
by: eBob.com | last post by:
In another thread VJ made me aware of Tag. Fantastic! I've been wanting this capability for a long time. But it seems that I cannot use it with Option Strict On. In an event handler I have ......
30
by: lgbjr | last post by:
hi All, I've decided to use Options Strict ON in one of my apps and now I'm trying to fix a late binding issue. I have 5 integer arrays: dim IA1(500), IA2(500), IA3(500), IA4(500), IA5(500) as...
5
by: jg | last post by:
I followed the pattern in the MSDN dot net example for .net server and I tried a making simple .net dll as COM from Option Explicit On Option Strict On Imports System Imports...
17
by: David | last post by:
Hi all, I have the following problem: my program works fine, but when I add option strict at the top of the form, the following sub fails with an error that option strict does not allow late...
8
by: Boni | last post by:
Dear All, I would like to allow a conversion of types (i.e. double to int) but prohibit a late binding (because obfuscators don't support this). i.e. obj as object obj=yyy obj.AAA ...
3
by: gregory_may | last post by:
I want to prevent late binding of this statement: MyServerConnections(Myindex).client = Value But when I try with the code below, I get an editor error: "Expression is a value and therefor...
5
by: b00n1 | last post by:
I'm trying to create a simple vb.net (server) dll, and then through late binding access the public methods of the contained class from another vb.net (client) app. I can do it through early...
14
by: Siv | last post by:
hi, I am converting an application that writes to an Excel spreadsheet and the code trips the "option Strict" that I would like on because the parser says "option Strict On disallows late...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.