-
Nov 22nd, 2014, 07:56 PM
#1
Thread Starter
New Member
How to create a object from a class?
The City-State from Form1’s GUI Sets the Destination Property in the Object.
b. The Form1 GUI then Gets these three Properties values from the object: ArrivalTime, DepartureTime, and Gate.
c. The TicketPrice Method calculates TicketPrice by adding 15% (Tax) to the Flight Price. That Method is used by Form1’s GUI, but the calculations are done within the TicketPrice Method within the Object.
The code below is something wrong and giving me errors for "objFlightInformation" it said "declaration expected" what is this mean?
Code:
Dim objFlightInformation as New clsFlightInformation
objFlightInformation.Destination=comboboxDestination.selecteditem
objFlightInformaiton.ArrivalTime=cstr(lblArrivalTime.text)
objFlightInformation.DepartureTime=cstr(lblDepartureTime.text)
objFlgihtInformation.Gate=cstr(lblGate.text)
-
Nov 22nd, 2014, 08:07 PM
#2
Re: How to create a object from a class?
I don't think creating a record to accomplish this task is the best way to do it. You're better of creating a structure. E.g
Structure FlightInformation
Dim Destination As String
Dim ArrivalTime As String
Dim DepartureTime As String
Dim Gate As String
End Structure
Dim FlightInfo As FlightInformation
FlightInfo.Destination = "VB Forums"
FlightInfo.ArrivalTime = "Foo"
like that
using a class is unnecessary imho.
-
Nov 22nd, 2014, 08:13 PM
#3
Thread Starter
New Member
Re: How to create a object from a class?
But I want to create by using the class, will it do it?
-
Nov 22nd, 2014, 08:18 PM
#4
Re: How to create a object from a class?
If anything is unnecessary it's the use of cstr... the text being passed into it already is a string, so there's no point in converting it into a string. It's like exchanging a quarter for a quarter.
Class vs structure ...may or may not be a matter of preference, given that this sounds a lot like a class assignment, I'm guessing it's showing how to use classes and properties and such.\
Speaking to the error - depends on the line where the error actually happens. At first I was thinking that the Dim line was the issue, but I don't think it really is. I bet it's the line below it, where you use the .SelectedItem .... which is going to try and use the OBJECT that is associated with the selection. It's not the text... it's the underlying OBJECT, Try using the combobox's .Text property instead.
-tg
-
Nov 22nd, 2014, 08:26 PM
#5
Re: How to create a object from a class?
Does that error go away if you put those statements in a method body like shown below?
Code:
Sub foo
Dim objFlightInformation as New clsFlightInformation
objFlightInformation.Destination=comboboxDestination.selecteditem
objFlightInformaiton.ArrivalTime=cstr(lblArrivalTime.text)
objFlightInformation.DepartureTime=cstr(lblDepartureTime.text)
objFlgihtInformation.Gate=cstr(lblGate.text
End Sub
-
Nov 22nd, 2014, 08:37 PM
#6
Thread Starter
New Member
Re: How to create a object from a class?
OK the error go away but there is another error the property "departure, arrival and gate" is readonly. What can i do?
Last edited by lisak1026; Nov 22nd, 2014 at 08:51 PM.
-
Nov 22nd, 2014, 10:57 PM
#7
Thread Starter
New Member
Re: How to create a object from a class?
I'm using combobox, the time, and number doesn't show in the label. Please help i'm very appreciate. Thank you!
[CODE] Private Sub cboDestination_SelectedItemChanged(sender As System.Object, e As System.EventArgs) Handles cboDestination.SelectedIndexChanged
Dim objFlightInformation As New clsFlightInformation
objFlightInformation.Destination = cboDestination.SelectedItem
objFlightInformation.DepartureTime = lblDepartTime.Text
objFlightInformation.Destination = lblArrivalTime.Text
objFlightInformation.Destination = lblGate.Text
lblTicketPrice.Text = FormatCurrency(objFlightInformation.TicketPrice)
Private mstrDestination As String
Private mstrDepartureTime As String
Private mstrArrivalTime As String
Private mstrGate As String
Private Const mdecWashinton_DC_Price As Decimal = 300D
Private Const mdecVancouver_WA_Price As Decimal = 100D
Private Const mdecChicago_IL_Price As Decimal = 200D
Private Const mdecTax_Rate As Decimal = 0.15D [\CODE]
Last edited by lisak1026; Dec 4th, 2014 at 02:57 PM.
-
Nov 29th, 2014, 02:09 PM
#8
New Member
Re: How to create a object from a class?
Originally Posted by lisak1026
I'm using combobox, the time, and number doesn't show in the label. Please help i'm very appreciate. Thank you!
Code:
Private Sub cboDestination_SelectedItemChanged(sender As System.Object, e As System.EventArgs) Handles cboDestination.SelectedIndexChanged
Dim objFlightInformation As New clsFlightInformation
objFlightInformation.Destination = cboDestination.SelectedItem
objFlightInformation.DepartureTime = lblDepartTime.Text
objFlightInformation.Destination = lblArrivalTime.Text
objFlightInformation.Destination = lblGate.Text
lblTicketPrice.Text = FormatCurrency(objFlightInformation.TicketPrice)
I'm working on the same assignment and have the same issue. It's showing me an error which says "Property 'DepartureTime is 'ReadOnly'. If I switch to the code below than the error disappears, however, it doesn't display the ticket price in the label. My class code is similar to what lisak1026 has.
Code:
Private Sub cboDestination_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboDestination.SelectedIndexChanged
Dim objFlightInformation As New clsFlightInformation
objFlightInformation.Destination = CStr(cboDestination.SelectedItem)
objFlightInformation.Destination = lblDisplayDepartingSacramento.Text
objFlightInformation.Destination = lblDisplayArrivingDestination.Text
objFlightInformation.Destination = lblDisplayGate.Text
lblDisplayTicketPrice.Text = FormatCurrency(objFlightInformation.TicketPrice)
End Sub
-
Nov 29th, 2014, 03:34 PM
#9
New Member
Re: How to create a object from a class?
Nevermind I got it to work. I just had to switch the order of the label and the object.
-
Nov 30th, 2014, 01:40 AM
#10
Thread Starter
New Member
Re: How to create a object from a class?
Ron963 so did you got it to work on showing the total price? I still have problem on solving this issue.
-
Nov 30th, 2014, 01:17 PM
#11
New Member
Re: How to create a object from a class?
just reverse the order of the lbl and obj because you're writing obj to the lbl and not the other way around.
Code:
objFlightInformation.Destination = CStr(cboDestination.SelectedItem)
lblDisplayDepartingSacramento.Text = objFlightInformation.DepartureTime
lblDisplayArrivingDestination.Text = objFlightInformation.ArrivalTime
lblDisplayGate.Text = objFlightInformation.Gate
lblDisplayTicketPrice.Text = objFlightInformation.TicketPrice
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|