One Last Class Question - Example that works

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Miro

    #1

    One Last Class Question - Example that works

    First off...thanks in advance for getting me this far.

    Sorry for all these class posts but im having a heck of a time here trying
    to get something to work,
    and have finally got it to work ( yahooooo ) but i dont know why now I cant
    get it to work the other way.

    Vb 2003

    Below are 2 examples. One Does not work and the other does.

    Now i know why the one example works, ive basically shared everything and
    off we go.
    But why cannot I Private the var and Public the property ?
    It compiles...but its like it thinks then its assigning the value to a
    "something" that doesnt exist yet.

    Even if I dim BlaSys() without a ( # ) # in there...and redim...it
    still fails.

    --- my conclusion ---
    What Im starting to think is that you CANNOT have an array of a Class that
    has properties.
    But what you have to do is have a Class with an Array of Properties.

    Am I right in this statement?
    Has anyone else tried to make an Array Class ?

    My Last example is a simple way so the full length ( since it might not be
    known ) will always work. 99 is not a set limit.


    Miro

    =======This does not work =============== =
    'Somewhere in a sub
    'compiles ok but will get an exception error during runtime
    Dim BlaSys(2) As SystemFilesClas s 'Cannot put "New" ...it
    wont allow it

    BlaSys(1).Prop_ FieldName = "hi" 'will return "Object
    reference not set to an instance of an object" - crashes here


    '==== a class
    Public Class SystemFilesClas s

    Private FieldName As String

    Public Property Prop_FieldName( ) As String 'This cannot go "SHARED"
    cause the FieldName is Private
    Get
    Return FieldName
    End Get
    Set(ByVal Value As String)
    FieldName = Value
    End Set
    End Property

    End Class


    =============== ======


    =========This works========== ===
    'Somewhere in a sub
    Dim BlaSys As New SystemFilesClas s() 'Allows a "NEW" on the
    class

    BlaSys.Prop_Fie ldName(1) = "hi" 'Works

    BlaSys.Prop_Fie ldName(2) = "Bye" 'Works


    '======= a class
    Public Class SystemFilesClas s

    Shared FieldName(99) As String 'Number of elelments must be defined

    Shared Property Prop_FieldName( ByVal Element As Integer) As String
    Get
    Return FieldName(Eleme nt)
    End Get
    Set(ByVal Value As String)
    FieldName(Eleme nt) = Value
    End Set
    End Property

    End Class
    =============== =============== ==


    ========= modified code of code that works that takes care of an infinite
    array of FieldName ===========

    Dim BlaSys As New SystemFilesClas s() 'Allows a "NEW" on the
    class

    BlaSys.Prop_Fie ldName(1) = "hi" 'Works

    BlaSys.Prop_Fie ldName(2) = "Bye" 'Works



    Public Class SystemFilesClas s

    Shared FieldName(1) As String 'the 1 or a number must be here...taken
    care of as we add new elements.

    Shared Property Prop_FieldName( ByVal Element As Integer) As String
    Get
    Return FieldName(Eleme nt)
    End Get
    Set(ByVal Value As String)
    If UBound(FieldNam e) < Element Then
    'redims the array + 1 : probably want to go up by 10's to
    save
    'time / memory but it proves the point of just adding to it.
    ( preserve ) must be here to
    'keep track of old assigned array values.
    ReDim Preserve FieldName(UBoun d(FieldName) + 1)
    End If
    FieldName(Eleme nt) = Value
    End Set
    End Property

    End Class

    =============== ==============


  • GhostInAK

    #2
    Re: One Last Class Question - Example that works

    Hello Miro,

    You are not instantiating the array element before you use it.

    Dim tObjects(4) As SomeClass ' Create an array of 5 objects, ALL SET
    TO NOTHING
    Dim tCount as Integer = 0

    ' Make it so we can use this crap
    For tCount = tObjects.GetLow erBound(0) to tObjects.GetUpp erBound
    tObjects(tCount ) = New SomeClass
    Next

    ' NOW we can do something with the array elements without blowing up the
    world.


    You may want to read up on how arrays work. RTFM man.. RTMFM.

    -Boo


    Comment

    • Miro

      #3
      Re: One Last Class Question - Example that works

      I was under the impression that if i can say

      Dim WhatThe(4) As SomeClass

      well then all elements are Copies of the class.

      The Example of a class being arrayed is not in my manual. :(

      Im surprised they dont allow Dim WhatThe(4) As New SomeClass
      It would make more sence ( to me anyway )

      Thank you,

      M.





      "GhostInAK" <ghostinak@gmai l.comwrote in message
      news:c71747b42d 9dd8c895d22a594 45e@news.micros oft.com...
      Hello Miro,
      >
      You are not instantiating the array element before you use it.
      >
      Dim tObjects(4) As SomeClass ' Create an array of 5 objects, ALL SET
      TO NOTHING
      Dim tCount as Integer = 0
      >
      ' Make it so we can use this crap
      For tCount = tObjects.GetLow erBound(0) to tObjects.GetUpp erBound
      tObjects(tCount ) = New SomeClass
      Next
      >
      ' NOW we can do something with the array elements without blowing up
      the world.
      >
      >
      You may want to read up on how arrays work. RTFM man.. RTMFM.
      >
      -Boo
      >
      >

      Comment

      • Patrice

        #4
        Re: One Last Class Question - Example that works

        Could have been but I guess that this addition where not considered a
        priority (for example most of the time you don't hold just "blank" object,
        you need to initialize some members to specific values and you may want to
        keep another reference to what you put in the array) so it's likely not
        terribly usefull...

        For now see things as just an array declaration i.e. you declare the array
        itself , but the cells of this array are left unitialized...

        Depending on your context you could use an initializer expression :

        Dim MyArray() As MyClass={New MyClass("A"),Ne w MyClass("B"), New
        MyClass("C")}

        --
        Patrice

        "Miro" <mironagy@golde n.neta écrit dans le message de news:
        uDK75KEyGHA.344 0@TK2MSFTNGP06. phx.gbl...
        >I was under the impression that if i can say
        >
        Dim WhatThe(4) As SomeClass
        >
        well then all elements are Copies of the class.
        >
        The Example of a class being arrayed is not in my manual. :(
        >
        Im surprised they dont allow Dim WhatThe(4) As New SomeClass
        It would make more sence ( to me anyway )
        >
        Thank you,
        >
        M.
        >
        >
        >
        >
        >
        "GhostInAK" <ghostinak@gmai l.comwrote in message
        news:c71747b42d 9dd8c895d22a594 45e@news.micros oft.com...
        >Hello Miro,
        >>
        >You are not instantiating the array element before you use it.
        >>
        >Dim tObjects(4) As SomeClass ' Create an array of 5 objects, ALL
        >SET TO NOTHING
        >Dim tCount as Integer = 0
        >>
        >' Make it so we can use this crap
        >For tCount = tObjects.GetLow erBound(0) to tObjects.GetUpp erBound
        > tObjects(tCount ) = New SomeClass
        >Next
        >>
        >' NOW we can do something with the array elements without blowing up
        >the world.
        >>
        >>
        >You may want to read up on how arrays work. RTFM man.. RTMFM.
        >>
        >-Boo
        >>
        >>
        >
        >

        Comment

        Working...