Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Can some please help me this is my code

C#
// the death movment
private void timer2_Tick(object sender, EventArgs e)
{
    int movement = (label2.Text == "This Is Level 1") ? 5 : 15;
    // level 1
    if (label2.Text == "This Is Level 1")
    {
        if (Death.Width == 64)
        {
            Death.Location = new Point(Death.Location.X + movement, Death.Location.Y);
        }

        if (Death.Location == new Point(390, 202))
        {
            Death.Width = 63;
        }
        if (Death.Width == 63)
        {
            Death.Location = new Point(Death.Location.X - movement, Death.Location.Y);
        }

        if (Death.Location == new Point(30, 202))
        {
            Death.Width = 64;
        }
    }
    // level 2
    if (label2.Text == "This Is Level 2")
    {
        if (Death.Width == 64)
        {
            Death.Location = new Point(Death.Location.X + movement, Death.Location.Y);
        }

        if (Death.Location == new Point(390, 202))
        {
            Death.Width = 63;
        }
        if (Death.Width == 63)
        {
            Death.Location = new Point(Death.Location.X - movement, Death.Location.Y);
        }

        if (Death.Location == new Point(30, 202))
        {
            Death.Width = 64;
        }
    }

}


I tried asking this in another post but i got no answer, so anyway, i dunno what this part does.

int movement = (label2.Text == "This Is Level 1") ? 5 : 15;


at this end part

? 5 : 15;
Posted
Updated 21-Feb-11 2:39am
v2

See what the documentation says[^]. :)

After reading the documentation about the conditional operator
(?:)
you will figure out that if the text of label2 is exactly equal to "This Is Level 1" then movement will be 5 otherwise movement will be 15. So you can rewrite the same line as:
C#
int movement = 15; // Initially 15

// Check if we need to assign 5 as value
if(label2.Text == "This Is Level 1")
  movement = 5;

See that there is no need for else block, because I choose to initialize movement with 15. I hope it is all clear now. :)
 
Share this answer
 
v4
Comments
Manas Bhardwaj 21-Feb-11 5:53am    
+5
Nuri Ismail 21-Feb-11 5:57am    
Thank you, Manas!
Nuri Ismail 21-Feb-11 5:57am    
@EdMan196: Thanks a lot for the typo fix.
Ed Nutting 21-Feb-11 6:22am    
No worries ;)
Dalek Dave 21-Feb-11 6:06am    
Good Call.
C#
int movement = (label2.Text == "This Is Level 1") ? 5 : 15;


OR
C#
int movement = 0;
if(label2.Text == "This Is Level 1")  
   movement = 5;
else 
   movement = 15;


Thanks,
Imdadhusen
 
Share this answer
 
v2
Comments
Manas Bhardwaj 21-Feb-11 5:53am    
+5
Dalek Dave 21-Feb-11 6:07am    
Good Answer.
int movement = 15;
if (label2.Text == "This Is Level 1")
    movement = 5;
 
Share this answer
 
Comments
Sunasara Imdadhusen 21-Feb-11 5:21am    
Hmm, good call!!
Manas Bhardwaj 21-Feb-11 5:38am    
thnx :)
This:

C#
int movement = (label2.text == "This is Level 1") ? 5 : 15;


Is the same as saying this:

C#
int movement;
if (label2.text == "This is Level 1")
{
    movement = 5;
}
else
{
    movement = 15;
}


It simply takes less room, and therefore keeps the code nice and tidy.

And by the way, if you do that, you do NOT need both blocks of code (as I indicated in the answer you're talking about). Go back and look at it again.
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900