Object and Class Confusion

Phreak

Regular
Joined
Jun 7, 2002
Messages
62
Location
Iowa, United States
Ok.

How do I use an object created in a DLL in my form.

For instance, I have Class Library named PB and a class named Comm. Now, I have a sub that creates a TCPClient in my Comm class.
In my form class I create a new object of Comm. And run the method to create the TCPClient.

How do I reference that in my form?

Here is my class code:
Visual Basic:
    ' --------------------------------------------------------------------------------------
    ' START GLOBAL DECLARATIONS
    ' --------------------------------------------------------------------------------------
    Const PORT_NUM As Int16 = 5000
    Const READ_BUFFER_SIZE As Integer = 255

    Public objClient As TcpClient
    Public Delegate Sub DisplayInvoker(ByVal t As String)
    ' --------------------------------------------------------------------------------------
    ' END GLOBAL DECLARATIONS
    ' --------------------------------------------------------------------------------------

    Public Sub CreateTCPClient()
        objClient = New TcpClient("localhost", PORT_NUM)
    End Sub

    ' Use a StreamWriter to send a message to server.
    Private Sub SendData(ByVal data As String)
        Dim writer As New IO.StreamWriter(objClient.GetStream)
        writer.Write(data & vbCr)
        writer.Flush()
    End Sub

End Class

In my form code I dimension and create a new PB.Comm object named Client. I then run the method CreateTCPClient in the OnLoad routine.

Any help on using that newly created objClient object in my form code? Or am I going about this the wrong way?
 
Hi - my suggestion would be:

Add a property to your class code and change the modifiers:
Visual Basic:
'Public objClient As TcpClient
'change to:
private objClient As TcpClient

Public Readonly Property CurrentClient as TcpClient
Get
 'check if you got a instance of one, and create if neccessary
  If objClient is Nothing Then
    CreateTCPClient()
  End If
  Return objClient
End Get
End Property

'you can now also change the modifier on your create function to private

This allow the form to ask for a tcpclient, without it needing to create one before it can use it. The responsibility of creating the tcpClient will now reside with your Class.

Hope it helps...

o, btw....

Use regions for comment blocks rather than the way you did it above, e.g.
Visual Basic:
#Region "GLOBAL DECLARATIONS"
  'your declarations here.....
#End Region

Cheers :)
 
Hmmm.... ok, maybe I'm not getting it or I didn't explain it very well. Here is my entire Comm class so far:

Visual Basic:
Option Explicit On 
Option Strict On

Imports System.Net.Sockets
Imports System.Text

Public Class Comm

    Public Sub New()
        MyBase.New()
    End Sub

#Region "Global Declarations"

    Const PORT_NUM As Int16 = 5000
    Const READ_BUFFER_SIZE As Integer = 255

    Private objClient As TcpClient

    Public Delegate Sub DisplayInvoker(ByVal t As String)

#End Region

    Private Sub CreateTCPClient()
        objClient = New TcpClient("localhost", PORT_NUM)
    End Sub

    ' Use a StreamWriter to send a message to server.
    Private Sub SendData(ByVal data As String)
        Dim writer As New IO.StreamWriter(objClient.GetStream)
        writer.Write(data & vbCr)
        writer.Flush()
    End Sub

    Public ReadOnly Property CurrentClient() As TcpClient
        Get
            'check if you got a instance of one, and create if neccessary
            If objClient Is Nothing Then
                CreateTCPClient()
            End If
            Return objClient
        End Get
    End Property

End Class

Now that's in the Comm.vb file in the PB class library. Here is the OnLoad method in the frmCChat.vb file in my Chat windows application.

Visual Basic:
Private Sub frmCMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            objClient = Client.CurrentClient()

            ' Start an asynchronous read invoking DoRead to avoid lagging the user interface.
            objClient.GetStream.BeginRead(readBuffer, 0, READ_BUFFER_SIZE, AddressOf DoRead, Nothing)

            ' Make sure the window is showing before popping up connection dialog.
            Me.Show()
            DisplayText("Connected to host" & vbCrLf)
            'AttemptLogin()
        Catch Ex As Exception
            MsgBox("Could not connect to server.  Please try again later.", _
                   MsgBoxStyle.Exclamation, Me.Text)
            Me.Dispose()
        End Try
    End Sub

Is the objClient = Client.CurrentClient line correct? (I already created the object Client in the global declarations). If it is correct, why is objClient not a TcpClient object? Such as the next line below it is not correct.
 
You don't need the 'objClient' object in the load event, if you already hold the 'Client' object on class level, e.g.
Visual Basic:
Private Sub frmCMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            ' Start an asynchronous read invoking DoRead to avoid lagging the user interface.
            Client.CurrentClient.GetStream.BeginRead(readBuffer, 0, READ_BUFFER_SIZE, AddressOf DoRead, Nothing)

            ' Make sure the window is showing before popping up connection dialog.
            Me.Show()
            DisplayText("Connected to host" & vbCrLf)
            'AttemptLogin()
        Catch Ex As Exception
            MsgBox("Could not connect to server.  Please try again later.", _
                   MsgBoxStyle.Exclamation, Me.Text)
            Me.Dispose()
        End Try
    End Sub
will work if you declared:
Visual Basic:
'you don't need to instanciate the object here, eg. you might want to do it in the load event...
private Client as PB.Comm = new PB.Comm
in the form..

Hope it's clearer
 
Well, my Client object isn't accessable in my other routines if I declare it in the OnLoad event, should I declare it globally? Would that work then? Would I just move the Dim Client As PB.Client = New PB.Client line to the global dec area?
 
Thats what I meant with:

will work if you declared:

VB:
--------------------------------------------------------------------------------
'you don't need to instanciate the object here, eg. you might want to do it in the load event...
Private Client As PB.Comm = New PB.Comm

--------------------------------------------------------------------------------


in the form..
 
Nevermind I got it... man I'm just not thinking at all today... must need more sleep... :)

I put "Dim Client As PB.Comm" in the global decs area, and put Client = New PB.Comm in the OnLoad event.
 
Back
Top