- Home /
C# - How do I access a variable on a passed Type?
What I'm trying to do is a pass a Type as a variable, then access a variable from the passed type. I have several classes which are subclasses of another.
My base class is called InventoryItem, its two subclasses look like this:
public class Animal : InventoryItem
{
public enum CategoryType {PIG = 0, CAT, NUM_CATEGORYTYPE}
public CategoryType categoryType = CategoryType.PIG;
}
public class Utility : InventoryItem
{
public enum CategoryType {POTION = 0, FENCE, ROAD, NUM_CATEGORYTYPE}
public CategoryType categoryType = CategoryType.POTION;
}
I want to create a sorted list from another script. In this script, inventoryList is a list of all the inventoryItems (these can be Animal's whose CategoryType is PIG or CAT, or they can be Utilities whose CategoryType is whatever). The goal of the function is to grab all the pigs and make a list out of them.
The function for this is something like:
InventoryItem[] inventoryList = new InventoryItem[20];
//classType refers to the class (either Animal or Utility)
//sortType refers to the enumerator
//The goal of the script is to return a list of Animals that are pigs
CreateSortedList<classType>(int sortType)
{
if(sortType == (int)classType.CategoryType.PIG)
{
print("Looking for pigs");
}
}
However, the compiler tells me that CategoryType is not a part of classType(because it doesn't know that classType can be an Animal or Utility).
What am I doing wrong here?
Answer by BadAssGames · Nov 15, 2013 at 08:37 AM
I came up with "A" solution. I welcome any other suggestions. It's a little sloppy, but it will work for my purposes. GetEnumerator() is a virtual function on the base class InventoryItem. Then I override it on all the subclasses. It just returns their categoryType as an int.
InventoryItem[] CreateSortedList<classType>(int sortType) where classType : class
{
System.Collections.Generic.List<InventoryItem> totalList = new System.Collections.Generic.List<InventoryItem>();//the totalList of classType
System.Collections.Generic.List<InventoryItem> subList = new System.Collections.Generic.List<InventoryItem>();//the sublist of classType
for(int i = 0; i < inventoryList.Length; i ++)
{
if(inventoryList[i] is classType)//If this is the classType we're looking for
{
totalList.Add (inventoryList[i]);//Add it to the totalList of classType
}
}
for(int i = 0; i < totalList.Count; i ++)
{
if(totalList[i].GetEnumerator() == sortType)//For every item in our totalList, get the enumerator and compare it to sortType
{
subList.Add (totalList[i]);
}
}
return subList.ToArray();
}
Answer by zanearn · Nov 15, 2013 at 07:35 AM
Can you put all these types into a single namespace (e.g. MyFarm)? If so, you can then using that namespace (using MyFarm) in classes that needs those types, and avoid future confusion.
OR you can simply put CategoryType in InventoryItem.
public enum CategoryType {PIG = 0, CAT, POTION, FENCE, ROAD, NUM_CATEGORYTYPE}
--Edit--
I think you should then clearly name these types AnimalCategoryType and UtilityCategoryType.
Otherwise (int)classType.CategoryType.PIG == (int)classType.CategoryType.POTION
if(sortType == (int)classType.CategoryType.PIG) // also true for classType.CategoryType.POTION
{
print("Looking for pigs"); // and potions
}
Avoid making traps for yourself.
Thanks for your answer, but I wanted to avoid having to put CategoryType in inventory item.
When you call the above function for POTIONs, PIGs are angry too.
They were named that way initially, actually. I renamed them to 'categoryType' universally because I thought that might solve my problem. I was wrong.
Actually I'm making a similar thing as yours, and I put all types categories into a single namespace. Not sure if this is the best way of doing it, so will also be glad to see a better solution to this.