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
518 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 33

Accepted Solution

by:
paulmacd 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
crm development 2 37
Change data in datatable 8 18
Copy/Clone an object. 9 17
XML & .net 5 21
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Artificial Intelligence comes in many forms, and for game developers, Path-Finding is an important ability for making an NPC (Non-Playable Character) maneuver through terrain.  A* is a particularly easy way to approach it.  I’ll start with the algor…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now