Home All Groups Group Topic Archive Search About

late binding problem with option strict

Author
28 Jan 2006 12:28 AM
Rich
This code worked before I added option strict.  But I should do it correctly.

option ...
option Strict On

Public Structure Itm
    Dim mailSvr As String
    ...
    Public Sub New(ByVal a1 As String...)
        mailSvr = a1
    End Sub
End Structure

Sub...
ht = New Hashtable
ht.Add("0", New Itm("thecorporatecounsel", ".net", "CC", "TCCNET_ID", "T"))
ht.Add("0", New Itm("thecorporatecounsel", ".net", "CC", "TCCNET_ID", "T"))
....
End Sub

Sub ...
str1 = ht(0).mailSvr  <<<<----complains here now - say late binding not
allowed
....
End Sub

What do I need to do to make it early binding?  What do I need to cast this
to?

Any suggestions appreciated.

Thanks,
Rich

Author
28 Jan 2006 12:39 AM
Rich
The index 0 is actually a string "0".  Still complains though with option
Strict on

Sub ...
str1 = ht("0").mailSvr  <<<<----complains here now - say late binding not
allowed
....
End Sub

I tried CType(ht("0").MailSvr, String)  ---- still complains

wait !!!!  I just tried this and it stopped complaining !!!

str1 = CType(ht("0"), Itm).mailSvr  

Nevermind.  He's happy again.
Author
28 Jan 2006 12:41 AM
Armin Zingler
Show quote Hide quote
"Rich" <R***@discussions.microsoft.com> schrieb
> This code worked before I added option strict.  But I should do it
> correctly.
>
> option ...
> option Strict On
>
> Public Structure Itm
>    Dim mailSvr As String
>    ...
>    Public Sub New(ByVal a1 As String...)
>        mailSvr = a1
>    End Sub
> End Structure
>
> Sub...
> ht = New Hashtable
> ht.Add("0", New Itm("thecorporatecounsel", ".net", "CC",
> "TCCNET_ID", "T")) ht.Add("0", New Itm("thecorporatecounsel",
> ".net", "CC", "TCCNET_ID", "T")) ...
> End Sub
>
> Sub ...
> str1 = ht(0).mailSvr  <<<<----complains here now - say late binding
> not allowed
> ...
> End Sub
>
> What do I need to do to make it early binding?

Cast to the type of the object.

> What do I need to
> cast this to?

To type Itm:

    str1 = directcast(ht(0), Itm).mailSvr

If you will access the hashtable in more situations, you should consider
writing your own collection having a typed Item property.


Armin
Author
28 Jan 2006 1:37 AM
Herfried K. Wagner [MVP]
Show quote Hide quote
"Rich" <R***@discussions.microsoft.com> schrieb:
> This code worked before I added option strict.  But I should do it
> correctly.
> [...]
> Public Structure Itm
>    Dim mailSvr As String
>    ...
>    Public Sub New(ByVal a1 As String...)
>        mailSvr = a1
>    End Sub
> End Structure
>
> Sub...
> ht = New Hashtable
> ht.Add("0", New Itm("thecorporatecounsel", ".net", "CC", "TCCNET_ID",
> "T"))
> ht.Add("0", New Itm("thecorporatecounsel", ".net", "CC", "TCCNET_ID",
> "T"))
> ...
> End Sub
>
> Sub ...
> str1 = ht(0).mailSvr  <<<<----complains here now - say late binding not

Use 'str1 = DirectCast(ht(0), Itm).mailSvr' instead.  In .NET 2.0 you can
use one of the generic dictionary classes which can be parameterized with
the item type instead of 'Hashtable'.

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