dcsimg
Results 1 to 5 of 5

Thread: User control, 2 instances in the same .net app

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2000
    Location
    Manchester NH
    Posts
    833

    User control, 2 instances in the same .net app

    I have a custom user control as part of my "solution".

    I load the control twice at runtime on 2 different forms within the same app



    Private WithEvents CG As CG.WINDOW

    ' later

    CG = New CG.WINDOW




    an event in one of the user controls on one form may execute the event in the other.

    very frustrating

    how can I completely separate them ?


    (this is a VB6 interop user control)
    Last edited by kurtsimons; Aug 11th, 2013 at 03:00 PM.
    Kurt Simons
    [I know I'm a hack but my clients don't!]

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

    Re: User control, 2 instances in the same .net app

    Something is entangled between the two. Exactly what that is can't be divined through pure guesswork all that easily, but the general point is this: All variables that are reference types (ALL classes, including forms and controls) don't hold the actual object, they just hold the location in memory where the object is located. Therefore:

    Code:
    Public Class Foo
     'Something
    End Class
    
    Dim A As New Foo
    Dim B As Foo
    
    B = A
    B and A are two different variables, but there is only one Foo object, and both B and A hold its address. You are doing something like that when you create the usercontrol. Most likely, this is happening in the constructor, and you may be able to find it by looking for a line as simple as B = A where A and B are some class (probably the control that you are seeing this issue with). It doesn't have to be that easy, but that's the problem you are having. The two controls are actually the same control with two different references to them in each of the two different user controls. How that can be is what you have to figure out.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2000
    Location
    Manchester NH
    Posts
    833

    Re: User control, 2 instances in the same .net app

    the problem is that my usercontrol has a module with some public variables and subroutines.

    when I load two of the same control on my form they share these public variables.

    how can I force these two usercontrols to be completely separate?

    if I create two windows applications, and they both use the control then the two are separate. I want this functionality but in the same executable?
    Kurt Simons
    [I know I'm a hack but my clients don't!]

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,803

    Re: User control, 2 instances in the same .net app

    Don't use a module... if they were intended to be independant of all instances of the UC, they should have been in the UC to begin with. The globalness of things in the module happens because modules by nature are shared... meaning they are not instance dependant (or instance independant) ... they are agnostic as it were.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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

    Re: User control, 2 instances in the same .net app

    Every object should be independent of all others. All the data it needs to work on should be its own local data or data supplied by some outside agency via arguments to properties, constructors, or other methods. When you introduce a module and have the class work with it, the very existence and proper functioning of the class is now dependent on the existence of the module. That's bad. The fact that you now entangle the class with global state data or global methods (all methods and data in a module), means that the class is now tied to something that can be altered by a third party. In this case, it is being altered by the fact that the data is shared with another instance of the class.

    One thing to know is that a module is quietly turned into a class with all Shared members. Therefore, anything you write in a module you could have written in a class and marked it Shared. That has lots of ramifications that are often not welcome, as they are not welcome in this case.
    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