Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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:

    ' --------------------------------------------------------------------------------------
   ' 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?

If it works... don't worry, I'll fix it.
Posted

Hi - my suggestion would be:

 

Add a property to your class code and change the modifiers:

'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.

#Region "GLOBAL DECLARATIONS"
 'your declarations here.....
#End Region

 

Cheers :)

Howzit??
Posted

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

 

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.

 

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.

If it works... don't worry, I'll fix it.
Posted

You don't need the 'objClient' object in the load event, if you already hold the 'Client' object on class level, e.g.

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:

'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

Howzit??
Posted
Great! I think that worked... so I just need to use the Client.CurrentClient each time basically to access "TcpClient" object?
If it works... don't worry, I'll fix it.
Posted
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?
If it works... don't worry, I'll fix it.
Posted

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..

 

Howzit??
Posted

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.

If it works... don't worry, I'll fix it.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...