How would I create Dynamic Class Items? C# WinForms
I want to dynamically add items to a class, but someone may have to help me with my syntax here.
I've got a ContainerTank
class like this:
class ContainerTank {
double radius;
public ContainerTank(int diameter) {
radius = (double)diameter / 2;
}
public int Gauge {get;set;}
public double Volume { get { return (3.1415)*(radius*radius)*Gauge; } }
}
Each of our Sites can have anything from none to an absurd value of 10 tanks (water tanks, 开发者_如何转开发oil tanks, etc.).
I can easily create a Class that using List<ContainerTank> tankList
to maintain the data in each tank per site, but I'd rather find a way to initialize my class by specifying how many tanks are on a site, then have each tank show up individually.
For example, if I create a new site with 2 tanks, I would have a map to Tank1 and Tank2 (or Tank0 and Tank1 if you want to use zero index).
Why?
For our developers using the code, seeing Tank1 and Tank2 pop up with Visual Studio's IntelliSense would help them when using our control rather than presenting them with a collection.
Mainly, I want to know if it can be done and how I'd implement it.
No, this isn't possible. All class members must be specified when the assembly is compiled.
If what you're interested in is purely ease of debugging, it's possible to write your own debug visualizer.
精彩评论