Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. Class of Classes!

Class of Classes!

Scheduled Pinned Locked Moved Visual Basic
saleshelpquestion
12 Posts 5 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alex Feature
    wrote on last edited by
    #1

    Hi Guys, Stupid question! :confused: I got a collection class, Customers, that should store classes of type Customer. Simple enough task. BUT!!! for some reason after I add my first customer class to the collection class every time I instantiate a new customer class the customer that is already in the collection gets the same data as the new customer. So in the end I get a ton of classes in the collection identical to the last class added. The customer class has a private structure to hold customer info and the add method of the collection class passes the new customer class directly into the collection. The new method of the Customer class gets the params byval and fills the structure so i don't how the previous instances of the class already in the collection assume that they need to change. :confused: The whole thing is done vb2005. Any kind of light shed on the issue would be greatly appreciated The perfect code is always in your head...it just a matter of how hard you bang your head against the wall to get it out!

    R 1 Reply Last reply
    0
    • A Alex Feature

      Hi Guys, Stupid question! :confused: I got a collection class, Customers, that should store classes of type Customer. Simple enough task. BUT!!! for some reason after I add my first customer class to the collection class every time I instantiate a new customer class the customer that is already in the collection gets the same data as the new customer. So in the end I get a ton of classes in the collection identical to the last class added. The customer class has a private structure to hold customer info and the add method of the collection class passes the new customer class directly into the collection. The new method of the Customer class gets the params byval and fills the structure so i don't how the previous instances of the class already in the collection assume that they need to change. :confused: The whole thing is done vb2005. Any kind of light shed on the issue would be greatly appreciated The perfect code is always in your head...it just a matter of how hard you bang your head against the wall to get it out!

      R Offline
      R Offline
      Robert Rohde
      wrote on last edited by
      #2

      Hard to answer without some code. Do you have some Shared fields within that customer class?

      A 1 Reply Last reply
      0
      • R Robert Rohde

        Hard to answer without some code. Do you have some Shared fields within that customer class?

        A Offline
        A Offline
        Alex Feature
        wrote on last edited by
        #3

        Hi Robert Nope nothin is shared. All vars are private and if anything is passed to the Customer class it's byval. Here is the Customer class: Public Class clsSalesRecord Private Record As Object Public Sub New(ByVal SalesRecordObject As Object) Record = SalesRecordObject End Sub Public ReadOnly Property Reference() As String Get Return Record.id End Get End Property End Class Here is the collection class: Public Class clsSalesRecords private _Records As Generic.List(Of clsSalesRecord) Public Sub Refresh(byval Record as object) _Records.Add(New clsSalesRecord(Record)) End Sub HELP!!! The perfect code is always in your head...it just a matter of how hard you bang your head against the wall to get it out!

        R M 2 Replies Last reply
        0
        • A Alex Feature

          Hi Robert Nope nothin is shared. All vars are private and if anything is passed to the Customer class it's byval. Here is the Customer class: Public Class clsSalesRecord Private Record As Object Public Sub New(ByVal SalesRecordObject As Object) Record = SalesRecordObject End Sub Public ReadOnly Property Reference() As String Get Return Record.id End Get End Property End Class Here is the collection class: Public Class clsSalesRecords private _Records As Generic.List(Of clsSalesRecord) Public Sub Refresh(byval Record as object) _Records.Add(New clsSalesRecord(Record)) End Sub HELP!!! The perfect code is always in your head...it just a matter of how hard you bang your head against the wall to get it out!

          R Offline
          R Offline
          Robert Rohde
          wrote on last edited by
          #4

          Are you always passing the same Record object into the Refresh sub? If yes then surely all your customers will point to the same reference...

          A 1 Reply Last reply
          0
          • R Robert Rohde

            Are you always passing the same Record object into the Refresh sub? If yes then surely all your customers will point to the same reference...

            A Offline
            A Offline
            Alex Feature
            wrote on last edited by
            #5

            :laugh: I appreciate the vote of confidence! :laugh: No!!! Thats the thing, when the first class is added to the colection it has it's set of values. Then when i instantiate a fresh Customer class with a new record object and even before the new customer class is added to the collection the class already in the collection changes it's values to those of the fresh Customer. (My bad! I don't think I made this clear before :-O) So it seems as if the collection is holding onto a ref of the customer class. This in turn is strange as you can see that Customer classes are passed byval into the collection. Class params passed byval should preserve the structure of the reference class but not be references themselves....No? Thanks for givin me hope The perfect code is always in your head...it just a matter of how hard you bang your head against the wall to get it out!

            M 1 Reply Last reply
            0
            • A Alex Feature

              :laugh: I appreciate the vote of confidence! :laugh: No!!! Thats the thing, when the first class is added to the colection it has it's set of values. Then when i instantiate a fresh Customer class with a new record object and even before the new customer class is added to the collection the class already in the collection changes it's values to those of the fresh Customer. (My bad! I don't think I made this clear before :-O) So it seems as if the collection is holding onto a ref of the customer class. This in turn is strange as you can see that Customer classes are passed byval into the collection. Class params passed byval should preserve the structure of the reference class but not be references themselves....No? Thanks for givin me hope The perfect code is always in your head...it just a matter of how hard you bang your head against the wall to get it out!

              M Offline
              M Offline
              mbuller
              wrote on last edited by
              #6

              I was wondering what DB are you using? I might be able to give you an alternate way of looking at this if I knew... Michael

              A 1 Reply Last reply
              0
              • M mbuller

                I was wondering what DB are you using? I might be able to give you an alternate way of looking at this if I knew... Michael

                A Offline
                A Offline
                Alex Feature
                wrote on last edited by
                #7

                Hi Michael I'm using Access but I don't store this type of info in it anyway. Basically my app is an add-on extending an accounting software with its own proprietary db. My app is using a separate db to store some info and i don't need to keep the customers etc. on my side. Hence these classes are written as wrappers for a very !!!UGLY!!! sdk class library supplied with the software. And at this point it's a matter of principle :mad: for me to understand why this issue arises with collections of classes in .net. If anyone could throw my way some code with a class collection I would greatly appreciate it. I just want to know what :wtf: I’m doing wrong. Thanks in advance The perfect code is always in your head...it just a matter of how hard you bang your head against the wall to get it out!

                M 1 Reply Last reply
                0
                • A Alex Feature

                  Hi Michael I'm using Access but I don't store this type of info in it anyway. Basically my app is an add-on extending an accounting software with its own proprietary db. My app is using a separate db to store some info and i don't need to keep the customers etc. on my side. Hence these classes are written as wrappers for a very !!!UGLY!!! sdk class library supplied with the software. And at this point it's a matter of principle :mad: for me to understand why this issue arises with collections of classes in .net. If anyone could throw my way some code with a class collection I would greatly appreciate it. I just want to know what :wtf: I’m doing wrong. Thanks in advance The perfect code is always in your head...it just a matter of how hard you bang your head against the wall to get it out!

                  M Offline
                  M Offline
                  mbuller
                  wrote on last edited by
                  #8

                  Ok Im Looking Through and trying to find where you call your refresh... It seems to me that its passing the New Customer in Correctly... but the Refresh refreshes all of the info in the list to the newly saved customer... Tell me what you think while I try and write this ... I use Stored procs for all of my database access so this is new to me as well.. so go easy on me but, i will give this an attempt and if anything everyone else will eb able to correct me and give you more insight. Michael

                  M 1 Reply Last reply
                  0
                  • M mbuller

                    Ok Im Looking Through and trying to find where you call your refresh... It seems to me that its passing the New Customer in Correctly... but the Refresh refreshes all of the info in the list to the newly saved customer... Tell me what you think while I try and write this ... I use Stored procs for all of my database access so this is new to me as well.. so go easy on me but, i will give this an attempt and if anything everyone else will eb able to correct me and give you more insight. Michael

                    M Offline
                    M Offline
                    Mel Padden
                    wrote on last edited by
                    #9

                    Hey there. Don't kill me but... I'm not that familiar with the list...of collection. If ever I need to do this kind of thing I just use a Collection variable. The only thing I can see is that the call to Sub New in you Refresh is comehow calling across all your clsSalesrecords held in mem. Sounds insane. Anywho, is there a need to use the List type? just slap 'em all into a simple collection, and see if that makes a difference. If so, at least you'll know exactly where it's happening, though why is beyond me.

                    1 Reply Last reply
                    0
                    • A Alex Feature

                      Hi Robert Nope nothin is shared. All vars are private and if anything is passed to the Customer class it's byval. Here is the Customer class: Public Class clsSalesRecord Private Record As Object Public Sub New(ByVal SalesRecordObject As Object) Record = SalesRecordObject End Sub Public ReadOnly Property Reference() As String Get Return Record.id End Get End Property End Class Here is the collection class: Public Class clsSalesRecords private _Records As Generic.List(Of clsSalesRecord) Public Sub Refresh(byval Record as object) _Records.Add(New clsSalesRecord(Record)) End Sub HELP!!! The perfect code is always in your head...it just a matter of how hard you bang your head against the wall to get it out!

                      M Offline
                      M Offline
                      mr_lasseter
                      wrote on last edited by
                      #10

                      What you have should work with the exception that _Records never gets created. private _Records As Generic.List(Of clsSalesRecord) should be private _Records As New Generic.List(Of clsSalesRecord) Where is the code that is calling the Refresh method? Mike Lasseter -- modified at 17:29 Friday 21st July, 2006

                      A 1 Reply Last reply
                      0
                      • M mr_lasseter

                        What you have should work with the exception that _Records never gets created. private _Records As Generic.List(Of clsSalesRecord) should be private _Records As New Generic.List(Of clsSalesRecord) Where is the code that is calling the Refresh method? Mike Lasseter -- modified at 17:29 Friday 21st July, 2006

                        A Offline
                        A Offline
                        Alex Feature
                        wrote on last edited by
                        #11

                        Hi Guys, First off all, thanks for all the replies. Like i said before the sdk is UUUGLY! so the refresh method would make no sense to you guys anyway without the supporting docs. In short all it does is assign Nothing to the Customer class and loop through something that was meant to be a collection in the SDK classes etc. In any case you're fixating on the wrong thing. The collection either way i declare it generates the exact same issue. I even tried this with a structure called Customer instead of a class and the structure would have a private var to store stuff. The exact same ..it happenz! As Michael said i pass the Customer in correctly, the collection, God love it!, is properly initialized and gets its first element fine. BUT the moment i Nothing the Customer class instance that was passed BYVAL to the frigin collection, the instance of the class in the collection gets Nothinged as well. HOW THE ... ... .... ......... IS THAT POSSIBLE????????????? The instance that gets Nothing is outside the scope of the sub or the collection class, and even if it wasn't it's passed BYVAL for crying out loud. That’s my current concern! X| I really appreciate your assistance but I will havta end my life now...probably very violently! :((

                        The perfect code is always in your head...it just a matter of how hard you bang your head against the wall to get it out!

                        M 1 Reply Last reply
                        0
                        • A Alex Feature

                          Hi Guys, First off all, thanks for all the replies. Like i said before the sdk is UUUGLY! so the refresh method would make no sense to you guys anyway without the supporting docs. In short all it does is assign Nothing to the Customer class and loop through something that was meant to be a collection in the SDK classes etc. In any case you're fixating on the wrong thing. The collection either way i declare it generates the exact same issue. I even tried this with a structure called Customer instead of a class and the structure would have a private var to store stuff. The exact same ..it happenz! As Michael said i pass the Customer in correctly, the collection, God love it!, is properly initialized and gets its first element fine. BUT the moment i Nothing the Customer class instance that was passed BYVAL to the frigin collection, the instance of the class in the collection gets Nothinged as well. HOW THE ... ... .... ......... IS THAT POSSIBLE????????????? The instance that gets Nothing is outside the scope of the sub or the collection class, and even if it wasn't it's passed BYVAL for crying out loud. That’s my current concern! X| I really appreciate your assistance but I will havta end my life now...probably very violently! :((

                          The perfect code is always in your head...it just a matter of how hard you bang your head against the wall to get it out!

                          M Offline
                          M Offline
                          mr_lasseter
                          wrote on last edited by
                          #12

                          I don't think you understand how ByVal works when using objects/classes. For objects (that is, reference types) ByVal means that the function being called cannot overwrite the *reference* to the object instance which is actually passed in the parameter variable. But it does not prevent the callee to alter properties on the instance. ByRef, on the other hand, enables the callee to overwrite the reference itself and therefore substitute the original object instance with another one.

                          Mike Lasseter

                          -- modified at 8:30 Monday 24th July, 2006

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups