How to create array class?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I have create a class name employee.
Next, i would like to create array for the employee class.
The code as below:
dim eply(5) as employee

when i call eply(0).name="Khrish", an error message prompt out as saying
object reference is null...

Any idea to solve it?
Please advice.
thank you in advance.

Rgrds,
GL
 
Hi,

I have create a class name employee.
Next, i would like to create array for the employee class.
The code as below:
dim eply(5) as employee

when i call eply(0).name="Khrish", an error message prompt out as saying
object reference is null...

Any idea to solve it?
Please advice.
thank you in advance.

Rgrds,
GL

Dim eply() As Employee { _
new Employee(), _
new Employee(), _
new Employee(), _
new Employee(), _
new Employee()}
 
Hi all,

thanks for your response.
I managed to solve the "object reference to null..." error message,however,
another issue appears.

I cannot get the list of 5 different records to be updated into the class
arrays, for instance:

inputs:
eply(0).name="Krish"
eply(1).name="James"
eply(2).name="Bond"

outputs:
all display as "Bond" name which is the last record during updating.

In my class, my code as follow:
public shared sname as string

Public Shared Property name() As String
Get
Return sname
End Get
Set(ByVal Value As String)
sname = Value
End Set
End Property

Any advice, please.

thank you in advance.

Rgrds,
GL
 
Tom Shelton showed you how to create an array of 5 with 5 elements and
initialize each element to a seperate instance of an Employee object.

It is clear, however, that you have created one instance of an Employee
object and assigned it to all 5 elements of the array. This will cause the
exact symptons you are observing.
 
: Hi all,
:
: thanks for your response.
: I managed to solve the "object reference to null..." error
message,however,
: another issue appears.
:
: I cannot get the list of 5 different records to be updated into the
class
: arrays, for instance:
:
: inputs:
: eply(0).name="Krish"
: eply(1).name="James"
: eply(2).name="Bond"
:
: outputs:
: all display as "Bond" name which is the last record during updating.
:
: In my class, my code as follow:
: public shared sname as string
:
: Public Shared Property name() As String
: Get
: Return sname
: End Get
: Set(ByVal Value As String)
: sname = Value
: End Set
: End Property
:
: Any advice, please.
:
: thank you in advance.
:
: Rgrds,
: GL
:
:
: "Cor Ligthert [MVP]" wrote:
:
: > Daniel,
: >
: > For 2005
: >
: >
http://www.vb-tips.com/dbpages.aspx?ID=11a1001e-f651-42e8-9a5b-9d0a3cf01846
: >
: > I hope this helps,
: >
: > Cor
: >
: >
: >
 
: Hi all,
:
: thanks for your response.
: I managed to solve the "object reference to null..." error
: message,however, another issue appears.
:
: I cannot get the list of 5 different records to be updated into the
: class arrays, for instance:
:
: inputs:
: eply(0).name="Krish"
: eply(1).name="James"
: eply(2).name="Bond"
:
: outputs:
: all display as "Bond" name which is the last record during updating.
:
: In my class, my code as follow:
: public shared sname as string
:
: Public Shared Property name() As String
: Get
: Return sname
: End Get
: Set(ByVal Value As String)
: sname = Value
: End Set
: End Property
:
: Any advice, please.
:
: thank you in advance.


Sorry for the earlier response. I fat fingered and inadvetantly sent
the post. Oops...


Anyway, your name variable (sName) is Shared which means there is only
one copy of it. When each object's name property is being set, they
are all setting the value of the same variable. Try removing the
Shared keyword.


Ralf
 
Hi all,

thanks for the help.
Finally, _AnonCoward has identified the root cause which i almost forget
:shared syntax.

Thanks for all the help given.

Rgrds,
GL
 
Daniel,

I think that you make not only me curious, what has shared/instanced to do
with the by you described problem. It is obvious that your (new made) class
has no indexer, that has nothing to do with instanced or shared?

Cor
 
Daniel,

I think that you make not only me curious, what has shared/instanced to do
with the by you described problem. It is obvious that your (new made) class
has no indexer, that has nothing to do with instanced or shared?

Cor

Cor,


He had the name property of his class defined as shared - so, setting it in
one instance set it for all....
 
Tom,

That he wrote, but how he did that, it can by handy

Something like this?

static x as string = "Tom"
dim y(5) as string = x

Can be very efficient but I don't know it.

Cor
 
Back
Top