1

Is there any way to check if a array exists or has a value in a specific element?

I have this line of code

if (rummen[positionX, positionY].Levandesaker[0].alive == true)

And it works fine as long as it exists. But what I want to do is

if (rummen[positionX, positionY].Levandesaker != null)
{
    if (rummen[positionX, positionY].Levandesaker[0].alive == true)
    {
    }
}

Does anyone know what Im after or can help me out with what im looking for?

3
  • You'll have to tell us what sort of expression rummen[positionX, positionY].Levandesaker is. Is that an array? Have you tried what you want to do? Commented Feb 24, 2009 at 20:19
  • I'd say @mquander has your answer, but the "it" in "And it works fine as long as it exists." is a bit vague. Commented Feb 24, 2009 at 20:25
  • @Jeff Martin you are truly correct. It's a pretty bad example when viewing it after a while. I promise that it was quite clear to me when I posted it. I wanted a way to check if array[] existed or not before doing anything else to the array which would make it crash, since it didnt exist Commented Feb 24, 2009 at 21:05

4 Answers 4

3
if (rummen[positionX, positionY].Levandesaker != null &&
    rummen[positionX, positionY].Levandesaker.Count > 0)
{
    if (rummen[positionX, positionY].Levandesaker[0].alive == true)
    {
    }
}

I'm not entirely sure which array you're talking about now that I think about it. Are you referring to Levandesaker or rummen?

Sign up to request clarification or add additional context in comments.

1 Comment

array.Count > 0 This was precisly what I was looking for :) Thanks for the very quick answer.
3

I hope I interpreted this question right!

An array is of constant size, and contains a value for every index from 0 to the upper bound of the array.

So to check whether a position in the array exists, you can just make sure it's less than the upper bound. Something like this should cover absolutely every condition (assuming rummen itself is not null!):

// make sure rummen contains the [positionX, positionY] element
if(rummen.GetUpperBound(0) > positionX && rummen.GetUpperBound(1) > positionY)
{
    if(rummen[positionX, positionY].Levandesaker != null)
    {
        // make sure Levandsaker contains at least one element
        if(rummen[positionX, positionY].Levandsaker.Length > 0)
        {
            if(rummen[positionX, positionY].Levandesaker[0].alive == true)
            {
            }
        }
    }
}

EDIT: Sorry, fixed C#-specific syntax. Also added a check on Levandsaker for demonstration purposes.

1 Comment

should probably make sure that rummen isn't null before checking upperbound.
1

If you want to check if something is an array or not, check for the Array type:

if (rummen[positionX, positionY].Levandesaker is Array) { ... }

Comments

0

the array aspect is immaterial (your code already assumes the entry is non null)

I assume the following:

public class Rummen
{
    public property Levandesaker { get; }
}

public class Levandesaker
{
    public bool alive
}

which lets you do:

public static bool LevandesakerExistsAndAlive(this Rummen r)
{
    return (r.Levandesaker != null && r.Levandesaker.alive);     
}

if (rummen[positionX, positionY].LevandesakerExistsAndAlive())
{
}

note that this assumes you do not control the Rummen class (otherwise you could simply make this an instance method or property and be done with it)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.