Alright so essentially I need to create a class that houses the methods and then call the methods in the form but I can't seem to find out what the right code is to call it. I think there may be an issue with the conversion from a double to a string.

This is what I have for the Class

Public Class Aircraft
    Dim Names() As String = {"A-747", "A-737", "C-150", "D-240"}
    Dim TakeOffVelocity() As Double = {250, 264, 270, 240}
    Dim Acceleration() As Double = {33.5, 44.2, 37.1, 51.9}

    Sub CalculateTakeOff1()
        Dim time1 As Double = TakeOffVelocity(0)
        Dim accel1 As Double = Acceleration(0)
        Dim TakeOffTime1 As Double = time1 / accel1

        Dim TakeOffDistance1 As Double = accel1 * time1


        Dim takeOffA747 As String = ("The A-747 Aircraft has a required takeoff velocity of " + time1 +
                                     " ft/second and an acceleration constant of " + accel1 +
                                     ". Therefore, it requires " + TakeOffTime1 +
                                     " seconds to take off, with a distance of " + TakeOffDistance1 + " feet.")
        MsgBox("The A-747 Aircraft has a required takeoff velocity of " + time1 +
                                     " ft/second and an acceleration constant of " + accel1 +
                                     ". Therefore, it requires " + TakeOffTime1 +
                                     " seconds to take off, with a distance of " + TakeOffDistance1 + " feet.")
    End Sub
    Sub CalculateTakeOff2()
        Dim time2 As Double = TakeOffVelocity(1)
        Dim accel2 As Double = Acceleration(1)
        Dim TakeOffTime2 As Double = time2 / accel2

        Dim TakeOffDistance2 As Double = accel2 * time2


        Dim takeOffA737 As String = ("The A-737 Aircraft has a required takeoff velocity of " + time2 +
                                             " ft/second and an acceleration constant of " + accel2 +
                                             ". Therefore, it requires " + TakeOffTime2 +
                                             " seconds to take off, with a distance of " + TakeOffDistance2 + " feet.")
        takeOffA737.ToString()
    End Sub
    Sub CalculateTakeOff3()
        Dim time3 As Double = TakeOffVelocity(2)
        Dim accel3 As Double = Acceleration(2)

        Dim TakeOffTime3 As Double = time3 / accel3

        Dim TakeOffDistance3 As Double = accel3 * time3

        Dim takeOffC150 As String = ("The C-150 Aircraft has a required takeoff velocity of " + time3 +
                                     " ft/second and an acceleration constant of " + accel3 +
                                     ". Therefore, it requires " + TakeOffTime3 +
                                     " seconds to take off, with a distance of " + TakeOffDistance3 + " feet.")
        takeOffC150.ToString()
    End Sub
    Sub CalculateTakeOff4()


        Dim time4 As Double = TakeOffVelocity(3)
        Dim accel4 As Double = Acceleration(3)



        Dim TakeOffTime4 As Double = time4 / accel4



        Dim TakeOffDistance4 As Double = accel4 * time4


        Dim takeOffD240 As String = ("The D=240 Aircraft has a required takeoff velocity of " + time4 +
                                     " ft/second and an acceleration constant of " + accel4 +
                                     ". Therefore, it requires " + TakeOffTime4 +
                                     " seconds to take off, with a distance of " + TakeOffDistance4 + " feet.")
        takeOffD240.ToString()

    End Sub

End Class

This is the code for the form

Public Class DePlane

    Private Sub txtDisplay_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtDisplay.TextChanged

    End Sub

    Private Sub btnGo_Click(sender As System.Object, e As System.EventArgs) Handles btnGo.Click
        If cboxA747.Checked = True Then
            Dim plane1 As Aircraft
            plane1 = New Aircraft
            plane1.CalculateTakeOff1()
        ElseIf cboxA737.Checked = True Then
            Dim plane2 As Aircraft
            plane2 = New Aircraft
            plane2.CalculateTakeOff2()
        ElseIf cboxC150.Checked = True Then
            Dim plane3 As Aircraft
            plane3 = New Aircraft
            plane3.CalculateTakeOff3()
        ElseIf cboxD240.Checked = True Then
            Dim plane4 As Aircraft
            plane4 = New Aircraft
            plane4.CalculateTakeOff4()
        End If
    End Sub
End Class

Recommended Answers

All 9 Replies

>   Aircraft Calculations.exe!WindowsApplication1.Aircraft.CalculateTakeOff1() Line 14  Basic
    Aircraft Calculations.exe!WindowsApplication1.DePlane.btnGo_Click(Object sender = {Text = "GO!"}, System.EventArgs e = {X = 34 Y = 11 Button = MouseButtons.Left}) Line 11 + 0xa bytes  Basic
    [External Code] 

These are the error codes I am getting.

Use "&" instead of "+" when concatenating your string and numeric values. Also, inside your Class, you should declare properties as Public or Private instead of using Dim. And declare Subs and Functions as Public or Private as well.

Hi Dave,
I suggest that you either type "Option Strict On" as the first line in your code or enable as the default state under "Options->Projects and Solutions->VB Defaults". This will force VS to flag improper type conversions. It may seem like a pain at first, but you will find that fixing the errors it finds will save you many headaches down the road.

You also have an error in your distance calculation. For reference, see Click Here
V(t) = at + c1; where V(t) = velocity at time t, a = acceleration, t = time, and c1=constant.
Subjecting this equation to the boundary condition of V=0 at t=0, leads to c1=0
V(t) = a
t
The position (or distance) formula is:
p(t) = (at^2)/2 + c1t + c2
but c1 = 0 and at t=0, p(0) = 0, therefore c2 = 0
p(t) = (a*t^2)/2

I have restructured your code to show you a more conventional way of doing what your code is doing. You do not need a method for each airplane; just use the arrays you defined and do an array lookup on the name.

Public Class Aircraft
   'as this data is common and constant, declare it as shared.
   Private Shared Names() As String = {"A-747", "A-737", "C-150", "D-240"}
   Private Shared TakeOffVelocity() As Double = {250, 264, 270, 240}
   Private Shared Acceleration() As Double = {33.5, 44.2, 37.1, 51.9}

   'there is nothing unique about the calculation either. so make it a shared method as well
   'this way you do not have to create an instance of the class to use it
   'Example usage:
   '  Aircraft.CalculateTakeOff("A-737")
   '
   Public Shared Sub CalculateTakeOff(ByVal PlaneName As String)
      PlaneName = PlaneName.Trim 'eliminate any extra spaces
      'lookup the PlaneName
      Dim index As Int32 = Array.IndexOf(Names, PlaneName)
      If index = -1 Then 'not found
         MsgBox("I donot have data for " & PlaneName)
         Exit Sub
      End If

      Dim TimeToReachTakeoffVelocity As Double = TakeOffVelocity(index) / Acceleration(index)
      'the "#" after the 2 in the following line indicates a constant of type double 
      Dim TakeOffDistance As Double = Acceleration(index) * System.Math.Pow(TimeToReachTakeoffVelocity, 2.0#) / 2.0#

      MsgBox("The " & PlaneName & " Aircraft has a required takeoff velocity of " & TakeOffVelocity(index).ToString("0") & _
             " ft/second and an acceleration constant of " & Acceleration(index).ToString("0.0 ft/s^2") & _
             ". Therefore, it requires " & TimeToReachTakeoffVelocity.ToString("0.0") & _
             " seconds to take off, with a distance of " & TakeOffDistance.ToString("0.00") & " feet.")
   End Sub

End Class

Hello all, I am sorry to add to a post that is a month old, but I really need some help. I am fairly new to Visual Basic and currently taking classes in college to learn the language. This problem is what my homework is suppose to be, now I don't want an answer to the problem, belive me I don't. What I would like is for someone to help me figure out how to place my Names array in the class (same one as this "Dim Names() As String = {"A-747", "A-737", "C-150", "D-240"}") into a combobox. That is all I would like help in, I have look around and read my book many times but for some reason I can't grasp the way to do it.

If anyone can help me I would appreciate it very much

The answer is "it depends". Do you want to be able to access Names from outside (Public) or just from inside the class (Private)?

how to place my Names array in the class (same one as this "Dim Names() As String = {"A-747", "A-737", "C-150", "D-240"}") into a combobox

Or are you having trouble populatinig the combobox?

I have to have it set to private, so I would guess that meas from inside the class. And to answer your second question, yes. The requirements are for me to set up a seperate class and populate the combobox with the information from that class.

I will post what my instructions are here:

Define your custom class in a separate file.
Create an instance of your custom class for each value in the three arrays.
Read the values from the arrays into each custom class.
Implement the calculations in two properties. Make sure not to use shared properties.
Override the ToString method in your class to display the name of the aircraft in the ListBox.

Thanks for the reply, and thanks for the help ahead of time.

Based on my interpretaion of your assignment instructions, you need to create a class that defines an aircraft (let's call this class: Aircraft). This class would have three properties that define the aircraft: 1)Name, 2) TakeOfffVelocity, and 3) Acceleration. You also need to define a property two calculated properties: 1) TimeToReachTakeOffVelocity and 2) TakeOffDistance.

In this class you also need to override the ToString Function such that it returns only the Name Property.

Now, I'm just guessing, but in your form you will need to define an array or list of Aircraft (suggest you use a list if possible). You need to also include the array definitions that you previously had for: 1) Names, 2) TakeOffVelocity, and 3) TakeOffVelocity. You are to iterate through these arrays to fill the list.

You then need to set a combox's "DataSource" property equal to your list. This is where the overriden "ToString" comes into play. It provides the combobox a way of displaying the List (of Aircraft) in a meaningful way.

So hopefully without writing the for you, I have given a word statement that you can follow.

Thanks TnTinMN, I appreciate the word statment and not writing it out for me. I don't want you to do my work and in turn me take the credit for it, I don't wanna be that guy. But with your instructions I can continue on with my project and post here if I ever need any more help grasping what I need to do on this project or any future projects.

Also sorry Reverend Jim, I should have posted the full instructions. I will do so in any future responces.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.