Collections Woes - Help!

B

Bmack500

I'm definitely doing something wrong here. I have a class called
Servers, implemented as below. I then create my collection as below.
Then, I'll add a server object to the collection as follows:

Dim sServer as New Server
sServer.Path(iIndex2) = "servername"
serversCollection.Add(sServer)

No problems. Then I try and access a variable like this:

For i = 0 To (serversCollection.Count - 1)
serversCollection.Item(i).Path = "\\servername\path\"
Next
I'm always getting "option strict disallows late binding" when trying
to assign a value to one of the server objects in the collection.

What am I doing wrong?

'*************Create Collection************************
Private serversCollection As New System.Collections.ArrayList
Public Property sServer(ByVal I As Integer) As Server
Get
Return CType(serversCollection(I), Server)
End Get
Set(ByVal Value As Server)
serversCollection(I) = Value
End Set
End Property

'************Server Class*******************************
Public Class Server
Public mPath As String
Default Public Property Path(ByVal I As Integer) As String
Get
Return mPath
End Get
Set(ByVal Value As String)
mPath = Value
End Set
End Property
'Public InstanceName As String
Public Name As String
Public Nick As String
Public Status As String

Public newDat As Integer
Public newEngine As Integer
Public oldDat As Integer
Public oldEngine As Integer

Public datChange As Boolean
Public engineChange As Boolean
Public updateCycle As Boolean
Public contactFailure As Boolean

Public NumContactFailures As Integer
Public NumUpdateCycles As Integer

End Class
 
A

Andreas Mueller

Bmack500 said:
I'm definitely doing something wrong here. I have a class called
Servers, implemented as below. I then create my collection as below.
Then, I'll add a server object to the collection as follows:

Dim sServer as New Server
sServer.Path(iIndex2) = "servername"
serversCollection.Add(sServer)

No problems. Then I try and access a variable like this:

For i = 0 To (serversCollection.Count - 1)
serversCollection.Item(i).Path = "\\servername\path\"
Next
I'm always getting "option strict disallows late binding" when trying
to assign a value to one of the server objects in the collection.

What am I doing wrong?

'*************Create Collection************************
Private serversCollection As New System.Collections.ArrayList
Public Property sServer(ByVal I As Integer) As Server
Get
Return CType(serversCollection(I), Server)
End Get
Set(ByVal Value As Server)
serversCollection(I) = Value
End Set
End Property

'************Server Class*******************************
Public Class Server
Public mPath As String
Default Public Property Path(ByVal I As Integer) As String
Get
Return mPath
End Get
Set(ByVal Value As String)
mPath = Value
End Set
End Property
'Public InstanceName As String
Public Name As String
Public Nick As String
Public Status As String

Public newDat As Integer
Public newEngine As Integer
Public oldDat As Integer
Public oldEngine As Integer

Public datChange As Boolean
Public engineChange As Boolean
Public updateCycle As Boolean
Public contactFailure As Boolean

Public NumContactFailures As Integer
Public NumUpdateCycles As Integer

End Class
For i = 0 To (serversCollection.Count - 1)
CType(serversCollection.Item(i),Server).Path =
"\\servername\path\"
 
B

Bmack500

Argument not specified for Parameter I of 'Public Default Property Path
(I as Integer) as String'
 
H

Herfried K. Wagner [MVP]

Bmack500 said:
I'm definitely doing something wrong here. I have a class called
Servers, implemented as below. I then create my collection as below.
Then, I'll add a server object to the collection as follows:

Dim sServer as New Server
sServer.Path(iIndex2) = "servername"
serversCollection.Add(sServer)

No problems. Then I try and access a variable like this:

For i = 0 To (serversCollection.Count - 1)
serversCollection.Item(i).Path = "\\servername\path\"

'DirectCast(ServersCollection.Item(i), Server).Path = ...'.
 
B

Branco Medeiros

Bmack500 wrote:
Dim sServer as New Server
sServer.Path(iIndex2) = "servername"
serversCollection.Add(sServer)

No problems. Then I try and access a variable like this:

For i = 0 To (serversCollection.Count - 1)
serversCollection.Item(i).Path = "\\servername\path\"
Next
I'm always getting "option strict disallows late binding" when trying
to assign a value to one of the server objects in the collection.

What am I doing wrong?

'*************Create Collection************************
Private serversCollection As New System.Collections.ArrayList
Public Property sServer(ByVal I As Integer) As Server
Get
Return CType(serversCollection(I), Server)
End Get
Set(ByVal Value As Server)
serversCollection(I) = Value
End Set
End Property

'************Server Class*******************************
Public Class Server
Public mPath As String
Default Public Property Path(ByVal I As Integer) As String
Get
Return mPath
End Get
Set(ByVal Value As String)
mPath = Value
End Set
End Property
<snip>

Maybe you'd better use a strongly typed method, such as
serversCollection.sServer, which would give you back a Server
reference. Then you'd need to provide the required integer parameter
for the Server.Path property (btw, why do you need it?)...

For i = 0 To (serversCollection.Count - 1)
serversCollection.sServer(i).Path(i) = "\\servername\path\"
Next


HTH.

Regards,

Branco.
 
C

Claes Bergefall

Why do you have a variable and a property with the same name (sServer)?
You could use sServer(i).Path instead of serversCollection.Item(i).Path
since you have that property but it looks really weird.

My suggestion is that you define a strongly typed ServerCollection class and
use that instead. Makes your code much easier to read and maintain:

Public Class ServerCollection
Inherits System.Collections.CollectionBase

Public Sub New()
MyBase.New()
End Sub

Default Public Property Item(ByVal index As Integer) As Server
Get
Return CType(List.Item(index), Server)
End Get
Set(ByVal value As Server)
List.Item(index) = value
End Set
End Property

Public Function Add(ByVal value As Server) As Integer
Return List.Add(value)
End Function

Public Sub AddRange(ByVal values() As Server)
For Each value As Server In values
Add(value)
Next
End Sub

Public Function Contains(ByVal value As Server) As Boolean
Return List.Contains(value)
End Function

Public Function IndexOf(ByVal value As Server) As Integer
Return List.IndexOf(value)
End Function

Public Sub Insert(ByVal index As Integer, ByVal value As Server)
List.Insert(index, value)
End Sub

Public Sub Remove(ByVal value As Server)
List.Remove(value)
End Sub

Public Sub CopyTo(ByVal values() As Server, ByVal index As Integer)
List.CopyTo(values, index)
End Sub

End Class

/claes
 

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