how can we do like this in runtime?

Thread Starter

Shafty

Joined Apr 25, 2023
358
private static PurchasingItem Sridhar;
private static PurchasingItem Inverter;
private static PurchasingItem ReadingTablet;
private static PurchasingItem VOIPphone;
private static PurchasingItem LandlinePhone;
private static PurchasingItem ChainSprocket;

how can we do like this in runtime? I mean naming the variable itself..
 

nsaspook

Joined Aug 27, 2009
16,473
private static PurchasingItem Sridhar;
private static PurchasingItem Inverter;
private static PurchasingItem ReadingTablet;
private static PurchasingItem VOIPphone;
private static PurchasingItem LandlinePhone;
private static PurchasingItem ChainSprocket;

how can we do like this in runtime? I mean naming the variable itself..
Why? What do you want to do?
 
You can't dynamically create static instances of types in .Net. You can create heap instances using the reflection API.

But your example is easily implemented using a generic dictionary of type <string,PurchasingItem> where you just add as many named instances to the dictionary as you need, at runtime.

dict.Add("Inverter",new PurchasingItem());

then you can access these as:

dict["Inverter"].whatever = something;
 
Top