Tag and Late Binding

E

eBob.com

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 ...

Private Sub chkbxSelI_Click(ByVal sender As Object, ByVal e As
System.EventArgs)

MsgBox("bingo for number " & sender.Tag.ToString)
End Sub

.... but with Option Strict On I get a complaint about "sender.Tag".

I looked up a brief article about Late Binding and I get the
impression that this is just the way it is. If you use Late Binding
you cannot use Option Strict On.

But I don't understand exactly what Late Binding it is complaining
about. I would guess that the compiler is concerned that he can't be
sure that sender.Tag is an Integer. But when I cast sender.Tag to an
Integer I still get a Late Binding error on sender.Tag.

I hate to give up Option Strict On, so I thought I'd ask here if there
is some way to use Option Strict On and still use Late Binding. Maybe
casting was the right idea but I did it wrong? Or maybe there is some
other way to pass info to my event handler which does not involve Tag.
Although I really like the idea of Tag. I think that I am using it
exactly the way it is intended to be used.

Thanks, Bob
 
Q

Qwert

Don't think so:

"In addition to the conditions described above, Option Strict generates an
error for:
* Any undeclared variable since it is implied that Option Strict also means
Option Explicit.
* Late-binding."

- Microsoft.
 
C

Cor Ligthert

Bob,

This is easy, you have to use Convert or Cast commands. (Convert converts,
Cast uses as).
Private Sub chkbxSelI_Click(ByVal sender As Object, ByVal e As
System.EventArgs)

MsgBox("bingo for number " & sender.Tag.ToString)
End Sub
The sender is I assume a CheckBox object. Than you have to say
DirectCast(sender, CheckBox).Tag.ToString
"Use sender as a CheckBox because it is that (or derived from)".

That is all, one time you know this all problems about early binding are
gone.

When it is a value by instance a string from which you want to use the
contents as an integer, than you can say.
CInt("1234")
or in the long way
CType("1234",Integer)
' what is the same

Here DirectCast fails because "1234" is not an integer (or derives from
that).

In other words, you can use everywhere CType (ore the shortcuts) where you
can use DirectCast however not in the opposite way. Using (DirectCast) is of
course less expensive than converting.

ToString() is standard a converting command CType(x,String), However that is
often overridden in different methods

I hope this helps?

Cor
 
H

Herfried K. Wagner [MVP]

eBob.com said:
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 ...

Private Sub chkbxSelI_Click(ByVal sender As Object, ByVal e As
System.EventArgs)

MsgBox("bingo for number " & sender.Tag.ToString)
End Sub

... but with Option Strict On I get a complaint about "sender.Tag".

I looked up a brief article about Late Binding and I get the
impression that this is just the way it is. If you use Late Binding
you cannot use Option Strict On.

But I don't understand exactly what Late Binding it is complaining
about. I would guess that the compiler is concerned that he can't be
sure that sender.Tag is an Integer.

No! 'sender' is of type 'Object', and 'Object' doesn't have a 'Tag'
property. You can solve the problem by casting 'sender' to 'Control', which
provides the 'Tag' property and is the superclass of all controls:

\\\
Dim SourceControl As Control = DirectCast(sender, Control)
MsgBox("Bingo for number " & CStr(SourceControl.Tag)
///
 
E

eBob.com

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 ...

Private Sub chkbxSelI_Click(ByVal sender As Object, ByVal e As
System.EventArgs)

MsgBox("bingo for number " & sender.Tag.ToString)
End Sub

... but with Option Strict On I get a complaint about "sender.Tag".

I looked up a brief article about Late Binding and I get the
impression that this is just the way it is. If you use Late Binding
you cannot use Option Strict On.

But I don't understand exactly what Late Binding it is complaining
about. I would guess that the compiler is concerned that he can't be
sure that sender.Tag is an Integer. But when I cast sender.Tag to an
Integer I still get a Late Binding error on sender.Tag.

I hate to give up Option Strict On, so I thought I'd ask here if there
is some way to use Option Strict On and still use Late Binding. Maybe
casting was the right idea but I did it wrong? Or maybe there is some
other way to pass info to my event handler which does not involve Tag.
Although I really like the idea of Tag. I think that I am using it
exactly the way it is intended to be used.

Thanks, Bob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top