|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
One Last Class Question - Example that worksSorry 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 SystemFilesClass '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 SystemFilesClass 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 SystemFilesClass() 'Allows a "NEW" on the class BlaSys.Prop_FieldName(1) = "hi" 'Works BlaSys.Prop_FieldName(2) = "Bye" 'Works '======= a class Public Class SystemFilesClass Shared FieldName(99) As String 'Number of elelments must be defined Shared Property Prop_FieldName(ByVal Element As Integer) As String Get Return FieldName(Element) End Get Set(ByVal Value As String) FieldName(Element) = 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 SystemFilesClass() 'Allows a "NEW" on the class BlaSys.Prop_FieldName(1) = "hi" 'Works BlaSys.Prop_FieldName(2) = "Bye" 'Works Public Class SystemFilesClass 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(Element) End Get Set(ByVal Value As String) If UBound(FieldName) < 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(UBound(FieldName) + 1) End If FieldName(Element) = Value End Set End Property End Class ============================= 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.GetLowerBound(0) to tObjects.GetUpperBound 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 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. Show quoteHide quote "GhostInAK" <ghosti***@gmail.com> wrote in message news:c71747b42d9dd8c895d22a59445e@news.microsoft.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.GetLowerBound(0) to tObjects.GetUpperBound > 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 > > 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"),New MyClass("B"), New MyClass("C")} -- Patrice "Miro" <miron***@golden.net> a écrit dans le message de news: uDK75KEyGHA.3***@TK2MSFTNGP06.phx.gbl...Show quoteHide quote >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" <ghosti***@gmail.com> wrote in message > news:c71747b42d9dd8c895d22a59445e@news.microsoft.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.GetLowerBound(0) to tObjects.GetUpperBound >> 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 >> >> > >
How to create a report (invoice)?
Q: deleting relations DirectX rendered inside a control? problem save and reading from the registry Problems with SaveAs Database Current namespace, stack and lineno How to make double click event to call and execute the mouseup event code Using SQLDataSource without a control? Arrays please help me |
|||||||||||||||||||||||