[RESOLVED] [2005] Syntax Problem Calling Function from Subroutine
Could someone take a look at this section of code and tell me how to fix it?
It's giving me this eror code on build:
Method 'Public Function UpdateTextBox() As Object' does not have the same signature as delegate 'Delegate Sub myDelegate()'.
I change "UpdateTextBox" from a sub to a function so it could pass arguements to function "sendData". That's when the syntax error appeared. I've been looking all over the place for the answer but I have only been coding this stuff for a few weeks so maybe I didn't search for the correct keywords.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
With SerialPort1
Some Code Here
End With
SerialPort1.Open()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Public Delegate Sub myDelegate()
Private Sub OnDataReceived(ByVal sender As Object, _
ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
Handles SerialPort1.DataReceived
TextBox1.Invoke(New myDelegate(AddressOf UpdateTextBox), New Object() {})
End Sub
Public Function UpdateTextBox()
Some Code Here
sendData(varOne, varTwo)
End Function
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
End Sub
Error Code:
Method 'Public Function UpdateTextBox() As Object' does not have the same signature as delegate 'Delegate Sub myDelegate()'.
Thank you,
Ike
Re: [2005] Syntax Problem Calling Function from Subroutine
If you just want to pass in parameters, you do not need a function. A Function returns a value. If you do not return a value, then you don't need it to be a function. If you do need a function, then you have to specify a return type in the definition. If you want to pass in parameters to a sub or function, then you have to define those parameters in the sub or function declaration. Regardless of the two, your delegate sub or function has to have the exact same signature....
Re: [2005] Syntax Problem Calling Function from Subroutine
Here's an example of what you are trying to do, just pulled it from a test prog of mine when messing with the serial port. Shows how to invoke a delegate, which just runs the updateTextBox() sub to update the contents when the data is received.
VB Code:
'txtDataReceived is just a textbox
Private Sub SerialPort1_DataReceived(...) Handles SerialPort1.DataReceived
Console.WriteLine("data received")
Me.Invoke(New myDelegate(AddressOf updateTextBox), New Object() {})
End Sub
Public Delegate Sub myDelegate()
Public Sub updateTextBox()
txtDataReceived.AppendText(SerialPort1.ReadExisting)
txtDataReceived.ScrollToCaret()
End Sub
If you change updateTextBox to accept parameters, then you have to change the delegate sub to be the exact same signature as it. Shouldn't be too hard being that the delegate does not actually need any code inside of it.
Re: [RESOLVED] [2005] Syntax Problem Calling Function from Subroutine
The text box was a very bad idea so I ripped it out. Didn't need it anyway.
I used a form and drop a serial port object on it because I couln't figure out how to get a serial port object into a console application:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
With SerialPort1
.PortName = "Com1"
.BaudRate = 300
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = 1
.ReadBufferSize = 14 'inbound data stream + 1
.ReadTimeout = 100
.ReceivedBytesThreshold = 13 'the length of your inbound data stream
.Handshake = IO.Ports.Handshake.None
.RtsEnable = True
.DtrEnable = True
'.Encoding = System.Text.Encoding.ASCII (when I leave this out the inbound hex stream turns into ascii)
End With
SerialPort1.Open()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Public Delegate Sub myDelegate()
Private Sub OnDataReceived(ByVal sender As Object, _
ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
Handles SerialPort1.DataReceived
grabSerialData()
End Sub
Public Sub grabSerialData()
' declare variables
Dim comportData
Dim intBytesAvailable As Integer = SerialPort1.BytesToRead
If intBytesAvailable > 0 Then
comportData &= SerialPort1.ReadExisting() 'this line of code grabs the incoming data event
End If
...
...
...
I hope this thread helps someone out in the future.
Regards,
Ike