简体   繁体   English

如果其他C#语句简单

[英]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. 我正在使用C#Windows应用程序。 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. 我该如何处理以下情况:如果“ cam.facesdetected.ToString() ”等于或大于2,则不执行其他操作。

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
        }

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. 尽管将ABC声明为以整数开头的整数可能是明智的选择,但始终将其声明为整数。

I believe you've got your if statement backwards. 我相信您的if陈述倒退了。

abc = cam.facesdetected;

Now you can operate on abc , as you had listed. 现在,您可以像列出的那样对abc进行操作。

you can only assign leftside variable by rightside variable 您只能通过右侧变量分配左侧变量

LHS=RHS

you have wrongly assigned it must be abc = cam.facesdetected; 您分配错误,必须是abc = cam.facesdetected;

and you can check whether it is greater than & equal to 2 by using TryParse Method 您可以使用TryParse方法检查它是否大于或等于2

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

I think that you should use it without assigning any new variable. 我认为您应该在不分配任何新变量的情况下使用它。 There is no need to use the abc variable. 无需使用abc变量。 You can use cam.facesdetected directly (I suppose that it is a number) like this: 您可以像这样直接使用cam.facesdetected (我想这是一个数字):

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

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

    else
    {
        //Do nothing
    }

Dont use .ToString() when you use numbers. 使用数字时不要使用.ToString()

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 如果cam.facesdetected不是一个数字,则使用

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

and then 接着

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM