C# Newbie, class properties

Hi there,

I have a class with properties as follows:

public class ItemClass {
	
	public string 		itemName;
	public int 			itemId;
	public string 		itemType;	
	
}

Instances of this class are contained within a list.

Depending on the item type, I wish to add different properties to the class. For example;

public class ItemClassGeneric {
	
	public string 		itemName;
	public int 			itemId;
	public string 		itemType;
(Something here that adds additional properties depending on the itemType)	
	
}

//Additional properties if itemType is fruit
public string taste;
public string health;
public string etc;

//Additional properties if itemType is chair
public int hardness;
public string material;
public bool safety;

Is it possible to do this, or would I / should I move to nested classes or nested lists?

Any suggestions would be most welcome.

Thanks.
Pli

You extend classes from the base class to add properties. These can be put in the same list, but when you check item type, you cast them to the higher class to access the other properties.

of base class.

note: what you have posted are member variables of the class not properties. just that you avoid confusion when googling for this stuff

public class fruit : ItemClassGeneric // this adds all the member variables + methods of the base class
{
//add additional members here
public string taste; // this is a member variable or field
public string Taste  // this is a property
{
 get{return taste;}
}
}

Excellent, thanks for the information fire, and the corrections exi. It’s certainly easier to find these answers when using the correct terms. I think I’m set now :wink:

Thanks,
Pli

I wanna use this for client data
I have two variable string and float
Can ı use this for communication between to class.

You need to be more specific on what you want to do. Also it does not seam to relate to original problem of this thread. Probably best if you started a new one.