Results 1 to 6 of 6

Thread: to declare a public param for public class form1

  1. #1

    Thread Starter
    Fanatic Member ksuwanto8ksd's Avatar
    Join Date
    Apr 2005
    Posts
    636

    to declare a public param for public class form1

    Code:
    Imports System
    Imports System.Threading
    Imports System.ComponentModel
    Imports System.IO.Ports
    Public Class Form1
        Dim SMSEngine As New SMSCOMMS("COM3")
        Dim i As Integer
    
        Private Sub ConnectButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConnectButton.Click
    
            If SMSPort.IsOpen Then
                SMSPort.Close()
            End If
    
            Try
                'WITH 
                'END WITH
                SMSPort.Open()
                ListBox1.Items.Add("com3" & " connected.")
                ConnectButton.Enabled = False
                'btnDisconnect.Enabled = True 
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
        End Sub
    
        Public Class SMSCOMMS
            Private WithEvents SMSPort As SerialPort
            Private SMSThread As Thread
            Private ReadThread As Thread
            Shared _Continue As Boolean = False
            Shared _ContSMS As Boolean = False
            Private _Wait As Boolean = False
            Shared _ReadPort As Boolean = False
            Public Event Sending(ByVal Done As Boolean)
            Public Event DataReceived(ByVal Message As String)
    
            Public Sub New(ByRef COMMPORT As String)
                'initialize all values
                SMSPort = New SerialPort ':wave:DECLARE THIS ONE AS PUBLIC
                With SMSPort
                    .PortName = COMMPORT
                    .BaudRate = 19200
                    .Parity = Parity.None
                    .DataBits = 8
                    .StopBits = StopBits.One
                    .Handshake = Handshake.RequestToSend
                    .DtrEnable = True
                    .RtsEnable = True
                    .NewLine = vbCrLf
                End With
            End Sub
    end class
    end class
    i am new to vb.net
    any guru can help on how to declare "smsport" on class smscomms as public so that it can be access in class form1 or button1_click event

    thanks in advance for any help

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: to declare a public param for public class form1

    You could just declare it Public instead of Private. The preferred method though is to use a public property, e.g.
    vb.net Code:
    1. Private myField As SomeType
    2.  
    3. Public Property MyProperty() As SomeType
    4.     Get
    5.         Return myField
    6.     End Get
    7.     Set(ByVal value As SomeType)
    8.         myField = value
    9.     End Set
    10. End Property
    You can just type 'prop' and then hit Tab twice to insert that as a code snippet, which you can then edit as required.

  3. #3

    Thread Starter
    Fanatic Member ksuwanto8ksd's Avatar
    Join Date
    Apr 2005
    Posts
    636

    Re: to declare a public param for public class form1

    hi thanks for replay,
    eventhoguh public, i still cant access "smsport" from ConnectButton_Click

    Code:
    Imports System
    Imports System.Threading
    Imports System.ComponentModel
    Imports System.IO.Ports
    Public Class Form1
        Dim SMSEngine As New SMSCOMMS("COM3")
        Dim i As Integer
    
        Private Sub ConnectButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConnectButton.Click
    
            If SMSPort.IsOpen Then
                SMSPort.Close()
            End If
    
            Try
                'WITH 
                'END WITH
                SMSPort.Open()
                ListBox1.Items.Add("com3" & " connected.")
                ConnectButton.Enabled = False
                'btnDisconnect.Enabled = True 
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
        End Sub
    
    
        Public Class SMSCOMMS
            Public WithEvents SMSPort As SerialPort
            Private SMSThread As Thread
            Private ReadThread As Thread
            Shared _Continue As Boolean = False
            Shared _ContSMS As Boolean = False
            Private _Wait As Boolean = False
            Shared _ReadPort As Boolean = False
            Public Event Sending(ByVal Done As Boolean)
            Public Event DataReceived(ByVal Message As String)
    
            Public Sub New(ByRef COMMPORT As String)
                'initialize all values
                SMSPort = New SerialPort
                With SMSPort
                    .PortName = COMMPORT
                    .BaudRate = 19200
                    .Parity = Parity.None
                    .DataBits = 8
                    .StopBits = StopBits.One
                    .Handshake = Handshake.RequestToSend
                    .DtrEnable = True
                    .RtsEnable = True
                    .NewLine = vbCrLf
                End With
            End Sub
    
         End Class
    
    End Class

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: to declare a public param for public class form1

    I don't see anywhere that you're actually trying to access it. SMSPort is the public field and it's a member of the SMSCOMMS class. SMSEngine is your SMSCOMMS variable but nowhere are you actually using that variable.

  5. #5
    Addicted Member
    Join Date
    Mar 2007
    Posts
    163

    Re: to declare a public param for public class form1

    Should it be instead:

    vb Code:
    1. If SMSEngine.SMSPort.IsOpen Then
    2.             SMSEngine.SMSPort.Close()
    3. End If
    4. ' etc

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

    Re: to declare a public param for public class form1

    Yes it should.

    SMSPort is a member of an instance of the class. The instance is called SMSEngine, and you always have to refer to the instance you want. This should make sense if you think about it a bit. After all, you could easily have two different instances of the SMSCOMMS class, in which case you would have to state which SMSPort you were refering to, so it makes sense that you have to refer to the instance even if you only have one instance.
    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
  •  



Click Here to Expand Forum to Full Width