0

Share your experience, please, what do you do when you want to set the names of class and structure the same noun? Or am I doing something wrong in my logic?
Thank you!

enter image description here

2 Answers2

0

As far as I know it's impossible to give them the same name and it would be a bad practice if it were possible. Personaly, I don't understand really well the need of a struct but I don't have a lot of context to help you with.

If the struct is absolutely necessary I would simply declare it like this

struct CarStruct {
 ...
}

public class Car {
 ...
}

I don't see the need of a struct here if you declare a class for the same type of Object. You could simply declare the brand, YearIssue and MaxSpeed as attributes in your class but, like I said, if you could give more context on the matter, I could figure out if the logic itself makes sense !

Hope this helps !

ptparadis
  • 1
  • 1
  • Thank you, I also thought to do the same, but after reading the articles it seems that self-repetition in names is a bad practice: https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/names-of-classes-structs-and-interfaces – Eugenio Uglov Mar 31 '22 at 13:52
0

There is no need to move properties to another struct Car as it is tightly coupled to class Car and it has high cohesion

So, in my view, it would be better if you put code together in one class:

public  class Car
{
    public string Brand;
    public int YearIssue;
    public int MaxSpeed;

    public void Go() { }

    public void Stop() { }
}

If you are using C#, then you can read more about struct and class here.

UPDATE:

If you want to separate responsibilities between data model and how data should be represented to user, so it is better to call them like CarModel and CarViewModel. CarModel should be mapped to your stored data in some source and CarViewModel should be shown at the user interface. Read more here about model and viewModel

Community
  • 1
  • 1
StepUp
  • 36,391
  • 15
  • 88
  • 148
  • Thanks for the advice. However, I need to get exactly the type (structure) Car and the class that already performs operations. The point is to use the minimum set of necessary properties in the structure that determine how the variable will look. I have a "conditional" class Car which should not be an object for all kinds of cars. To create cars, I will use a structure, and pass a variable of the Car type to the function. – Eugenio Uglov Mar 31 '22 at 13:49
  • The fact is that I would like to separate the class from the structure, since the class has many other public fields and functions, and the structure is needed only to to read the properties of the vehicle. For example: Structure usage Car myCar = new Car(); myCar.Brand = "some brand"; ... Using functions Car carControl = new Car(); carControl.Go(myCar); – Eugenio Uglov Mar 31 '22 at 13:50
  • @EugenioUglov However, I do not think that it is good idea to create class and struct as they are very tightly coupled. You are separating data and behavior – StepUp Mar 31 '22 at 14:08
  • Yes, I agree with you. So I'm using the MVC pattern. And I have conditional CarController, CarModel and CarView. I have a Car structure stored in CarModel in order not to strongly separate behavior with data. I don’t know how much this corresponds to the rule of good practice – Eugenio Uglov Mar 31 '22 at 15:34
  • Are you using ASP.NET MVC? What is CarView and CarModel? – StepUp Mar 31 '22 at 16:28
  • No, I don't use ASP.NET. But I write names manually to separate responsibilities where Model is responsible for working with data, and view for displaying the interface – Eugenio Uglov Mar 31 '22 at 18:16
  • Do I understand correctly that models can also contain structures that can be used from anywhere? For example: CarModel.CarStruct car = ... – Eugenio Uglov Apr 01 '22 at 09:05
  • @EugenioUglov yeah, you can. E.g. see [code first approach in ASP.NET MVC](https://learn.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application#the-student-entity) – StepUp Apr 01 '22 at 09:13
  • Great, thank you! And leaving a little off topic, can a controller be used to call executable functions from somewhere else as well, or should controlelr just check for an event and redirect its processing somewhere else? For example: CarController { Go() {} Stop() {} } ResultsController() { carController.Go(); } – Eugenio Uglov Apr 01 '22 at 11:27
  • @EugenioUglov you can create a service and call it from controller. Then in your service you can do what you want. – StepUp Apr 01 '22 at 11:30
  • 1
    Yes, sure. You have answered the main question. And I wish clarify only the last detail. I see that you have a lot of experience. I want to know why this is called a service and what is the difference between a service, manager and system? I asked the same question here: https://stackoverflow.com/questions/71693338/what-is-the-difference-between-suffixes-service-manager-system?noredirect=1#comment126704670_71693338 – Eugenio Uglov Apr 01 '22 at 15:57