VS 2012 Referencing a class in another classes-VBForums
Results 1 to 7 of 7

Thread: Referencing a class in another classes

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Location
    London, UK
    Posts
    16

    Thumbs up Referencing a class in another classes

    As you may already know, the default class for Kinect application in VB environment contains the following method:

    Code:
    Public Sub New()    
    InitializeComponent()
    End Sub
    In this class (MainWindow.vb) I have decleared some variables as well as TextBoxes which need to be changed/manipulated by another class (Changer.vb) In Changer.vb, I have created this:

    Code:
    Dim mainWin = New MainWindow
     mainWin.TextBox1.Text = “hello World”
    The problem is that whenever the compiler reaches “Dim mainWin = New MainWindow” point, it initiates another instance of that class and consequently runs “InitializeComponent()” of it which causes the Kinect sensor stop working as its components are already initialized. I do not what it to be reinitialized. I only need to access variables in this class from other classes. Is there any way to do so?

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

    Re: Referencing a class in another classes

    When you call New, you create a new instance. That will certainly call InitializeComponent, since that's what's in the constructor. What you need to do is create a single instance of that form (which it sounds like you have done), then pass that instance to the other classes you create. Those classes need to know about that instance, so give them a MainWindow as a member variable and pass in the single instance in the constructor for those other classes.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Location
    London, UK
    Posts
    16

    Re: Referencing a class in another classes

    Quote Originally Posted by Shaggy Hiker View Post
    When you call New, you create a new instance. That will certainly call InitializeComponent, since that's what's in the constructor. What you need to do is create a single instance of that form (which it sounds like you have done), then pass that instance to the other classes you create. Those classes need to know about that instance, so give them a MainWindow as a member variable and pass in the single instance in the constructor for those other classes.
    Thank you for your reply. Could you please give me an example? I am quite new.

    Regards.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    30,836

    Re: Referencing a class in another classes

    Here's a stub of a class that takes a MainWindow variable as an argument in the constructor:

    Code:
    Public SomeClass
     Private mMyMainWindow As MainClass
    
     Public Sub New(inWindow as MainClass)
      mMyMainWindow = inWindow
     End Sub
    
     Public Sub SomeMethod()
      mMyMainWindow.Textbox1.Text = "hello World"
     End Sub
    End Class
    To use it, you would create an instance and pass it the single instance of MainWindow that you have. In this example, I am creating the class instance from within that single instance of MainWindow:

    Code:
     Dim newClass As New SomeClass(Me) 'Passing in Me, which is the current instance of MainWindow
     
     newClass.SomeMethod
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Location
    London, UK
    Posts
    16

    Re: Referencing a class in another classes

    Thank you so much for this. The problem has been solved. But I remember in my past software I did not have that issue; does this happen only when there is a "Public new sub ()"?
    Moreover, I did not encounter this issue with Java. Could you please clarify as I am learning.

    Regards.

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    30,836

    Re: Referencing a class in another classes

    I don't know what you were doing before, so I can't say for certain what the issue is. What I showed is standard stuff for any Object Oriented language. I thought this might have something to do with the default instance of the form, which causes great confusion, but you don't show yourself using the default instances at all, which is quite right.

    Whenever you call New, you create a new object of whatever type you specify. There may be an actual Sub New that gets called, or there may not (they aren't necessary unless there is some kind of setup that the class needs to do, such as the call that forms make to InitializeComponents). Sub New is the constructor, and constructors exist in every object oriented language, though they are not called New in C-derived languages. If it were private, you wouldn't be able to create an instance of the form at all. Private constructors are used for some specialized types of classes such as Singletons. Therefore, nothing about this is driven by the fact that I added a Public Sub New().

    By the way, you can see the constructor for the forms, along with the code for InitializeComponents. These are located in the .designer.vb file for the form. They may be worth a look. You will see that InitializeComponents does nothing more than create all the controls and add them to the form. It's often a lot of code, but it is all stuff you could write yourself. The point of that file is so that you don't HAVE to write it yourself. If you create your own constructor for a form, the Public Sub New found in .designer.vb is removed automatically.

    Another thing to remember is that whenever you have a variable of a class type (forms are classes, too), the variable doesn't hold the actual object. All the variable holds is a reference to the actual object. Roughly speaking, this means that the object itself is somewhere out in RAM, and the variable just holds the address of the memory that the object occupies. When you call New, you allocate new memory and create an object in that memory, while getting back the address of that memory for later use. That meanst that you can pass form variables around, as I did in that snippet, and you are not passing the actual form around. All you are passing around is a copy of the address of the form. I don't know if that helps or not, but it may be part of the issue. You were creating new objects when you really just wanted to work with the existing objects. That's how it works in all object oriented languages, and every other language I can think of, except that there probably isn't a New in non-object oriented languages.
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Location
    London, UK
    Posts
    16

    Re: Referencing a class in another classes

    Thank you for your informative explanations. As I mentioned above, the issue has been resolved by using your instructions.

Tags for this Thread

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

Survey posted by VBForums.