Results 1 to 19 of 19

Thread: I had to take a VB.NET test today

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2002
    Location
    The Big D
    Posts
    310

    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.

  2. #2
    Member
    Join Date
    Mar 2003
    Posts
    34
    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.
    AKA 'Lethal'

  3. #3
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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:
    1. Dim i As Double
    2. Dim s As String
    3.  
    4. s = "i = "
    5.  i = 1 / 0
    6. s = s & "infinity"
    7. Label1.Text = s
    So you fooled by Catch structure .

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    exception not acception

  5. #5
    Member
    Join Date
    Mar 2003
    Posts
    34
    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!
    AKA 'Lethal'

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2002
    Location
    The Big D
    Posts
    310
    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

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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:
    1. Dim i As Integer = 1
    2.         Dim s As String
    3.         Try
    4.             s = "i = "
    5.             i = i \ 0
    6.             s = s & "infinity"
    7.         Catch
    8.             s = s & "undefined"
    9.         Finally
    10.             Me.Text = s
    11.         End Try

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2002
    Location
    The Big D
    Posts
    310
    It will give you a compile error if you do integer division 1\0 but will evaluate 1/0 as infinity.

  9. #9
    Member
    Join Date
    Mar 2003
    Posts
    34
    When I try the same example in C#, the compiler will not even let you run the application.
    AKA 'Lethal'

  10. #10
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I guess in VB the / returns a floating point and thus the #INF but the \ returns an integer and gives the error.

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2002
    Location
    The Big D
    Posts
    310
    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.

  12. #12
    Member
    Join Date
    Mar 2003
    Posts
    34
    What was the name of the company? What part of cincinnati you from?
    AKA 'Lethal'

  13. #13
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    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!!

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2002
    Location
    The Big D
    Posts
    310
    SimonVega, check your prvate messages

  15. #15
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    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

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2002
    Location
    The Big D
    Posts
    310
    DevGrp, turning option strict only makes you have to convert the infinity to a string


    label1.Text = CStr(1 / 0)

  17. #17
    Hyperactive Member
    Join Date
    Feb 2002
    Posts
    261

    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.

  18. #18
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

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

  19. #19
    Junior Member
    Join Date
    Mar 2003
    Posts
    16

    Thumbs up

    You were perfectly correct!!

    Go back & tell them that they are not good enough for you to work for them!!
    vijay pahuja
    mumbai

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width