Solved

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

Posted on 2010-11-17
4
522 Views
Last Modified: 2012-05-10
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

Try
..DoStuff
Catch
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.
0
Comment
Question by:Valleriani
4 Comments
 
LVL 34

Accepted Solution

by:
Paul MacDonald earned 125 total points
ID: 34159333
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
0
 
LVL 17

Assisted Solution

by:Zhaolai
Zhaolai earned 50 total points
ID: 34159785
I think you need to use both, like this:

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

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.
0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 175 total points
ID: 34159999
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.
0
 
LVL 12

Assisted Solution

by:omegaomega
omegaomega earned 150 total points
ID: 34161678
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.

Cheers,
Randy

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

0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question