0
    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
        }
5
  • What does "but it does not seems to work" mean? What is the error? Commented Dec 4, 2012 at 18:02
  • 1
    Would it be abc = cam.facesdetected; Commented Dec 4, 2012 at 18:02
  • I have declare it as: private int abc; Commented Dec 4, 2012 at 18:02
  • It looks like you want to parse the string "2" into an integer, is that correct? Commented Dec 4, 2012 at 18:03
  • It means that the return value is "0" for abc when messagebox is pop up but however, cam.facedeteced actual value is "1" cause its a webcame face detection return value Commented Dec 4, 2012 at 18:03

5 Answers 5

2

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.

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

3 Comments

Considering that the string appears to come from user provided input int.TryParse should probably be used instead.
I was just thinking the same thing.
Agreed. The details of the question were a bit.. vague.. so i generalized the answer.
2

I believe you've got your if statement backwards.

abc = cam.facesdetected;

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

Comments

0

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
    }

Comments

0

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 
}

Comments

0

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;   
         }   
      }

1 Comment

I believe your syntax to call TryParse is incorrect. See here: msdn.microsoft.com/en-us/library/f02979c7.aspx

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.