Option Strict and a Collection of Structures

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • zacks@construction-imaging.com

    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?

  • Michael D. Ober

    #2
    Re: Option Strict and a Collection of Structures

    Can you change your structures to classes? I use collections of classes all
    the time with no problems.

    Mike Ober.

    <zacks@construc tion-imaging.com> wrote in message
    news:1132347820 .088966.284220@ f14g2000cwb.goo glegroups.com.. .[color=blue]
    > 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?
    >[/color]



    Comment

    • m.posseth

      #3
      Re: Option Strict and a Collection of Structures

      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(B yVal sender As System.Object, ByVal e As
      System.EventArg s) 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("finishe d")

      End Sub

      End Class





      regards

      Michel Posseth [MCP]



      <zacks@construc tion-imaging.com> wrote in message
      news:1132347820 .088966.284220@ f14g2000cwb.goo glegroups.com.. .[color=blue]
      >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?
      >[/color]


      Comment

      • Joe Cool

        #4
        Re: Option Strict and a Collection of Structures

        On Sat, 19 Nov 2005 10:07:07 +0100, "m.posseth"
        <michelp@nohaus ystems.nl> wrote:
        [color=blue]
        >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(B yVal sender As System.Object, ByVal e As
        >System.EventAr gs) 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(sCo l, i.ToString)[/color]

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

        sCol.Add(strTes t)

        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.
        [color=blue]
        >
        >Next i
        >
        >MsgBox("finish ed")
        >
        >End Sub
        >
        >End Class
        >
        >
        >
        >
        >
        >regards
        >
        >Michel Posseth [MCP]
        >
        >
        >
        ><zacks@constru ction-imaging.com> wrote in message
        >news:113234782 0.088966.284220 @f14g2000cwb.go oglegroups.com. ..[color=green]
        >>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?
        >>[/color]
        >[/color]

        Comment

        • m.posseth

          #5
          Re: Option Strict and a Collection of Structures

          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(strTes t)

          Next i

          For Each Strval As test In sCol

          Debug.WriteLine (Strval.a)

          Debug.WriteLine (Strval.b)

          Next

          when i typed this
          [color=blue][color=green]
          >>sCol.Add(sCol , i.ToString)[/color][/color]


          my head was probably not so clear :-) as it ofcourse should have been[color=blue][color=green]
          >>sCol.Add(strT est, i.ToString)[/color][/color]


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

          regards

          Michel Posseth [MCP]



          "Joe Cool" <joecool@home.n et> wrote in message
          news:1v2vn1tj0d nt7d18q4lb642lu a3qpbp76i@4ax.c om...[color=blue]
          > On Sat, 19 Nov 2005 10:07:07 +0100, "m.posseth"
          > <michelp@nohaus ystems.nl> wrote:
          >[color=green]
          >>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(B yVal sender As System.Object, ByVal e As
          >>System.EventA rgs) 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)[/color]
          >
          > Key difference in my usage and yours. The above statement in my usage
          > would have been:
          >
          > sCol.Add(strTes t)
          >
          > 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.
          >[color=green]
          >>
          >>Next i
          >>
          >>MsgBox("finis hed")
          >>
          >>End Sub
          >>
          >>End Class
          >>
          >>
          >>
          >>
          >>
          >>regards
          >>
          >>Michel Posseth [MCP]
          >>
          >>
          >>
          >><zacks@constr uction-imaging.com> wrote in message
          >>news:11323478 20.088966.28422 0@f14g2000cwb.g ooglegroups.com ...[color=darkred]
          >>>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?
          >>>[/color]
          >>[/color]
          >[/color]


          Comment

          Working...