Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / October 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
TheVillageCodingIdiot - 08 Oct 2007 17:53 GMT
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?
TheVillageCodingIdiot - 08 Oct 2007 18:18 GMT
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
Jack Jackson - 08 Oct 2007 18:23 GMT
>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
[quoted text clipped - 69 lines]
>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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.