繁体   English   中英

为什么不能将类型 'void' 隐式转换为 'bool'?

[英]why Cannot implicitly convert type 'void' to 'bool'?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Diagnostics;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;

namespace ClassLibrary1
{
    public class Class1
    {
        public void open_swfile(string filepath, int x, string pgid)
        {
            SldWorks swApp = null;
            ModelDoc2 swAssemModleDoc = null;
            //SldWorks swApp;

            if (x == 0)
            {
                MessageBox.Show("no SolidWorks");
            }
            else if (x == 1)
            {
                System.Type swtype = System.Type.GetTypeFromProgID(pgid);
                swApp = (SldWorks)System.Activator.CreateInstance(swtype);
                //swApp.Visible = true;
                swAssemModleDoc = (ModelDoc2)swApp.ActiveDoc;
            }
        }

        public static void ProcessExited()
        {
            Process[] processes = Process.GetProcessesByName("SLDWORKS");

            foreach (Process process in processes)
            {
                if (process.ProcessName == "SLDWORKS")
                {
                    return true;
                }
            }

            return false;
        }

        public static void DoKillOnce()
        {
            Process[] processes = Process.GetProcessesByName("SLDWORKS");

            foreach (Process process in processes)
            {
                if (process.ProcessName == "SLDWORKS")
                {
                    try
                    {
                        process.Kill();
                    }
                    catch
                    {
                    }
                }
            }
        }

        public static void KILLSW()
        {
            if (ProcessExited())
            {
                do
                {
                    DoKillOnce();
                } while (ProcessExited());
                MessageBox.Show("Soldiworks process clean!");
            }
            else
            {
                MessageBox.Show("no process!");
            }
        }
    }
}

获取错误:

CS0103 当前上下文中不存在名称“MessageBox” ClassLibrary2 23 Active
错误 CS0127 由于 'Class1.ProcessExited()' 返回 void,返回关键字后不得跟随对象表达式 ClassLibrary2 40 Active
错误 CS0127 由于 'Class1.ProcessExited()' 返回 void,返回关键字后不能跟随对象表达式 ClassLibrary2 43 Active
错误 CS0029 无法将类型“void”隐式转换为“bool”ClassLibrary2 65 Active
错误 CS0029 无法将类型“void”隐式转换为“bool” ClassLibrary2 70 Active
错误 CS0103 当前上下文 ClassLibrary2 中不存在名称“MessageBox” 71 Active
错误 CS0103 当前上下文 ClassLibrary2 中不存在名称“MessageBox” 75 Active

警告 NU1701 使用“.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, . NETFramework,Version=v4.8”不是“.NETStandard,Version=v2.1”还原包“SolidWorks.Interop 16.10.0”。这个包可能与项目不完全兼容。 类库2 1

void不是一种类型,更像是一种类型的缺失。

这意味着方法不返回值。 您试图从声明为不返回值的方法返回一个值(在本例中为 true 或 false)。

如果要返回类型,则必须在方法上指定返回类型

public static bool ProcessExited() 
{
  return true;
}

进行以下更正:

CS0103 当前上下文中不存在名称“MessageBox” ClassLibrary2 23 Active

  • 右键单击Project > Add > Reference > Assemblies > System.Windows.Forms

错误 CS0127 由于 'Class1.ProcessExited()' 返回 void,返回关键字后不能跟随对象表达式 ClassLibrary2 43 Active

  • ProcessExited()返回类型从Void更改为bool

笔记

您可以使用内置属性Process.HasExited来检查关联的进程是否已终止。

 if (process.HasExited==true)
 {
   //This process has exited, do something 
 } 

暂无
暂无

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

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