Jump to content

C# interclass communication

stefanmz
Go to solution Solved by Franck,
24 minutes ago, stefanmz said:

Sure yeah I don't know about reflection anyway so I guess I won't use it. The second option I might try but I mean it's an app just for fun and exercising classes so no big deal,thanks anyway. I think I am gonna try the second option.

second option is what I would recommend you to do. It will teach you Static and/or Singleton. It is also closer to what you would do in a real world application based on the current subject you choose for that exercise.

ok so I got a class restaurant and I got a class foods. everytime you add a food in the constructor for foods an instance of the class restaurant gets created so the food that you created under class foods can also be added to the list of foods which is in class restaurant. My question is how do I do if possible: I write a food with certain parameters in class foods and these parameters get recorded for this food in class restaurant but I call the food pizza(because I am adding pizza). Is there a way to get that name and record the food in the restaurant class but with the same name without putting the value into another variable first and then putting that variable into the restaurant. Can I directly use the food name I created for class foods in main?
 I mean I know I can do it with input by the user console read stuff but can I do it like this directly in code by referencing? like I write a bunch of foods and when I start the program they all automatically get recorded in the restaurant without me having to put them in manually and r.Foods is a list https://paste.mod.gg/ewexeyotan.cs
 
 
 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Sounds like you want to do something like this:

 

class Program
{

    public static void Main(string[] args)
    {
        Address address = new Address("Haskovo", "Bulgaria", "Kiril i Metodii 82");
        Person person = new Person("Gosho", 27, "Plovdiv,Bulgaria,bul.Ruski 85");
        Child child = new Child("Toshko", 8, "Plovdiv,Bulgaria,bul.Ruski 85");
        child.Play();
        //Foods pizza = new Foods("Margherita", 5, 500);
        Restaurant myRestaurant = new Restaurant
        {
            Foods = new List<Food>(),
            Drinks = new List<Drink>()
        };
        myRestaurant.Foods.Add(new Food("Margherita", 5, 500)); //Addes a new instance of type "Food" to the List of Food items in the Restaurant object we created above

    }
}
class Restaurant
{

    public List<Food> Foods { get; set; }

    public List<Drink> Drinks { get; set; }


}
class Food
{
    public string Name { get; set; }
    public double Price { get; set; }
    public double Weight { get; set; }
    public Food(string Name, double Price, double Weight)
    {
        this.Name = Name;
        this.Price = Price;
        this.Weight = Weight;
        Restaurant r = new Restaurant();
        Program program = new Program();
        r.Foods.Add(program.)
    }
}
class Drink
{
    public string Name { get; set; }
}

 

Instead of just creating a stand alone object of type "Food", you want to create a list of Food objects within your restaurant object. Then you can reference that list of food items from the restaurant object "myRestaurant" when you want to look one up or print out a list of Food items that exists within the restaurant object.

Link to comment
Share on other sites

Link to post
Share on other sites

39 minutes ago, stefanmz said:
 I mean I know I can do it with input by the user console read stuff but can I do it like this directly in code by referencing? like I write a bunch of foods and when I start the program they all automatically get recorded in the restaurant without me having to put them in manually and r.Foods is a list

If I'm understanding you correctly, you want to be able to create Food instances like this

Food pizza = new Food(…);

and whenever you do, that instance should (magically) be added to the Food list of a Restaurant instance?

 

You could probably achieve this with some creative use of reflection, but I'd really recommend against that. Also, what would be the expected result if you have two instances of Restaurant? Should the Food instances be added to both?

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, Eigenvektor said:

If I'm understanding you correctly, you want to be able to create Food instances like this



Food pizza = new Food(…);

and whenever you do, that instance should (magically) be added to the Food list of a Restaurant instance?

 

You could probably achieve this with some creative use of reflection, but I'd really recommend against that. Also, what would be the expected result if you have two instances of Restaurant? Should the Food instances be added to both?

Yes that is more what I want because in the upper example the food is manually added to the list and in your example what you're saying is correct I want to create the food instance and then that magically be added to the restaurant list,however I might try to do it with reflection as you told me but you said it's not recommended and I don't know about reflection yet so I will have to google that so maybe not I don't know,it's not practical anyway I was just curious if you could do it. Normally I would have a program(if we talk about practical cases) that has GUI and like combobox to choose your food or textbox if you want to add new food and then the input gets added to the restaurant list,still using classes like in my example. Thanks anyway I might learn about reflection just to try and do it out of pure curiosity even though it's not recommended(as you said at least).Also I haven't thought as far as two instances so I have no idea xD

 

Link to comment
Share on other sites

Link to post
Share on other sites

You should never do that. You end up with complex coupling and introduce a bunch of headache. You have 2 choices,

1 - Add the food directly to the restaurant and call it a day

2 - Create a static (or singleton) object that has the collection public and restaurant feeds of that class and you handle only 1.

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, Franck said:

You should never do that. You end up with complex coupling and introduce a bunch of headache. You have 2 choices,

1 - Add the food directly to the restaurant and call it a day

2 - Create a static (or singleton) object that has the collection public and restaurant feeds of that class and you handle only 1.

Sure yeah I don't know about reflection anyway so I guess I won't use it. The second option I might try but I mean it's an app just for fun and exercising classes so no big deal,thanks anyway. I think I am gonna try the second option.

Link to comment
Share on other sites

Link to post
Share on other sites

24 minutes ago, stefanmz said:

Sure yeah I don't know about reflection anyway so I guess I won't use it. The second option I might try but I mean it's an app just for fun and exercising classes so no big deal,thanks anyway. I think I am gonna try the second option.

second option is what I would recommend you to do. It will teach you Static and/or Singleton. It is also closer to what you would do in a real world application based on the current subject you choose for that exercise.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×