One Last Class Question - Example that works

M

Miro

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 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

=============================
 
G

GhostInAK

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
 
M

Miro

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.
 
P

Patrice

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")}
 

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

Top