PC Review
Forums
Newsgroups
Microsoft DotNet
Microsoft VB .NET
Array of a Class - Weird Output
Forums
Newsgroups
Microsoft DotNet
Microsoft VB .NET
Array of a Class - Weird Output
![]() |
Array of a Class - Weird Output |
|
|
Thread Tools | Rate Thread |
|
|
#1 |
|
Guest
Posts: n/a
|
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 |
|
|
|
#2 |
|
Guest
Posts: n/a
|
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 theshared: Private FieldName As String and you should get the behavior your after. -- Tom Shelton |
|
|
|
#3 |
|
Guest
Posts: n/a
|
Man I was going nuts.
At least in learning this lesson, Ill never do that again. Thanks Miro "Tom Shelton" <tom@mtogden.com> wrote in message news:1156392319.616517.4480@b28g2000cwb.googlegroups.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 > |
|
|
|
#4 |
|
Guest
Posts: n/a
|
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" <mironagy@golden.net> schreef in bericht news:ufglwozxGHA.3632@TK2MSFTNGP03.phx.gbl... > Man I was going nuts. > > At least in learning this lesson, Ill never do that again. > > Thanks > > Miro > > "Tom Shelton" <tom@mtogden.com> wrote in message > news:1156392319.616517.4480@b28g2000cwb.googlegroups.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 >> > > |
|
|
|
#5 |
|
Guest
Posts: n/a
|
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]" <notmyfirstname@planet.nl> wrote in message news:OvkqVo0xGHA.1256@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" <mironagy@golden.net> schreef in bericht > news:ufglwozxGHA.3632@TK2MSFTNGP03.phx.gbl... >> Man I was going nuts. >> >> At least in learning this lesson, Ill never do that again. >> >> Thanks >> >> Miro >> >> "Tom Shelton" <tom@mtogden.com> wrote in message >> news:1156392319.616517.4480@b28g2000cwb.googlegroups.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 >>> >> >> > > |
|
![]() |
|
| Thread Tools | |
| Rate This Thread | |
|
|

Main Page 

Declare FieldName as private and remove the
