简体   繁体   中英

Can't call a method from another class

I try to call myCar.FormatMe() , but it doesn't show. I don't know why. Any suggestions?

using System;
namespace SimpleClasses
{
    class Program
    {
        static void Main(string[] args)
        {
            Car myCar = new Car();
            myCar.Make = "BMW";      
            myCar.FormatMe();
            Console.ReadLine();
        }
    }

    class Car
    {
        public string Make { get; set; }
        public string FormatMe()
        {
            return string.Format("Make: {0}", this.Make);
        }

    }
}

Thanks a lot.

There is no output in your code

class Program
{
    static void Main(string[] args)
    {
        Car myCar = new Car();
        myCar.Make = "BMW";      
        Console.WriteLine(myCar.FormatMe());
    }
}

class Car
{
    public string Make { get; set; }
    public string FormatMe()
    {
        return string.Format("Make: {0}", this.Make);
    }

}

Orphaned Strings and Reading Instead Of Writing

You are returning a string from your FormatMe() function but aren't actually doing anything with it :

myCar.FormatMe(); // This will return your value, but it isn't being stored

Additionally, you are calling the Console.ReadLine() method which actually expects input from the user as opposed to outputting it to the user.

Store Your Variable and Write It Out

Consider storing it in a variable or directly passing it as a parameter to the Console.WriteLine() method to be sent as output :

// This will store the results from your FormatMe() method in output
var output = myCar.FormatMe();
// This will write the returned string to the Console
Console.WriteLine(output);
// You can now read it here
Console.ReadLine();

or :

// Write the output from your FormatMe() method to the Console
Console.WriteLine(myCar.FormatMe());
// Now you should be able to read it
Console.ReadLine();

Example

You can see an interactive example of this in action here and its output demonstrated below :

在此处输入图片说明

Either wrap your function call in a call to Console.WriteLine() or have FormatMe() do the same thing.

What you are doing with your function call is simply returning a string, but not doing anything with it, like assigning it to a variable or passing it to another function as a parameter. Hence, it "does nothing," because you haven't done anything with it.

you should get the value from FormatMe() or simply print it:

static void Main(string[] args)
{
     Car myCar = new Car();
     myCar.Make = "BMW";           
     Console.ReadLine(myCar.FormatMe());
}

Sorry for little late; and i'm not sure about the acceptance of this kind of answers; Please forgive me if it is not the right way.

Let's have a look at the problem with the help of an example. That, you are asking for something( here it is a formatted string ) to a named person(here it is car ). and the question is give me a formatted sting( FormatMe() ). everything is fine up-to this.

Whats next? If everything is fine(means there is no issues in the function) to give you the formatted string, the person will give you the result. This too okay in your case(function returned the formatted string perfectly).

What you need to do at this time? you need to collect the product from the respective person.unfortunately you forgot to collect them, but you are trying to deliver it to another person. this is happening in your case.

So what to do? you need to collect the product, the formatted string before you delivering it to the console. that is:

string formattedString =myCar.FormatMe(); // collecting formatted string
Console.WriteLine(formattedString); // delivering it to the console

Or you can collect them on the way to the console like the following:

Console.WriteLine(myCar.FormatMe()); // delivering it to the console

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM