简体   繁体   中英

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!");
            }
        }
    }
}

Getting errors:

CS0103 The name 'MessageBox' does not exist in the current context ClassLibrary2 23 Active
Error CS0127 Since 'Class1.ProcessExited()' returns void, a return keyword must not be followed by an object expression ClassLibrary2 40 Active
Error CS0127 Since 'Class1.ProcessExited()' returns void, a return keyword must not be followed by an object expression ClassLibrary2 43 Active
Error CS0029 Cannot implicitly convert type 'void' to 'bool' ClassLibrary2 65 Active
Error CS0029 Cannot implicitly convert type 'void' to 'bool' ClassLibrary2 70 Active
Error CS0103 The name 'MessageBox' does not exist in the current context ClassLibrary2 71 Active
Error CS0103 The name 'MessageBox' does not exist in the current context ClassLibrary2 75 Active

Warning NU1701 using“.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”not“.NETStandard,Version=v2.1”Restore package“SolidWorks.Interop 16.10.0”。This package may not be fully compatible with the project. ClassLibrary2 1

void isn't a type, more the absence of a type.

It means that a method doesn't return a value. You are trying to return a value (true or false in this case) from a method that is declared as returning no value.

You have to specify a return type on the method if you want to return a type

public static bool ProcessExited() 
{
  return true;
}

Make the following corrections:

CS0103 The name 'MessageBox' does not exist in the current context ClassLibrary2 23 Active

  • Right Click Project > Add > Reference > Assemblies > System.Windows.Forms

Error CS0127 Since 'Class1.ProcessExited()' returns void, a return keyword must not be followed by an object expression ClassLibrary2 43 Active

  • Change return type of ProcessExited() from Void to bool

Note

You can use inbuilt property Process.HasExited to check the associated process has been terminated.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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