|
-
Mar 12th, 2003, 04:55 PM
#1
Thread Starter
Hyperactive Member
I had to take a VB.NET test today
I had to take a VB.NET test today for an employer. This question was included.
If you run the code below what will the value of label1.Text
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Double
Dim s As String
Try
s = "i = "
i = 1 / 0
s = s & "infinity"
Catch
s = s & "undefined"
Finally
label1.Text = s
End Try
End Sub
My answer was "i = infinity" but was told I was wrong. Can someone explain how I am wrong? They told me the correct answer is "i = undefined". I think they caught themselves in their own trick question.
-
Mar 12th, 2003, 05:09 PM
#2
Member
Because when you try to divide 1 by 0, an exception is thrown. In your catch block, you append on 'undefined' to the string. Then, when the finally block executes, the label's caption is updated. Hey, I'm from cincinnati also.
-
Mar 12th, 2003, 05:16 PM
#3
Sleep mode
Originally posted by SimonVega
Because when you try to divide 1 by 0, an exception is thrown. In your catch block, you append on 'undefined' to the string. Then, when the finally block executes, the label's caption is updated. Hey, I'm from cincinnati also.
No acception is thrown at all ! I tried this code
VB Code:
Dim i As Double
Dim s As String
s = "i = "
i = 1 / 0
s = s & "infinity"
Label1.Text = s
So you fooled by Catch structure .
-
Mar 12th, 2003, 05:16 PM
#4
Sleep mode
exception not acception
-
Mar 12th, 2003, 05:17 PM
#5
Member
Hmm....I didn't try the code, im not at my dev machine, just eye ballin' it, it seems that would be the cause....Oh well, can't always be right!
-
Mar 12th, 2003, 05:23 PM
#6
Thread Starter
Hyperactive Member
I think the person that wrote the code expected the 1/0 to throw an error but it doesn't. .NET defines a divide by zero as infinity.
Try this code
Code:
label1.Text = 1 / 0
-
Mar 12th, 2003, 05:25 PM
#7
You should get a divid by zero error.
That is wierd though, because you don't. Hmmm.
Ahh its because they have or the post has the division slash backwards. So it means something else and not division and thus no error.
You should black their eye for marking you off.
I think they meant this:
VB Code:
Dim i As Integer = 1
Dim s As String
Try
s = "i = "
i = i \ 0
s = s & "infinity"
Catch
s = s & "undefined"
Finally
Me.Text = s
End Try
-
Mar 12th, 2003, 05:27 PM
#8
Thread Starter
Hyperactive Member
It will give you a compile error if you do integer division 1\0 but will evaluate 1/0 as infinity.
-
Mar 12th, 2003, 05:27 PM
#9
Member
When I try the same example in C#, the compiler will not even let you run the application.
-
Mar 12th, 2003, 05:28 PM
#10
I guess in VB the / returns a floating point and thus the #INF but the \ returns an integer and gives the error.
-
Mar 12th, 2003, 05:33 PM
#11
Thread Starter
Hyperactive Member
Well, a compile error was not an option so they expected it to throw an error.
Based on this (and several other issues), I told them I was no longer interested in the posistion.
-
Mar 12th, 2003, 05:34 PM
#12
Member
What was the name of the company? What part of cincinnati you from?
-
Mar 12th, 2003, 05:41 PM
#13
this might be helpful
when you divide by zero .NET wont throw an exception error. I think it's only like this for Double. There is Double.isNaN and Double.IsInfinity
Nan is Not A Number, I dont remember when that happens.
Now if you try this with that code it will return true : Double.IsInfinity(i)
I think they made it like this so that your program wouldnt return an exception for these values. Makes more sense..... there was an explanation of it in my book...
edit: and the reason that it doesnt work with "\" is that "\" converts the value to INTEGER. But the Integer data type doesnt accept the values of Infinity or Nan
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Mar 12th, 2003, 06:02 PM
#14
Thread Starter
Hyperactive Member
SimonVega, check your prvate messages
-
Mar 12th, 2003, 07:46 PM
#15
Frenzied Member
This is a long shot, but it may throw an exception if option explicit or option strict is on.
Dont gain the world and lose your soul
-
Mar 12th, 2003, 07:49 PM
#16
Thread Starter
Hyperactive Member
DevGrp, turning option strict only makes you have to convert the infinity to a string
label1.Text = CStr(1 / 0)
-
Mar 12th, 2003, 08:47 PM
#17
Hyperactive Member
Re: I had to take a VB.NET test today
Originally posted by VBGuy
I had to take a VB.NET test today for an employer. This question was included.
If you run the code below what will the value of label1.Text
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Double
Dim s As String
Try
s = "i = "
i = 1 / 0
s = s & "infinity"
Catch
s = s & "undefined"
Finally
label1.Text = s
End Try
End Sub
My answer was "i = infinity" but was told I was wrong. Can someone explain how I am wrong? They told me the correct answer is "i = undefined". I think they caught themselves in their own trick question.
I just ran your code, and I get "i = infinity". Their completely wrong!!!!
The double data type allows for NaN's and infinities.
-
Mar 12th, 2003, 10:58 PM
#18
Re: Re: I had to take a VB.NET test today
Originally posted by Hu Flung Dung
I just ran your code, and I get "i = infinity". Their completely wrong!!!!
The double data type allows for NaN's and infinities.
that's what I said
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Mar 13th, 2003, 01:20 AM
#19
Junior Member
You were perfectly correct!!
Go back & tell them that they are not good enough for you to work for them!!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|