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




!!
Reply With Quote
