'how to use a class to stock data? [closed]
I'm new to c#, and I was wondering what is the best way to stock data in a class and access it from a different class.
for example:
public class Class1{
public static int nb;
}
public class Class2{
public int nb;
}
public class Class3{
public int nb;
public static Class3 current = this;
}
public class ClassTest{
int myNb = 5;
Class1.nb = myNb;
var instClass2 = new Class2();
instClass2.nb = myNb;
Class3.current.nb = myNb;
}
if someone can enlighten me on what's the best solution for those questions :
-what's the "correct" way to stock data with class (maybe I'm using class in a wrong way)
-if a class is supposed to carry lots of variable, like several arrays of string, or int ect
thank you very much for your concern and your time! :D
Solution 1:[1]
You need to be more concrete. A class is meant to represent an object that your program has to deal with. Lets suppose you want an app that deals with cars. What info do you need? Manufacturer, Model, Year
So have
public class Car
{
public string Manufacturer {get;set;}
public string Model {get;set;}
public int Year {get;set;}
}
now you have all the data for a car in one place.
There is no 'hidden' data here everything is plain to see. All other code can access the data. But here is an example of a class with hidden internals.
public class CarDealer{
private List<Car> Inventory;
public List<Car> GetByModel(string model){
}
public GetPrice(Car car){
}
}
here the dealer object has an inventory of cars, but we cant get at it directly we have to call
dealer.GetByModel("Escort")
to get a list of 'Escort' models that they have. And if we want to know the price we have to call
dealer.GetPrice(carWeLike);
The point is that later in our program we might change how the prices is calculated, the rest of the program wont care, because it will still call GetPrice. Same way , we might change how the inventory is stored for the Dealer, it could be a database, or on a file or whatever. Bu the rest of the program doesnt care, its just calls GetByModel. This is called 'encapsulation'
EDIT
lets add some cars to the dealer
public class CarDealer{
private List<Car> Inventory;
public void AddCar(Car car){
Inventory.Add(car);
}
public List<Car> GetByModel(string model){
}
public GetPrice(Car car){
}
}
now
CarDealer dealer = new Dealer();
Car ford1 = new Car{Manufacturer = "Ford", Model = "Escort", Year=2020};
Car ford2 = new Car{Manufacturer = "Ford", Model = "Cortina", Year=1975};
dealer.AddCar(ford1);
dealer.AddCar(ford2);
Solution 2:[2]
Normally to populate a class you'd pass the data to the class constructor like:
public class Class2{
public int Nb;
public Class(int nb){
Nb = nb;
}
}
var myClass2 = new Class2(5);
or you can:
public class Class2{
public int Nb;
}
var myClass2 = new Class2() {
Nb = 5
};
It really depends a lot what you're trying to do.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Pete Michaud |
