Groups | Blog | Home
all groups > vb.net controls > august 2004 >

vb.net controls : 2 newbie questions


Hari
8/13/2004 2:55:24 PM

From: "Hari" <harixseshadri@yahoo.com>
Subject: 2 extreme newbie ?s
Date: Friday, August 13, 2004 2:43 PM

I just started programming in Visual Basic.NET about a month ago, so I am
completely oblivious as to which libraries to look in. However, I have good
experience with Java and C. Below are ten extremely simple questions which I
couldn't seem to find the answers to after looking in some text and
searching through the libraries:

I looked many places for the library that parses Strings into different
number formats, but couldn't find one.
Ex:
Dim str As String = "8.93"
Dim num As Double
num = ???
If I want num to be set to 8.93, which method of the String class should I
use?


When I implement the Try... Catch... Finally set, I can't quite get the
clause to function the way I want it to. Take the following piece of code:
____ 'Start
Dim slots(3) As Integer
Try
slots(4) = 3
Catch Except As IndexOutOfBounds 'or something
'catch code here
Catch Except As Exception
'other catch code here
___ 'End
if I want to have a certain exception handled one way and any other
exception all handled in one general way, what do I need to do. If I type in
the above code, I always get the the general code executed, even if the
IndexOutOfBounds exception was thrown.



Scott M.
8/13/2004 5:16:00 PM
In .NET there are several ways to "cast" data from one type to another. The
most common way is to use the CType(original, new) functon so:

Dim str As String = "8.93"
Dim num As Double
num = CType(str, Double)




[quoted text, click to view]

Scott M.
8/13/2004 5:25:10 PM
Everything in .NET is one type or another and most types are objects
fundamentally, even exceptions. Exceptions come in types like the most
generic exception of all the "exception" exception. All exceptions are of
this type, but there are more specific exception types as well
(InvalidCastException, OverflowException, etc.).

When you want to handle multiple possible exceptions, just add another Catch
section to the Try. Start with the most specific exceptions and work your
way to the most generic exception.

Try
something
Catch ex as InvalidCastException
handle exception
Catch ex as OverflowException
handle exception
Catch ex as Exception
handle exception
Finally
do something
Ent Try


The reason your code below is not working is not because you've structured
the Try...Catch statement incorrectly. The reason is that there is no
exception of the type "IndexOutOfBounds". That may be the exception's
message, but not its type. Change "IndexOutOfBounds" to
"IndexOutOfRangeException".

One trick that I always do when building Try...Catch statements is to first
start with nothing but the generic exception handler and in its code I put:
response.write(ex.getType.toString))

Then I run my app over and over tyring to break it each time. When it does
break, I get a message telling me the exact type of exception that was
thrown. After I've broken the app in all the ways I could think of, I go
back to the Try...Catch and build Catch statements for all the types of
exceptions that were reported to me during my "destructive" phase.



[quoted text, click to view]

AddThis Social Bookmark Button