C# If Statement

I have the following if statement.

if ((i % 2) == 0)
{
    yInitialSLTEloc = yInitialSLTEloc + 20;
    //picBoxSize_Y = yInitialSLTEloc - 65;
 }
 else
    yInitialSLTEloc = yInitialSLTEloc + 65;
    picBoxSize_Y = yInitialSLTEloc + 65;
    xInitalSLTELoc = gsp_X;

Open in new window


When I un-comment the picBoxSize_Y = yInitialSLTEloc - 65; line it becomes unhappy.

Any input?
JedidiaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gustav BrockCIOCommented:
I guess picBoxSize_Y can't be negative while yInitialSLTEloc probably can be smaller than 65.

So do something like:
if (yInitialSLTEloc > 65)
{
    picBoxSize_Y = yInitialSLTEloc - 65;
} 
else
{
    picBoxSize_Y = 0;
}

Open in new window

/gustav
JedidiaAuthor Commented:
hmmm

The main effort is to evaluate "i" to see if it is odd or even.

If Odd, I need it to run through

    yInitialSLTEloc = yInitialSLTEloc + 20;
   picBoxSize_Y = yInitialSLTEloc + 85;

If Even,

   yInitialSLTEloc = yInitialSLTEloc + 65;
    picBoxSize_Y = yInitialSLTEloc + 65;
    xInitalSLTELoc = gsp_X;

I don't think I have my If statement setup correctly.
Gustav BrockCIOCommented:
Then this should work:
if ((i % 2) == 0)
{
    yInitialSLTEloc = yInitialSLTEloc + 65;
    picBoxSize_Y = yInitialSLTEloc + 65;
    xInitalSLTELoc = gsp_X;
}
else
{
    yInitialSLTEloc = yInitialSLTEloc + 20;
    picBoxSize_Y = yInitialSLTEloc + 85;
}

Open in new window

/gustav

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
käµfm³d 👽Commented:
line it becomes unhappy.
Perhaps if you told us what the actual error is?
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Not sure if you modified the code before posting, but that second "block" in the else portion isn't a block without the braces.  Only the first line will get executed for the else condition.  The other two lines will always get executed.

I always put in braces, even when there's only a one-liner in there...because invariably you'll end up adding code to the block at some point.

So it should be:
if ((i % 2) == 0)
{
    yInitialSLTEloc = yInitialSLTEloc + 20;
    picBoxSize_Y = yInitialSLTEloc - 65;
}
else
{
    yInitialSLTEloc = yInitialSLTEloc + 65;
    picBoxSize_Y = yInitialSLTEloc + 65;
    xInitalSLTELoc = gsp_X;
}

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.