Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6603327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:59:40+00:00 2026-05-25T18:59:40+00:00

Just have a quick question… any help would be greatly appreciate! I’m writing a

  • 0

Just have a quick question… any help would be greatly appreciate!

I’m writing a database. I have a class called “Mechanism” which is inherited by two other classes called “motorcycle” and “automobile.” How would i go about printing the contents of motorcycle or automobile – depending on what the user has decided to enter into the database?

this is what i have so far, it’s giving me this error: InvalidCastException was unhandled. Unable to cast object of type “ConsoleApplication1.Automobile” to type “ConsoleApplication1.Motorcycle”

foreach (Mechanism m in mechanisms)
{
    //ptr = m;
    if (flagAuto == true)
    {
        Mechanism ptr = null;
        ptr = m;
        Console.WriteLine("ptr = " + ptr);
        Console.WriteLine("ptr2 = " + ptr2); 
        ptr2 = (Automobile)ptr;
        Console.WriteLine("inside Auto");
        ofp.WriteLine("" + (ptr2.getManufacturer()));
        ofp.WriteLine("" + ptr2.getModel());
        ofp.WriteLine("" + ptr2.getModelYear());
        ofp.WriteLine("" + ptr2.getVIN());
        ofp.WriteLine("" + ptr2.getInitialPurchasePrice());
        ofp.WriteLine("" + ptr2.getPurchaseDate());
        ofp.WriteLine("" + ptr2.getSizeOfEngine());
        ofp.WriteLine("" + ptr2.getTypeOfFuel());
        ofp.WriteLine("" + ptr2.getNumberOfDoors());
        ptr2 = null;
        ptr = null; 
        Console.WriteLine("finishinge Auto");
    }
    else if (flagMotor == true)
    {
        Mechanism ptr = null;
        ptr = m;
        Console.WriteLine("ptr = " + ptr);
        Console.WriteLine("ptr2 = " + ptr1); 
        Console.WriteLine("inside Motor");
        ptr1 = (Motorcycle)ptr;
        ofp.WriteLine("" + ptr1.getManufacturer());
        ofp.WriteLine("" + ptr1.getModel());
        ofp.WriteLine("" + ptr1.getModelYear());
        ofp.WriteLine("" + ptr1.getVIN());
        ofp.WriteLine("" + ptr1.getInitialPurchasePrice());
        ofp.WriteLine("" + ptr1.getPurchaseDate());
        ofp.WriteLine("" + ptr1.getSizeOfEngine());
        ofp.WriteLine("" + ptr1.getTypeOfMotorcycle());
        ptr1 = null;
        ptr = null; 
        Console.WriteLine("finishing Motor");
    }

the flags are supposed to keep track of which type of vehicle is trying to be entered into my database – then it should write it out to a text file..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:59:41+00:00Added an answer on May 25, 2026 at 6:59 pm

    That means the value of the flag is wrong.

    You should do this instead:

    if (m is Automobile)
    {
        Automobile autoMobile = m as Automobile;
        //.. work with autoMobile
    }
    else if (m is Motorcycle)
    {
        Motorcycle motorCycle = m as Motorcycle;
        //.. work with motorCycle 
    }
    

    By the way, you’re doing the same thing in the both branch. That implies, that you could implement them in a base class (Mechanism), call those methods on m object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey just a quick question for any experts out there. I have a site
just a quick question. I have a function called multiple times with a 5
I just have a quick question, im wondering if someone could help me. I
I just have a quick question about how to get the length of a
Just a quick question, I have a figure which contain an image and a
Just a quick question here. I have a program in which I need to
Just a quick question. I don't use sitefinity and I don't currently have access
Just a (hopefully) quick question, I have the following HTML code: <tr> <td><img src=img/icons/file_pdf.png></td>
This is just a quick question in C#. I have a scenario where I
Just a quick newbie question here. I have a method that calculates a value

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.