C#: calling methods with multiple objects as parameters from other classes

I am wondering whether it is possible to call methods with objects as parameters from other classes without using too many parameters.

In detail: how can you call method Info.Status() inside class Car?

Extending parameters to

MethodA(Location location1, Location location2, Garage garage1, Car car1)

would do the trick.

BUT whenever you use MethodA() somewhere else, you always have to deal with all parameters and objects, just because of the method Info.Status() in it, that needs the additional parameters.

Or is it a lack of structure of my code and you need other principles (out, Interface, ...) ?

And where you should create the objects location1, location2, garage1 and car1 ? In Main ?

public class Location { }
public class Garage {}

public class Car
{
    public void MethodA(Location location1, Location location2)
    {
        // method doing stuff with objects location1 and location2
        // now calling method Info.Status() to get some status information
    }
}

public static class Info
{
    public static void Status(Location location1, Location location2, Garage garage1, Car car1)
    {
        // method to print out some values and properties of objects location1, location2,
        // garage1 and car1
    }
}

1 answer

  • answered 2021-02-27 06:48 Mohammad Shaddad

    In short, you can't call Info.Status without passing it the parameters it requires.

    It is really difficult to answer your question without a proper explanation of your use case, there are many ways where you might do that, but there is no one answer. you can

    • have Status method as part of Car class, that might be suitable in some cases
    • pass Car as an input to Status and have Location1 and Location2 as properties of car that can later be accessed by Status

    It just depends on what you are building, how your classes related to each other, etc.