late binding problem with option strict

G

Guest

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
 
G

Guest

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

Armin Zingler

Rich said:
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
 
H

Herfried K. Wagner [MVP]

Rich said:
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'.
 

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

Top