Results 1 to 5 of 5

Thread: VS2019 Throw new gives excpetion

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2016
    Posts
    25

    VS2019 Throw new gives excpetion

    I'm following the book Visual Basic 2012 How to Program
    I've created a very simple class :

    Code:
    Public Class Form1
        Dim First As Employee = New Employee
        Dim Second As Employee = New Employee
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            First.FirstName = "John"
            First.LastName = "Henderson"
            First.MonthlySalary = 0
    
            MessageBox.Show(First.MonthlySalary * 12 & " Year income")
            MessageBox.Show((First.MonthlySalary * 12) * 1.1 & " Year income with 10 % raise")
    
            Second.firstname = "Billy"
            Second.lastname = "The Kid"
            Second.MonthlySalary = 1500
    
            MessageBox.Show(Second.MonthlySalary * 12 & " Year income")
            MessageBox.Show((Second.MonthlySalary * 12) * 1.1 & " Year income with 10 % raise")
        End Sub
    End Class
    Code:
    Public Class Employee
        Private _FirstName As String
        Private _LastName As String
        Private _MonthlySalary As Integer
    
        Public Sub New()
            _FirstName = "test"
            _LastName = "test"
            _MonthlySalary = 1
        End Sub
    
        Public Property FirstName As String
            Get
                Return _FirstName
            End Get
            Set(value As String)
                _FirstName = value
            End Set
        End Property
    
        Public Property LastName As String
            Get
                Return _LastName
            End Get
            Set(value As String)
                _LastName = value
            End Set
        End Property
    
        Public Property MonthlySalary As Integer
            Get
                Return _MonthlySalary
            End Get
            Set(value As Integer)
                If value <= 0 Then
                    Throw New ArgumentOutOfRangeException("positive") '<_-------this one
                End If
                _MonthlySalary = value
            End Set
        End Property
    End Class
    If I delete the line with the comment <-- this one I get "0 year income", as expected.
    But with the line I should get "positive" But I get this error message seen below :
    Name:  IMG_1916.jpg
Views: 243
Size:  34.8 KB

    Here is the exercise question :
    9.7 (Employee Class) Create a class called Employee that includes three pieces of information as
    instance variables—a first name (type String), a last name (type String) and a monthly salary (type
    Integer). Your class should have a constructor that initializes the three instance variables. Provide
    a property for each instance variable. The property for the monthly salary should ensure that its value remains positive—if an attempt is made to assign a negative value, throw an exception. Write an
    app that demonstrates class Employee’s capabilities. Create two Employee objects and display each
    object’s yearly salary. Then give each Employee a 10% raise and display each Employee’s yearly salary
    again.

    Is it a bug in Visual Studio don't think so. But my book explains it so it should work!
    Shed some light on this one you guys !

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: VS2019 Throw new gives excpetion

    This is a perfect example of why you should ALWAYS read the documentation for new types and members, ESPECIALLY when they don't work as expected. You could have got the same information by simply paying attention to Intellisense as you typed but the documentation is more thorough. There's really no excuse for not doing so, given that you can simply click a type or member in VS and press F1 to be taken directly to the relevant documentation.

    If you had done that then you would know that the constructor with a single String parameter expects the name of the parameter whose value was out of range. You have no parameter named positive in that context so how can that be an appropriate argument to pass to that constructor? If you want to pass an actual error message to the constructor then you have to use one of the overloads that takes an error message as an argument. I'm not going to point out which those are because you can find out for yourself by reading the documentation.

    What I will say is that, in this case, you ought to use the overload that also allows you to pass in the actual value and you should also provide a sensible error message. "positive" doesn't qualify. In line with the sort of messages usually received in such cases, it should be something like "The value must be greater than 0." or "The value must be greater than or equal to 0.". Note that the assignment is bad because it says that the salary must be positive and must not be negative but it doesn't mention whether zero is allowed or not. This is an example of sloppy thinking on the part of whoever wrote the assignment and something you should avoid as a developer. If you fail to consider all the possible scenarios then, when one you haven't considered arises, your app will either crash or, even worse, carry on in an unknown state that could lead to data corruption or incorrect output.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Aug 2016
    Posts
    25

    Re: VS2019 Throw new gives excpetion

    Lets consider one thing a read the documentation about throw but there was a total lack of information on the Microsoft site.
    I've tried everything just a simple Throw New ArgumentOutOfRangeException() isnt working !

    Or
    Throw New ArgumentOutOfRangeException("value", "message")
    Last edited by lamko; Apr 11th, 2021 at 09:26 AM.

  4. #4
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,605

    Re: VS2019 Throw new gives excpetion

    I don't usually bash people like JMC does (no offense intended there JMC) but it looks like you refuse to read the documentation as he said in the previous post.
    If you read this then you would know that you need to capture the exception
    https://docs.microsoft.com/en-us/dot...n?view=net-5.0

    Code:
      Dim c As New Employee
            Try
                c.MonthlySalary = -1
            Catch ex As ArgumentOutOfRangeException
    
            End Try
    Having said that, it's not a good idea to throw exceptions when they are not absolutely necessary as they tend to slow performance.
    You can easily handle this by doing and IF .MonthlySalary < 0 before you set the value.
    Last edited by sapator; Apr 11th, 2021 at 04:42 PM.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Aug 2016
    Posts
    25

    Re: VS2019 Throw new gives excpetion

    I was in the assumption that a Throw New was enough. But I had to look better, my mistake.
    Thx guys

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