trying to pass a string back to main form from a class

  • Thread starter TheVillageCodingIdiot
  • Start date
T

TheVillageCodingIdiot

I'm creating a GUI for the NetSend service. I created a class called
Netsend. In there are to functions. The send function is public and
the NetMessageBufferSend function is private. Below is the code I
created for this class.

Public Class Netsend

Public Function Send(ByVal sTo As String, ByVal sMessage As
String) As Integer
Dim iReturnValue As Integer
Dim sReturn As String
Dim bBuffer As Byte() =
Encoding.Unicode.GetBytes(sMessage)

If sTo.Length And sMessage.Length <> 0 Then

iReturnValue = NetMessageBufferSend(Nothing, sTo,
Nothing, sMessage, sMessage.Length * 2 + 2)

Select Case iReturnValue
Case 0
sReturn = "Successfully sent message to " &
sTo
Case 2100
sReturn = ""
Case 2273
sReturn = "The message alias could not be
found on network for " & sTo
Case 2136
sReturn = "General network error occurred for
" & sTo
Case 5
sReturn = "Error: Access denied for " & sTo
Case 87
sReturn = "Invalid parameter for " & sTo
Case 50
sReturn = "This is not a supported command on
targert " & sTo
Case Else
sReturn = "Unknown return value for " & sTo
End Select

Return sReturn

Else
Return "Error: Either no computer was selected or
message was blank."
End If

End Function

<DllImportAttribute("Netapi32", CharSet:=CharSet.Unicode)> _
Private Shared Function NetMessageBufferSend(ByVal servername
As String, ByVal msgname As String, ByVal fromname As String, ByVal
buf As String, ByVal buflen As Integer) As Integer

End Function


So my form creates a new object for this class and calls the Send
function and passes the correct arguments, Computer name and message.

Then "send function" then validates and calls the Private function
NetMessageBufferSend and assigns the return to var iReturnValue.

the "Send" function runs through a case statment to set the value of
sReturn.

Then that value of sReturn is returned to the main form.


When it tries to return the value back to a string var on the form, i
get a invalid cast error saying that it is expecting a integer. I am
able to return a integer to the main form, but why cant I return a
string?
 
T

TheVillageCodingIdiot

nevermind i figured it out. I had my return type set as a integer and
not a string, im going to blame it on a monday =P
 
J

Jack Jackson

I'm creating a GUI for the NetSend service. I created a class called
Netsend. In there are to functions. The send function is public and
the NetMessageBufferSend function is private. Below is the code I
created for this class.

Public Class Netsend

Public Function Send(ByVal sTo As String, ByVal sMessage As
String) As Integer
Dim iReturnValue As Integer
Dim sReturn As String
Dim bBuffer As Byte() =
Encoding.Unicode.GetBytes(sMessage)

If sTo.Length And sMessage.Length <> 0 Then

iReturnValue = NetMessageBufferSend(Nothing, sTo,
Nothing, sMessage, sMessage.Length * 2 + 2)

Select Case iReturnValue
Case 0
sReturn = "Successfully sent message to " &
sTo
Case 2100
sReturn = ""
Case 2273
sReturn = "The message alias could not be
found on network for " & sTo
Case 2136
sReturn = "General network error occurred for
" & sTo
Case 5
sReturn = "Error: Access denied for " & sTo
Case 87
sReturn = "Invalid parameter for " & sTo
Case 50
sReturn = "This is not a supported command on
targert " & sTo
Case Else
sReturn = "Unknown return value for " & sTo
End Select

Return sReturn

Else
Return "Error: Either no computer was selected or
message was blank."
End If

End Function

<DllImportAttribute("Netapi32", CharSet:=CharSet.Unicode)> _
Private Shared Function NetMessageBufferSend(ByVal servername
As String, ByVal msgname As String, ByVal fromname As String, ByVal
buf As String, ByVal buflen As Integer) As Integer

End Function


So my form creates a new object for this class and calls the Send
function and passes the correct arguments, Computer name and message.

Then "send function" then validates and calls the Private function
NetMessageBufferSend and assigns the return to var iReturnValue.

the "Send" function runs through a case statment to set the value of
sReturn.

Then that value of sReturn is returned to the main form.


When it tries to return the value back to a string var on the form, i
get a invalid cast error saying that it is expecting a integer. I am
able to return a integer to the main form, but why cant I return a
string?

You aren't returning a string from Send(), you are returning an
Integer:
Public Function Send(ByVal sTo As String, ByVal sMessage As
String) As Integer

You must have Strict Off, otherwise you would have gotten errors on
the Return statements in Send(). With Strict On you would have
discovered this error during compilation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top