Option Strict and a Collection of Structures

  • Thread starter Thread starter zacks
  • Start date Start date
Z

zacks

A common programming technique I use in VB is making a collection of
structures. But if Option Strict is on (which I would prefer), the .Add
that adds the structure to the collection is flagged with a compiler
error (invalid type conversion). Is there a way to use a collection of
structures WITH the Option Strict On?
 
Can you change your structures to classes? I use collections of classes all
the time with no problems.

Mike Ober.
 
strange ,,,

changing structures to classes seems a bit odd to me as the TS might have a
good reasson to choose for a structure

when you do not need instancing , and ther are no actuall methods ( so the
struct only holds values ) i prefer to use structures also structures are
faster as classes in this case ( stack vs heap ) and more lightweight so
they seem perfect for this task.

I use structures in a hashtable , and i program always with option explicit
and option strict on


strange thingy i do not encounter this problem with a collection

Option Explicit On

Option Strict On

Public Class Form1

Inherits System.Windows.Forms.Form

Private Structure test

Friend a As String

Friend b As String

End Structure

Private sCol As Collection

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

sCol = New Collection

For i As Integer = 1 To 100

Dim strTest As New test

With strTest

..a = i.ToString

..b = "just a test"

End With

sCol.Add(sCol, i.ToString)

Next i

MsgBox("finished")

End Sub

End Class





regards

Michel Posseth [MCP]
 
strange ,,,

changing structures to classes seems a bit odd to me as the TS might have a
good reasson to choose for a structure

when you do not need instancing , and ther are no actuall methods ( so the
struct only holds values ) i prefer to use structures also structures are
faster as classes in this case ( stack vs heap ) and more lightweight so
they seem perfect for this task.

I use structures in a hashtable , and i program always with option explicit
and option strict on


strange thingy i do not encounter this problem with a collection

Option Explicit On

Option Strict On

Public Class Form1

Inherits System.Windows.Forms.Form

Private Structure test

Friend a As String

Friend b As String

End Structure

Private sCol As Collection

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

sCol = New Collection

For i As Integer = 1 To 100

Dim strTest As New test

With strTest

.a = i.ToString

.b = "just a test"

End With

sCol.Add(sCol, i.ToString)

Key difference in my usage and yours. The above statement in my usage
would have been:

sCol.Add(strTest)

And yes that would work just fine. It when I try to do this:

For Each strText in sCol
do something
Next

THIS is where I get the error.
Next i

MsgBox("finished")

End Sub

End Class





regards

Michel Posseth [MCP]



A common programming technique I use in VB is making a collection of
structures. But if Option Strict is on (which I would prefer), the .Add
that adds the structure to the collection is flagged with a compiler
error (invalid type conversion). Is there a way to use a collection of
structures WITH the Option Strict On?
 
Hello well this works just fine with me
sCol = New Collection

For i As Integer = 1 To 100

Dim strTest As New test

With strTest

..a = i.ToString

..b = "just a test"

End With

sCol.Add(strTest)

Next i

For Each Strval As test In sCol

Debug.WriteLine(Strval.a)

Debug.WriteLine(Strval.b)

Next

when i typed this


my head was probably not so clear :-) as it ofcourse should have been

but anyway i have tested above code and it foes work on my computer ( with
option strict on )

regards

Michel Posseth [MCP]



Joe Cool said:
strange ,,,

changing structures to classes seems a bit odd to me as the TS might have
a
good reasson to choose for a structure

when you do not need instancing , and ther are no actuall methods ( so the
struct only holds values ) i prefer to use structures also structures are
faster as classes in this case ( stack vs heap ) and more lightweight so
they seem perfect for this task.

I use structures in a hashtable , and i program always with option
explicit
and option strict on


strange thingy i do not encounter this problem with a collection

Option Explicit On

Option Strict On

Public Class Form1

Inherits System.Windows.Forms.Form

Private Structure test

Friend a As String

Friend b As String

End Structure

Private sCol As Collection

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

sCol = New Collection

For i As Integer = 1 To 100

Dim strTest As New test

With strTest

.a = i.ToString

.b = "just a test"

End With

sCol.Add(sCol, i.ToString)

Key difference in my usage and yours. The above statement in my usage
would have been:

sCol.Add(strTest)

And yes that would work just fine. It when I try to do this:

For Each strText in sCol
do something
Next

THIS is where I get the error.
Next i

MsgBox("finished")

End Sub

End Class





regards

Michel Posseth [MCP]



A common programming technique I use in VB is making a collection of
structures. But if Option Strict is on (which I would prefer), the .Add
that adds the structure to the collection is flagged with a compiler
error (invalid type conversion). Is there a way to use a collection of
structures WITH the Option Strict On?
 

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

Back
Top