Visual Basic 2010 - Array Size checking for a game -- Should I use TRY/CATCH or Should I use GetLength(0) + Delimiters?

Pretty simple question. I have a Client to Server application. Client sends a new application with user/password/etc, Server receives it seperated with a delimiter.

So let us say the delimiter is |

DataArray = Split(Data, "|")

Should I now call this:

If DataArray.GetLength(0) = 5 Then
...Do stuff
End if

Or should I do this

End Try

.. I want to prevent errors. I know of both but TRY/CATCH I haven't really used before, and I'm wondering if its practical here and won't be 'slow'. Application needs to be as fast as possible. Mainly to prevent someone from packet manipulation, sending one less variable to try to crash the server for example!

Also I'm wondering, what 'delimiter' can I use that someone cannot submit with a standard keyboard? Any ideas?

Thank you.
Who is Participating?
Paul MacDonaldConnect With a Mentor Director, Information SystemsCommented:
In the first example, you're only checking the array size.  Any other data errors go unhandled.

In the second example, any error that breaks the TRY can be ostensibly caught and dealt with.  There shouldn't be a performance hit for using TRY...CATCH
ZhaolaiConnect With a Mentor Commented:
I think you need to use both, like this:

    If DataArray.Length = 5 Then
         'do stuff
    End If

End Try

>what 'delimiter' can I use that someone cannot submit with a standard keyboard?
You can use a combination of symbols, like "~|`", which would make it hard to manipulate.

Hope it helps.
Carl TawnConnect With a Mentor Systems and Integration DeveloperCommented:
Exception handlers are for just that, handling "Exceptions", i.e. unexpected scenarios or things that can go wrong that you have no control over. You know you're expecting 5 elements, so you can check for that (as in your first sample) without using an exception handler.

The "Do Stuff" might benefit from and exception handler; depends what it's doing really.
omegaomegaConnect With a Mentor DeveloperCommented:
Hello, Valleriani,

Regarding testing vs exception handling, I concur with carl_tawn.  

Regarding a delimiter, you might want to consider using the ASCII NUL character.  I've included an example in the attached snippet.


Dim strTest As String = "ABC" & Chr(0) & "EFG"
        MsgBox("strTest = >>>" & strTest & "<<<")

        Dim straTest As String() = strTest.Split(Chr(0))
        MsgBox("straTest = >>>" & straTest(0) & "<<< and >>>" & straTest(1) & "<<<")

Open in new window

All Courses

From novice to tech pro — start learning today.