473,385 Members | 1,331 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Option Strict and a Collection of Structures

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?

Nov 23 '05 #1
4 1342
Can you change your structures to classes? I use collections of classes all
the time with no problems.

Mike Ober.

<za***@construction-imaging.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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?


Nov 23 '05 #2
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]

<za***@construction-imaging.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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?

Nov 23 '05 #3
On Sat, 19 Nov 2005 10:07:07 +0100, "m.posseth"
<mi*****@nohausystems.nl> wrote:
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]

<za***@construction-imaging.com> wrote in message
news:11**********************@f14g2000cwb.googleg roups.com...
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?


Nov 23 '05 #4
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
sCol.Add(sCol, i.ToString)

my head was probably not so clear :-) as it ofcourse should have beensCol.Add(strTest, i.ToString)


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

regards

Michel Posseth [MCP]

"Joe Cool" <jo*****@home.net> wrote in message
news:1v********************************@4ax.com... On Sat, 19 Nov 2005 10:07:07 +0100, "m.posseth"
<mi*****@nohausystems.nl> wrote:
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]

<za***@construction-imaging.com> wrote in message
news:11**********************@f14g2000cwb.google groups.com...
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?

Nov 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Microsoft News | last post by:
I have a project that was created all with Option Strict OFF. Works great, not a problem with it. But if I turn Option Strict ON then I get a LOT of errors. My question, should I even care...
8
by: Rich | last post by:
Hello, If I leave Option Strict Off I can use the following syntax to read data from a Lotus Notes application (a NotesViewEntry object represents a row of data from a Lotus Notes View - like a...
4
by: Howard Kaikow | last post by:
I am trying to retrive some WMI properties using Option Strict On. This requires the use of InvokeMember. I know that there are alternative ways to get the values, but I want to learn how to...
17
by: David | last post by:
Hi all, I have the following problem: my program works fine, but when I add option strict at the top of the form, the following sub fails with an error that option strict does not allow late...
9
by: YYZ | last post by:
After reading many messages in this group, it seems that the preferred setting for this is ON. Okay, I did that in my project (first with ..Net -- long time VB6 developer) and now a bunch of...
3
by: Rich | last post by:
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...
15
by: guy | last post by:
when i first started using .net (beta 1) i came across option strict and thought hey this could be really good, and since then have always turned it on, most people here seem to agree that this is...
8
by: Rory Becker | last post by:
A wise man once said: "Never put off until runtime what you can fix at compile time." Actually I think he said it about 10 minutes before I started this post. I am a firm believer, like the...
0
by: cday119 | last post by:
Hi everyone, I have a Collection that is set up like this: Lines( Line1( Reduction1( FontName )
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.