Referencing an object(RichTextBox more precisely) from within a class.

Hi,

I would like to pass an object to a class through the constructor so that every function from that class has access
to that object. More specifically, I would like to have control over a RichTextBox from my custom class so that I don't have to
pass the object as argument everytime I use a function from my class.

Thank you!
PatricePaquetteAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
PaulHewsConnect With a Mentor Commented:
Declare a class level variable to store the textbox.  If needed you can use WithEvents and sink events for the RichTextBox...  If needed we can show you how.


Public Class MyRTBClass
    Private mRichTextBox As RichTextBox
 
    Public Sub New(ByVal RTB As RichTextBox)
        mRichTextBox = RTB
    End Sub
    'Sub/function that uses class level variable.
    Public Function GetText() As String
        Return mRichTextBox.Text
 
    End Function
End Class

Open in new window

0
 
PatricePaquetteAuthor Commented:
This is exactly what I wanted, thanks alot.
I was under the impression that this wouldn't work because it would only copy the object and not
actually reference it. I guess I was wrong.
0
 
PaulHewsCommented:
>because it would only copy the object

Actually, it makes a copy of the variable that references the RichTextBox.  Think of object variables as references to object instances.  Setting one object variable = to another only makes another copy of the reference.  There is still only a single instance.

Dim x, y as MyClassThingy  'Two object references
x = new MyClassThingy       'New instance of the object.
y = x                                    'Both object references point to the same instance.
0
 
PatricePaquetteAuthor Commented:
Thank you very much for clearing this up for me.
0
All Courses

From novice to tech pro — start learning today.