Results 1 to 11 of 11

Thread: How to create a object from a class?

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2014
    Posts
    14

    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)

  2. #2
    Fanatic Member Toph's Avatar
    Join Date
    Oct 2014
    Posts
    655

    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.

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2014
    Posts
    14

    Re: How to create a object from a class?

    But I want to create by using the class, will it do it?

  4. #4
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    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

  6. #6

    Thread Starter
    New Member
    Join Date
    Oct 2014
    Posts
    14

    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.

  7. #7

    Thread Starter
    New Member
    Join Date
    Oct 2014
    Posts
    14

    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.

  8. #8
    New Member
    Join Date
    Nov 2014
    Posts
    5

    Re: How to create a object from a class?

    Quote Originally Posted by lisak1026 View Post
    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

  9. #9
    New Member
    Join Date
    Nov 2014
    Posts
    5

    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.

  10. #10

    Thread Starter
    New Member
    Join Date
    Oct 2014
    Posts
    14

    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.

  11. #11
    New Member
    Join Date
    Nov 2014
    Posts
    5

    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
  •  



Click Here to Expand Forum to Full Width