Derek
Derek

Reputation: 249

Simple If Else C# Statement

    private void ProcessFrame(object sender, EventArgs arg)
    {
        Wrapper cam = new Wrapper();

        //show the image in the EmguCV ImageBox
        WebcamPictureBox.Image = cam.start_cam(capture).Resize(390, 243, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC).ToBitmap();
        FaceDetectedLabel.Text = "Faces Detected : " + cam.facesdetected.ToString();
    }

I am working on a C# windows application. I am stuck with a simple question:

How can i do a if else with the condition: If "cam.facesdetected.ToString()" if equal or more than 2 do sth, else do nothing.

I tried this, but it does not seems to work. Can anyone help me?

        cam.facesdetected = abc;
        MessageBox.Show("The detected faces is:" + abc);

        if (abc >= 2)
        {
            //Do action 
        }

        else
        {
            //Do nothing
        }

Upvotes: 0

Views: 695

Answers (5)

Ravindra Bagale
Ravindra Bagale

Reputation: 17655

you can only assign leftside variable by rightside variable

LHS=RHS

you have wrongly assigned it must be abc = cam.facesdetected;

and you can check whether it is greater than & equal to 2 by using TryParse Method

   bool result = Int32.TryParse(abc, out number);
      if (result)
      {
         if(number>=2)
        {
          //dowork;   
         }   
      }

Upvotes: 0

lante
lante

Reputation: 7336

Dont use .ToString() when you use numbers.

You are assigning the variable in a wrong way, this should be

var abc = cam.facesdetected;

if cam.facesdetected is not a number then use

var abc = Convert.ToInt32(cam.facesdetected);

and then

if (Convert.ToInt32(abc) >= 2)
{
   //Do action 
}

Upvotes: 0

Nikola Davidovic
Nikola Davidovic

Reputation: 8656

I think that you should use it without assigning any new variable. There is no need to use the abc variable. You can use cam.facesdetected directly (I suppose that it is a number) like this:

    MessageBox.Show("The detected faces is:" + cam.facesdetected.ToString());

    if (cam.facesdetected >= 2)
    {
        //Do action 
    }

    else
    {
        //Do nothing
    }

Upvotes: 0

David Yaw
David Yaw

Reputation: 27864

I believe you've got your if statement backwards.

abc = cam.facesdetected;

Now you can operate on abc, as you had listed.

Upvotes: 2

Michael
Michael

Reputation: 1833

You could:

if (Convert.ToInt32(abc) > 2)
   DoWork()

Although it would probably be wise to declare ABC as an integer to begin with, if it is always an integer.

Upvotes: 2

Related Questions