dcsimg
Results 1 to 7 of 7

Thread: Cross class updating.

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2017
    Posts
    19

    Cross class updating.

    Hi folks.

    I have a situation where I have 3 different classes which hold a sub class (list) of objects. Each list (May) contain records which are in another class list.

    So the records may be spread as follows
    Class A 1,3,5,7,9
    Class B 2,4,5,6,9
    Class C 1,2,4,7,8

    Now, if I update record 9 in class A, I'd like the record 9 in Class B to be updated.
    Simarly if I update record 4 in class C I'd like the record in Class B to be updated.

    Speed is of the essence and I need the absolute fastest way of doing this. I can loop the lists, but there could be be 1,000 records in each list.

    I have a 'dirty' flag so I'm going to write a timed loop for now, but wondering if there is a way to somehow link the classes?

    Any ideas?

    Thanks.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    33,985

    Re: Cross class updating.

    The description lacks a crucial bit of information, but as stated, it looks like it will work as you want it to without any additional effort. The key information is "what is a record?".

    If records are any type of class, then all the list actually holds is the reference of the object. Therefore, since class B holds record 4 and class C also holds record 4, then if the record is a class, both B and C hold nothing more than a reference to the record. Essentially, the actual record is sitting out in memory somewhere, and both B and C hold the address of that memory. Any change to the record made through either class is visible to both, because there is really only just the one object and the two classes just hold its address. So, as long as the record is a reference type (a class), then changing a record changes the record, regardless of which class (A, B, or C) you go through to make the change.

    However, if a record is a value type, which seems unlikely, then the lists hold different copies of the information. This seems quite improbable, because in such a case there really is no record. Each class just holds a copy of the information and they are all independent of one another. If this was the case, you likely wouldn't even be thinking in terms of records, because the 4 in class B would be a wholly different thing from the 4 in class C.

    Therefore, I think it will likely work as you want the way you have it.
    My usual boring signature: Nothing

  3. #3
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    4,169

    Re: Cross class updating.

    Like sh said "what is a record" and I'd like to add this,

    If you are talking about database records that are in something like a datatable then I don't like this "timer" idea. Instead, all three forms should be using the same datatable.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,744

    Re: Cross class updating.

    One point to note with Shaggy's explanation is that, even if a record is a reference type, it depends whether you added the same instance to the lists in the first place or you created two instances with the same data. Presumably you wree trying to be as general as possible with your explanation but I think that you actually need to be specific. Show us the actual type definitions, minus the irrelevant members, and show us how you're populating the lists. If each type is a class and you're using the same record instance to represent the same data then, as Shaggy says, it will just work. The fact that you're asking this question in the first place seems to suggest that that is not the case though, or else you just didn't bother to test before assuming that it didn't work as is.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    33,985

    Re: Cross class updating.

    The way I read the question was that the OP was kicking around a design concept and hadn't yet put much flesh on those bones. So, there may not BE any more specific code, at this point. Therefore, my point was: Go for it.
    My usual boring signature: Nothing

  6. #6
    Addicted Member
    Join Date
    Apr 2008
    Posts
    193

    Re: Cross class updating.

    Would the Observer Design Pattern work in this case?
    You would not have to loop through anything, just handle a notification when the state of an object changes
    ...
    If someone helps you, show it by rating their post!
    VB.net Code:
    1. ' These two lines will make your coding life much easier!

    "Check everything. That's what software developers do." jmcilhinney

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    33,985

    Re: Cross class updating.

    It might, but it isn't clear whether or not that would even be necessary, yet. If the items in the list are all reference types, and they are not all clones, then it wouldn't be necessary to raise events. It might still be desirable to raise events to let all the holding classes know when any one of them changes something, but that's not directly part of the question. If the items in the lists are all clones of one another, then it would be useful to raise events so that each holding class can know to update their clones as needed.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width