Reputation: 86
I am trying to create a "Mario" video game, and in order to obtain intersecting objects, our teacher provided us with an example demonstration, which allows the object to detect if it is touching EXACTLY one other object, and the first object that the method finds, is returned. I am trying, instead to return an array of every object that the current object is currently touching. I was capable of returning an array of every object currently touching it, but now I need an easy/efficient way to check if the array contains an object of a required type, such as
if (array.Contains(Mario))
{
//Do Work here
}
The array that is being checked if it Contains(Mario), is the returned array of the intersecting Sprites, but when I ask if it actually contains objects of type Mario, it says "Error 14 'WindowsGame10.Mario' is a 'type' but is used like a 'variable' ". I Know I could do this with a for loop, and ask each individual index within the array if (array[i].GetType() == typeof(Mario))
, but for the amount of times I would need to perform this check within the code, and retype the same code over and over again, I feel that I need to learn a more efficient way to perform this. I am in my first year of Computer Programming, and I am working with C# XNA, and I need to have some solution that I can understand. If there is a better way to do this, please let me know.
Upvotes: 2
Views: 2482
Reputation: 6282
Most straight forward way (without using LINQ) would be to use the is
keyword:
foreach (Object obj in array)
{
if (obj is Mario)
{
...
}
}
Update:
If you need an exact Type
match, and want to avoid code duplication, I think a rather simplistic extension method similar to this would be best -
public static bool ContainsType<T>(this T[] arr, Type type)
{
for (int i = 0; i < arr.Length; i++)
if (arr[i].GetType().Equals(type))
return true;
return false;
}
And the use -
if (array.ContainsType(typeof(Mario)))
Upvotes: 1
Reputation: 150108
The Linq answers are great. Just to provide an alternative approach, you could define an extension method like this:
static public bool ContainsType(this Array array, Type type)
{
int len = array.Length; // Note that putting this in the loop is slightly slower
// because the compiler can't assume that a property value
// remains constant.
for (int i = 0; i < len; i++)
{
if (array.GetValue(i).GetType() == type) return true;
}
return false;
}
that would be used like this:
Array a = new object[] { "The meaning of life.", 42 };
bool hasString = a.ContainsType(typeof(string)); // true
bool hasInteger = a.ContainsType(typeof(int)); // true
bool hasChar = a.ContainsType(typeof(char)); // false
Upvotes: 2
Reputation: 2961
if you just need to know if there is any Mario then
if(array.Any(a=>a is Mario))
{
}
this will break out of the iteration as soon it finds the first match.
Upvotes: 2
Reputation: 52788
Using Linq you can do:
var marios = array.OfType<Mario>();
if (marios.Any())
{
//Stuff
}
Upvotes: 4
Reputation: 564363
You can use LINQ's OfType()
and Any()
methods:
if (array.OfType<Mario>().Any())
{
Upvotes: 6