user1932402
user1932402

Reputation:

Do something with returned value

I have written a function that tells you its function itselfs as you read it:

Public Function TestConnect()
    Dim verbinding As MySqlConnection
    Dim errStatus As String

    verbinding = New MySqlConnection()
    verbinding.ConnectionString = "server=" & vardbHost & "; port=" & vardbPort & "; uid=" & vardbUser & "; pwd=" & vardbPass & "; database=" & vardbName & ";"

    Try
        verbinding.Open()
        verbinding.Close()
        errStatus = 0
    Catch myerror As MySqlException
        verbinding.Dispose()
        verbinding.Close()
        errStatus = 1
    End Try

    Return errStatus
End Function

Now I call this function in my main form and I thought that if I used Try and then catch the 1 or 0 then I could do something with it. (eg. Display a form with the error message) but that does not seem to work and I could not find anything on Google that applies to my problem.

Could anybody explain to me why I am so dumb and how I could better understand how to handle a returned value?

Upvotes: 0

Views: 100

Answers (3)

Teejay
Teejay

Reputation: 7469

Dim returnCode as Int32 = SQLHook.TestConnect()

MessageBox.show(
         If(returnCode = 1, "OK", "Error"), "AppName", MessageBoxButtons.OK,
         If(returnCode = 1, MessageBoxIcon.Information, MessageBoxIcon.Error)
       ) 

Upvotes: 0

Jon Egerton
Jon Egerton

Reputation: 41549

The function will be returning your value, but you need to get that assign that returned value to a variable, and then make use of it in your Calling method, eg:

Dim errStatus As Integer
errStatus = SQLHook.TestConnect()
If errStatus = 1 Then
    'Show the error form
End If

Or more briefly, just test the returned value directly:

If SQLHook.TestConnect()= 1 Then
    'Show the error form
End If

You should also really sort out the variable typing in your function:

Public Function TestConnect() as Boolean
    Dim errStatus As Boolean
    Try
        errStatus = True
    Catch myerror As MySqlException
        errStatus = False
    End Try

    Return errStatus  
End Function

or even more simply, don't bother with the variable:

Public Function TestConnect() as Boolean

    Try
        ...
        Return True
    Catch myerror As MySqlException
        ...
        Return False
    End Try
End Function

Upvotes: 2

chrisb
chrisb

Reputation: 2210

Not entirely sure I understand the question, but do you mean something like this?

Public Function TestConnect() As Int32
  ...
  Dim errStatus As Int32
  ...

Upvotes: 0

Related Questions