473,320 Members | 1,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Array of a Class - Weird Output

Vb2003, im still learning vb.net but I do not understand my output from this
logic.
If someone can help me out here.

Cor Ligthert, you I believe were on the right track of what Im trying to
create.
-Thank you very much for leading me on to Classes.

Ok here is my code. ( see below )

I have a class defined with 1 propertie and then create an Array of the
Class and msgbox out my output.

Why does each msgbox that shows bring up the value "This One" ? even
though the counter increments.
I would have assumed I should have gotten, "Hi" "Bye" "This One"
instead of
"This One" "This One" ... and so on.

Am I missing something I didnt read up on correctly?

Thanks,

Miro

================ code ================
Public Class SystemFilesClass
'Public SystemFilePublicData As String = "Miros Public Data"

Shared FieldName As String

Shared Property Prop_FieldName() As String
Get
Return FieldName
End Get
Set(ByVal Value As String)
FieldName = Value
End Set
End Property

End Class

====== somewhere else in code

Dim BlaSys(3) As SystemFilesClass
BlaSys(1).Prop_FieldName() = "Hi"
BlaSys(2).Prop_FieldName() = "Bye"
BlaSys(3).Prop_FieldName() = "This One"

MsgBox("test me=" & BlaSys.Length.ToString)

Dim nCounter As Integer
For nCounter = 1 To BlaSys.Length
MsgBox("blasys is " & BlaSys(nCounter).Prop_FieldName & " " &
nCounter.ToString) 'Each msgbox returns "THIS ONE" ?
Next nCounter
Aug 23 '06 #1
4 1305

Miro wrote:
Vb2003, im still learning vb.net but I do not understand my output from this
logic.
If someone can help me out here.

Cor Ligthert, you I believe were on the right track of what Im trying to
create.
-Thank you very much for leading me on to Classes.

Ok here is my code. ( see below )

I have a class defined with 1 propertie and then create an Array of the
Class and msgbox out my output.

Why does each msgbox that shows bring up the value "This One" ? even
though the counter increments.
I would have assumed I should have gotten, "Hi" "Bye" "This One"
instead of
"This One" "This One" ... and so on.

Am I missing something I didnt read up on correctly?

Thanks,

Miro

================ code ================
Public Class SystemFilesClass
'Public SystemFilePublicData As String = "Miros Public Data"

Shared FieldName As String

Shared Property Prop_FieldName() As String
Get
Return FieldName
End Get
Set(ByVal Value As String)
FieldName = Value
End Set
End Property

End Class

====== somewhere else in code

Dim BlaSys(3) As SystemFilesClass
BlaSys(1).Prop_FieldName() = "Hi"
BlaSys(2).Prop_FieldName() = "Bye"
BlaSys(3).Prop_FieldName() = "This One"

MsgBox("test me=" & BlaSys.Length.ToString)

Dim nCounter As Integer
For nCounter = 1 To BlaSys.Length
MsgBox("blasys is " & BlaSys(nCounter).Prop_FieldName & " " &
nCounter.ToString) 'Each msgbox returns "THIS ONE" ?
Next nCounter
FieldName is declared as Shared. That means that there is only one
instance of that variable no matter how many objects you create. That
means in your loop you only see the last assignment, since there is
only one FieldName :) Declare FieldName as private and remove the
shared:

Private FieldName As String

and you should get the behavior your after.

--
Tom Shelton

Aug 24 '06 #2
Man I was going nuts.

At least in learning this lesson, Ill never do that again.

Thanks

Miro

"Tom Shelton" <to*@mtogden.comwrote in message
news:11********************@b28g2000cwb.googlegrou ps.com...
>
Miro wrote:
>Vb2003, im still learning vb.net but I do not understand my output from
this
logic.
If someone can help me out here.

Cor Ligthert, you I believe were on the right track of what Im trying to
create.
-Thank you very much for leading me on to Classes.

Ok here is my code. ( see below )

I have a class defined with 1 propertie and then create an Array of the
Class and msgbox out my output.

Why does each msgbox that shows bring up the value "This One" ? even
though the counter increments.
I would have assumed I should have gotten, "Hi" "Bye" "This One"
instead of
"This One" "This One" ... and so on.

Am I missing something I didnt read up on correctly?

Thanks,

Miro

================ code ================
Public Class SystemFilesClass
'Public SystemFilePublicData As String = "Miros Public Data"

Shared FieldName As String

Shared Property Prop_FieldName() As String
Get
Return FieldName
End Get
Set(ByVal Value As String)
FieldName = Value
End Set
End Property

End Class

====== somewhere else in code

Dim BlaSys(3) As SystemFilesClass
BlaSys(1).Prop_FieldName() = "Hi"
BlaSys(2).Prop_FieldName() = "Bye"
BlaSys(3).Prop_FieldName() = "This One"

MsgBox("test me=" & BlaSys.Length.ToString)

Dim nCounter As Integer
For nCounter = 1 To BlaSys.Length
MsgBox("blasys is " & BlaSys(nCounter).Prop_FieldName & " " &
nCounter.ToString) 'Each msgbox returns "THIS ONE" ?
Next nCounter

FieldName is declared as Shared. That means that there is only one
instance of that variable no matter how many objects you create. That
means in your loop you only see the last assignment, since there is
only one FieldName :) Declare FieldName as private and remove the
shared:

Private FieldName As String

and you should get the behavior your after.

--
Tom Shelton

Aug 24 '06 #3
Miro,

Tom has helped you almost completely. I think it is better not to ask only
for my help: as by AFAIK everybody else is my knowledge limited. By asking
for my help you limit your answers.

Tom has showed that you need a non shared class.

However adding something to that (and see that it is a newsgroup message
which I have changed a lot so keep in mind that I can have written or
corrected something wrong)

You have mixed up the shared keyword which tells where something is placed
in memory and the scope. Probably you have the option explicit off. I would
not do the later.

Private, Public and Friend tells what is the autorised scope. Those three
cannot be used in a method because every declaration inside a method is
Private. By instance a declaration of an integer in the sentence "For
myIndex as integer" means that "myIndex" is only accessible on the level it
is placed inside the method.
Private is forever inside the Class; Friend only inside your project and
Public completely everywhere even as your project is used as a library.
There is too the keyword Protected which means that it can only be used in
derived classes.

You will direct think on the by me hated keyword Dim. In my eyes it is
completely not needed but it can at the moment not be used optional except
if you set option explicit off. The Dim has other effects that I don't like.
Dim placed on Module level is public and on every other place private.

Shared is completely something else, it tells where something is placed in
memory and when set something is usable shared for every method as long as
it is not protected by its scope. That never can be an object and therefore
in my opinion has a Shared non main class not much to do with OOP but is
derived from classic use from C. (Don't mix up the C Static keyword with the
VB Static keyword. The VB static keyword means in fact that the declared
value is really static inside a methode. It will not loose its value that is
given to it. VB Static is a little bit crazy keyword handy, but too not
really OOP.

To explain that in another way.
Everything that is shared is placed in the main stack of a program. The same
as a module in fact is everything inside a module or shared class static to
the program.

You could have written your code as well like this if that was what you
wanted (and you did not).
\\\
Public Module SystemFilesModule
Private mFieldName As String
Property Prop_FieldName() As String
Get
Return FieldName
End Get
Set(ByVal Value As String)
FieldName = Value
End Set
End Property
End Module
///

The same as with your shared class was than that FieldName everywhere
accesible in your program or outside that (when a reference was set) as
SystemFilesModule.Fieldname.

I find a module nicer by the way than a shared class but in my opinion it is
in fact no OOP (the later sentence except for the mainstack needed to handle
the objects).

If you instance an object than you create an object on the managed heap.
That will be destroyed when used and not needed anymore by the GC and has
therefore a nicer memory management. Therefore you can use instanced classes
(objects) as much as you want, a module (or shared class) is one fixed part
of memory inside your program.

I hope this helps.

Cor


"Miro" <mi******@golden.netschreef in bericht
news:uf**************@TK2MSFTNGP03.phx.gbl...
Man I was going nuts.

At least in learning this lesson, Ill never do that again.

Thanks

Miro

"Tom Shelton" <to*@mtogden.comwrote in message
news:11********************@b28g2000cwb.googlegrou ps.com...
>>
Miro wrote:
>>Vb2003, im still learning vb.net but I do not understand my output from
this
logic.
If someone can help me out here.

Cor Ligthert, you I believe were on the right track of what Im trying to
create.
-Thank you very much for leading me on to Classes.

Ok here is my code. ( see below )

I have a class defined with 1 propertie and then create an Array of the
Class and msgbox out my output.

Why does each msgbox that shows bring up the value "This One" ? even
though the counter increments.
I would have assumed I should have gotten, "Hi" "Bye" "This One"
instead of
"This One" "This One" ... and so on.

Am I missing something I didnt read up on correctly?

Thanks,

Miro

================ code ================
Public Class SystemFilesClass
'Public SystemFilePublicData As String = "Miros Public Data"

Shared FieldName As String

Shared Property Prop_FieldName() As String
Get
Return FieldName
End Get
Set(ByVal Value As String)
FieldName = Value
End Set
End Property

End Class

====== somewhere else in code

Dim BlaSys(3) As SystemFilesClass
BlaSys(1).Prop_FieldName() = "Hi"
BlaSys(2).Prop_FieldName() = "Bye"
BlaSys(3).Prop_FieldName() = "This One"

MsgBox("test me=" & BlaSys.Length.ToString)

Dim nCounter As Integer
For nCounter = 1 To BlaSys.Length
MsgBox("blasys is " & BlaSys(nCounter).Prop_FieldName & " " &
nCounter.ToString) 'Each msgbox returns "THIS ONE" ?
Next nCounter

FieldName is declared as Shared. That means that there is only one
instance of that variable no matter how many objects you create. That
means in your loop you only see the last assignment, since there is
only one FieldName :) Declare FieldName as private and remove the
shared:

Private FieldName As String

and you should get the behavior your after.

--
Tom Shelton


Aug 24 '06 #4
Original post wasnt meant just for your answer (Cor ) - just thanking you
from the other post when you suggested the class.
My 3 books i have only go so far with examples but their examples in real
life applications are limited. The books try to
show ( and do show ) successfully how to create a class with a property /
and meathods and call on them. But it doesnt
really give you the idea that you can create an array of them.
Example: Dim Bla() As ClassCreated
I never even thought that vb could do that. Opens a whole new world in my
mind now.

I do appreciate your Posts / Replys and everyone elses just as much.

I dont think learning VB.net or maybe even another language - is fully
possible without a newsgroup.
Some things books just cant teach - or throw you in the right direction
properly.

Thanks again.

Miro



"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:Ov**************@TK2MSFTNGP04.phx.gbl...
Miro,

Tom has helped you almost completely. I think it is better not to ask only
for my help: as by AFAIK everybody else is my knowledge limited. By asking
for my help you limit your answers.

Tom has showed that you need a non shared class.

However adding something to that (and see that it is a newsgroup message
which I have changed a lot so keep in mind that I can have written or
corrected something wrong)

You have mixed up the shared keyword which tells where something is placed
in memory and the scope. Probably you have the option explicit off. I
would not do the later.

Private, Public and Friend tells what is the autorised scope. Those three
cannot be used in a method because every declaration inside a method is
Private. By instance a declaration of an integer in the sentence "For
myIndex as integer" means that "myIndex" is only accessible on the level
it is placed inside the method.
Private is forever inside the Class; Friend only inside your project and
Public completely everywhere even as your project is used as a library.
There is too the keyword Protected which means that it can only be used in
derived classes.

You will direct think on the by me hated keyword Dim. In my eyes it is
completely not needed but it can at the moment not be used optional except
if you set option explicit off. The Dim has other effects that I don't
like. Dim placed on Module level is public and on every other place
private.

Shared is completely something else, it tells where something is placed in
memory and when set something is usable shared for every method as long as
it is not protected by its scope. That never can be an object and
therefore in my opinion has a Shared non main class not much to do with
OOP but is derived from classic use from C. (Don't mix up the C Static
keyword with the VB Static keyword. The VB static keyword means in fact
that the declared value is really static inside a methode. It will not
loose its value that is given to it. VB Static is a little bit crazy
keyword handy, but too not really OOP.

To explain that in another way.
Everything that is shared is placed in the main stack of a program. The
same as a module in fact is everything inside a module or shared class
static to the program.

You could have written your code as well like this if that was what you
wanted (and you did not).
\\\
Public Module SystemFilesModule
Private mFieldName As String
Property Prop_FieldName() As String
Get
Return FieldName
End Get
Set(ByVal Value As String)
FieldName = Value
End Set
End Property
End Module
///

The same as with your shared class was than that FieldName everywhere
accesible in your program or outside that (when a reference was set) as
SystemFilesModule.Fieldname.

I find a module nicer by the way than a shared class but in my opinion it
is in fact no OOP (the later sentence except for the mainstack needed to
handle the objects).

If you instance an object than you create an object on the managed heap.
That will be destroyed when used and not needed anymore by the GC and has
therefore a nicer memory management. Therefore you can use instanced
classes (objects) as much as you want, a module (or shared class) is one
fixed part of memory inside your program.

I hope this helps.

Cor


"Miro" <mi******@golden.netschreef in bericht
news:uf**************@TK2MSFTNGP03.phx.gbl...
>Man I was going nuts.

At least in learning this lesson, Ill never do that again.

Thanks

Miro

"Tom Shelton" <to*@mtogden.comwrote in message
news:11********************@b28g2000cwb.googlegro ups.com...
>>>
Miro wrote:
Vb2003, im still learning vb.net but I do not understand my output from
this
logic.
If someone can help me out here.

Cor Ligthert, you I believe were on the right track of what Im trying
to
create.
-Thank you very much for leading me on to Classes.

Ok here is my code. ( see below )

I have a class defined with 1 propertie and then create an Array of the
Class and msgbox out my output.

Why does each msgbox that shows bring up the value "This One" ? even
though the counter increments.
I would have assumed I should have gotten, "Hi" "Bye" "This One"
instead of
"This One" "This One" ... and so on.

Am I missing something I didnt read up on correctly?

Thanks,

Miro

================ code ================
Public Class SystemFilesClass
'Public SystemFilePublicData As String = "Miros Public Data"

Shared FieldName As String

Shared Property Prop_FieldName() As String
Get
Return FieldName
End Get
Set(ByVal Value As String)
FieldName = Value
End Set
End Property

End Class

====== somewhere else in code

Dim BlaSys(3) As SystemFilesClass
BlaSys(1).Prop_FieldName() = "Hi"
BlaSys(2).Prop_FieldName() = "Bye"
BlaSys(3).Prop_FieldName() = "This One"

MsgBox("test me=" & BlaSys.Length.ToString)

Dim nCounter As Integer
For nCounter = 1 To BlaSys.Length
MsgBox("blasys is " & BlaSys(nCounter).Prop_FieldName & " " &
nCounter.ToString) 'Each msgbox returns "THIS ONE" ?
Next nCounter

FieldName is declared as Shared. That means that there is only one
instance of that variable no matter how many objects you create. That
means in your loop you only see the last assignment, since there is
only one FieldName :) Declare FieldName as private and remove the
shared:

Private FieldName As String

and you should get the behavior your after.

--
Tom Shelton



Aug 24 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Developwebsites | last post by:
const int MAX=999; class person { protected: char firstname, lastname; int ID; public: person();
1
by: m830266 | last post by:
I'm trying to use the APAX serial I/O control (www.turbocontrol.com/AProZilla.htm) in a VB.NET project and I'm having trouble with its 'data received' events. The data is supplied by APAX as a...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
13
by: Karl Groves | last post by:
I'm missing something very obvious, but it is getting late and I've stared at it too long. TIA for responses I am writing a basic function (listed at the bottom of this post) that returns...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.