PC Review


Reply
Thread Tools Rate Thread

2 newbie questions

 
 
Hari
Guest
Posts: n/a
 
      13th Aug 2004

From: "Hari" <(E-Mail Removed)>
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.




 
Reply With Quote
 
 
 
 
=?Utf-8?B?a2FybHd3?=
Guest
Posts: n/a
 
      13th Aug 2004

"Hari" wrote:

>
> From: "Hari" <(E-Mail Removed)>
> 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 = ???


num = CDbl(str)

> 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


Are you sure? I tried the following code which works as expected


Dim sl(3) As Integer

Try
sl(4) = 1
Catch ex As System.IndexOutOfRangeException
MsgBox("System.IndexOutOfRangeException " & ex.Message)
Catch ex As System.Exception
MsgBox("System.Exception " & ex.Message)
End Try

> 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.
>

 
Reply With Quote
 
Imran Koradia
Guest
Posts: n/a
 
      13th Aug 2004
> > Dim str As String = "8.93"
> > Dim num As Double
> > num = ???

>
> num = CDbl(str)


or you could also use Double.Parse(str)



 
Reply With Quote
 
Hari
Guest
Posts: n/a
 
      13th Aug 2004
Thanks karlww and Imran,
I was just trying it out again and I realized the following piece of code
worked:
Dim str As String = "8.93"
Console.WriteLine(str + 0.07)
which means that there is an automatic class cast in the background. I
didn't even realize this so, sorry for the hassel.
"karlww" <(E-Mail Removed)> wrote in message
news:780377BF-3720-4F1E-83E2-(E-Mail Removed)...
>
> "Hari" wrote:
>
> >
> > From: "Hari" <(E-Mail Removed)>
> > 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 = ???

>
> num = CDbl(str)
>
> > 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

>
> Are you sure? I tried the following code which works as expected
>
>
> Dim sl(3) As Integer
>
> Try
> sl(4) = 1
> Catch ex As System.IndexOutOfRangeException
> MsgBox("System.IndexOutOfRangeException " & ex.Message)
> Catch ex As System.Exception
> MsgBox("System.Exception " & ex.Message)
> End Try
>
> > 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.
> >



 
Reply With Quote
 
=?Utf-8?B?a2FybHd3?=
Guest
Posts: n/a
 
      13th Aug 2004
Hey Hari,
The automatic cast is generally frowned upon. Consider for example;

Dim str As String = "8.93"
Dim str2 As String = "0.07"

Console.WriteLine(str + str2) 'what do you expect as the output; 9 or
"8.930.07"?

 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      15th Aug 2004
Hari,
Include Option Strict On at the top of each of your source files. It will
disable these automatic class conversions, which IMHO can cause brittle code
(code that breaks easily at run time, usually with obscure errors or no
errors).

In other words Option Strict On will cause compile time errors instead of
runtime errors for most conversions...

The default is Option Strict Off, which allows this automatic conversions.

Hope this helps
Jay

"Hari" <(E-Mail Removed)> wrote in message
news:79ednRNaA8E1uIDcRVn-(E-Mail Removed)...
> Thanks karlww and Imran,
> I was just trying it out again and I realized the following piece of code
> worked:
> Dim str As String = "8.93"
> Console.WriteLine(str + 0.07)
> which means that there is an automatic class cast in the background. I
> didn't even realize this so, sorry for the hassel.
> "karlww" <(E-Mail Removed)> wrote in message
> news:780377BF-3720-4F1E-83E2-(E-Mail Removed)...
> >
> > "Hari" wrote:
> >
> > >
> > > From: "Hari" <(E-Mail Removed)>
> > > 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 = ???

> >
> > num = CDbl(str)
> >
> > > 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

> >
> > Are you sure? I tried the following code which works as expected
> >
> >
> > Dim sl(3) As Integer
> >
> > Try
> > sl(4) = 1
> > Catch ex As System.IndexOutOfRangeException
> > MsgBox("System.IndexOutOfRangeException " & ex.Message)
> > Catch ex As System.Exception
> > MsgBox("System.Exception " & ex.Message)
> > End Try
> >
> > > 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.
> > >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
newbie with newbie questions JohnE Microsoft ASP .NET 3 17th Aug 2009 11:10 PM
Newbie questions Steve Griffin Microsoft Access Database Table Design 7 12th Jan 2005 10:54 PM
Newbie Questions??? Gustavo Venturo Rivera Microsoft ASP .NET 1 20th Aug 2004 03:53 PM
Newbie DWT Questions auerbach Microsoft Frontpage 15 18th Jul 2004 08:37 AM
Newbie Questions on ADO.NET in VB.NET(basically ADO.NET questions) SStory Microsoft ADO .NET 3 14th Nov 2003 03:32 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:01 PM.