Results 1 to 4 of 4

Thread: InvalidOperationException error was unhandled..

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2014
    Posts
    11

    InvalidOperationException error was unhandled..

    Hi all!

    Struggling as hell with my first windows form programme. The task was to create a calculator, and the part i'm at is for fuel consumption (a further part is a calorie counter..)

    While it seemed more easy for me to create all code in the main class, the task is specifically to create a separate class for each part of the calculator, while using the main form class only for the GUI part. I couldn't really understand how to call a class within another class and how to use the functions / variables from the fuelCalc in the MainForm part. Eventually I have tried to adapt a sample programme that our teacher provided us, but since i can't really understand all the lines, it is perhaps not surprising that I can't figure it out for my own code.
    At the moment I get an "InvalidOperationException was unhandled" error. Tried to fix it in different ways but i really can't get around the proper way to call the class /use the object from the fuelCalc class.

    The code probably has many flaws, but a good start would be to figure out how to initialize/ call the functions and vars from one class into the Mainform.

    Oh and on a side note, I understood that using Public is a bad programming practice, but if I need to use the objects from a class into another one shouldn't everything be public? or does inherit mean that i could still use private?

    Thanks a lot for taking your time to help a poor beginner !!

    this is the code for fuelcalc class:
    Code:
    Option Strict On
    Option Explicit On
    
    Public Class fuelCalc
    
        Private reading0 As Double ' previous km reading, user input, has to be positive or zero
        Private reading1 As Double 'current km reading, user input, has to be higher than reading0
        Private volume As Double ' amount of fuel, user input
        Private price As Double 'user input
        Private dist As Double = reading1 - reading0
        Private litPerKm As Double = volume / dist
    
    
        Public Function ConsLitKm() As Double
            Dim kmPerLit As Double = dist / volume
            Return kmPerLit
        End Function
        Public Function ConsKmLit() As Double
            Dim litPerKm = volume / dist
            Return litPerKm
        End Function
    
        Public Function ConsLitMile() As Double
    
            Const kmToMile As Double = 0.621
            Dim litPerMile As Double = litPerKm / kmToMile
            Return litPerMile
        End Function
    
        Public Function ConsLitSMile() As Double
            Dim litPerSMil As Double = litPerKm * 10
            Return litPerSMil
        End Function
    
        Public Function Tcost() As Double
            Dim cost As Double = litPerKm * price
            Return cost
        End Function
    
    
    End Class

    and the code for the main form:
    Code:
    Option Strict On
    Option Explicit On
    ''' This class is solely responsible for all user interactions.
    ''' Everything that has to to with GUI is placed here.
    ''' BUT it uses other objects to do other jobs. In this example
    ''' project, it uses an object of the class fuelCalc
    Partial Public Class MainForm
        Inherits Form
        'instance variable 
        ' variables are read from the textboxes 
    
        Private reading0 As Double = Val(TxtReading0.Text)
        Private reading1 As Double = Val(txtReading1.Text)
        Private volume As Double = Val(TxtVol.Text)
        Private price As Double = Val(TxtPrice.Text)
        Private dist As Double = reading1 - reading0
        Private carMilage As fuelCalc
    
    
        Public Sub New()
    
            ' This call is required by the designer. 'VSs initialization
            InitializeComponent()
    
            ' Add any initialization after the InitializeComponent() call.
            'My initializations
            InitializeGUI()
    
        End Sub
    
        Private Sub InitializeGUI()
            'initialization
            carMilage = New fuelCalc()
    
        End Sub
        Private Function valid() As Boolean ' tests whether user input is a valid number and whether 
            Dim ok As Boolean
            If (dist > 0 And reading0 >= 0 And Double.TryParse(TxtReading0.Text, reading0) And Double.TryParse(txtReading1.Text, reading1) And Double.TryParse(TxtVol.Text, volume) And Double.TryParse(TxtPrice.Text, price)) Then
                carMilage.ConsKmLit()
                carMilage.ConsLitKm()
                carMilage.ConsLitMile()
                carMilage.ConsLitSMile()
                carMilage.Tcost()
            Else : MsgBox("Please write a valid number")
            End If
            Return ok
        End Function
    
        Private Sub updateGUI() 'prints the output to the read only textboxes
            txtConsKmLit.Text = carMilage.ConsKmLit().ToString()
            txtConsLitKm.Text = carMilage.ConsLitKm().ToString()
            txtConsLitMile.Text = carMilage.ConsLitMile().ToString()
            txtConsLitSMile.Text = carMilage.ConsLitSMile().ToString()
            txtCost.Text = carMilage.Tcost().ToString()
    
        End Sub
    
    
        Private Sub btnFuel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFuel.Click
            
            Dim ok As Boolean = valid() 'if user input is a valid number, then display output when clicked on calculate
            If ok Then
                updateGUI()
            Else : MessageBox.Show("invalid input")
            End If
    
        End Sub
    
    End Class

  2. #2
    Junior Member
    Join Date
    Nov 2014
    Posts
    16

    Re: InvalidOperationException error was unhandled..

    So mutch Textboxes...
    at first please make more Notes what u want to do oO....
    second one, upload ur whole Project, ill test it tomorrow. I tryed to reconstruct it, but its not as easy as u think to put all those different Textboxes in a Form and do a normal calculation....

  3. #3
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: InvalidOperationException error was unhandled..

    Okay, lots of things to address here, I'll start with this one:

    Quote Originally Posted by anac View Post
    Oh and on a side note, I understood that using Public is a bad programming practice, but if I need to use the objects from a class into another one shouldn't everything be public? or does inherit mean that i could still use private?
    I assume that you're referring to the practice of making the fields of a class Public. As you've figured, Public means that a member of a class is visible from outside the class. It would be a strange class if nothing was public (although not totally unheard of, but you're not going to hit those scenarios for a while).

    The "bad programming practice" part comes in when you make the internals of the class visible outside it, clearly you need to make the interface parts Public. This is the principle of information hiding and lets you change how the class works internally without affecting all the consumers of that class (and is one of the key ways that OO methods manage complexity).

    So, in the fuelCalc class, you have several fields marked as Private that hold the instance's state. That's good, those should be Private. You've also got some methods marked as Public that are designed to be used by the consumer classes to retrieve the calculated values. That's good, those should be Public.

    I think you've just got a bit hung up on having anything Public at all, which is not what the "avoid making things Public" thing is about. What you're doing with regards to that is fine

  4. #4

    Thread Starter
    New Member
    Join Date
    Oct 2014
    Posts
    11

    Re: InvalidOperationException error was unhandled..

    Yes, i know it's a lot of them but this is the task:/...I added more notes and attached the entire project file, but hopefully things will get more clear when you¨'ll see the form.


    Thanks a lot again, highly appreciated!!
    Attached Files Attached Files

Tags for this Thread

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