C#从其他类调用函数

时间:2017-08-19 15:31:24

标签: c#

我试图运行" progr.SetMonitorState(MonitorState.OFF)" in" public static void Main()",但我在尝试编译时遇到错误:

  1. " SetMonitorState"在progr名称空间中不存在......
  2. " MonitorState"在当前的背景下不存在......
  3. 我只是试图制作关闭我的电脑并关闭显示器的程序(我想检查它是否会关闭通过HDMI与PC连接的电视)

    代码:

    using System;
    using System.Diagnostics;
    using System.Runtime;
    using System.Data;
    using System.Runtime.InteropServices;
    using System.Timers;
    using System.Windows.Forms;
    
    namespace progr
    {
        public class MonitorSht
        {
            public int SC_MONITORPOWER = 0xF170;
            public uint WM_SYSCOMMAND = 0x0112;
    
            [System.Runtime.InteropServices.DllImport("user32.dll")]
            static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
    
            public enum MonitorState
            {
                ON = -1,
                OFF = 2,
                STANDBY = 1
            }
    
            public void SetMonitorState(MonitorState state)
            {
                Form frm = new Form();
    
                SendMessage(frm.Handle, WM_SYSCOMMAND, (IntPtr)SC_MONITORPOWER,(IntPtr)state);
    
            }
        }
    
        public class Shut
        {  
           public static void Main()
           {
              progr.SetMonitorState(MonitorState.OFF);
    
              Console.Write("Jestem jebanym leniem :)\n\n");      
              int t=0;
              for (int i=0;i<7;i++) 
              {       
                  Console.WriteLine("[{0}] {1} min", i,t);
                  t=t+30;
              }
              Console.Write("[7] Anuluj\n");
              Console.Write("\n\nZa ile wyłączyć sprzęcior?");
    
              int n = int.Parse(Console.ReadLine());
    
              switch (n)
              {
                  case 0: n=0; break;
                  case 1: n=1800; break;
                  case 2: n=3600; break;
                  case 3: n=5400; break;
                  case 4: n=7200; break;
                  case 5: n=9000; break;
                  case 6: n=10800; break;
              }
    
              string sht = "/s /t " + n;
              if (n==7) {sht = "/a";}
    
              Console.Write(sht);
              var psi = new ProcessStartInfo("shutdown",sht);
              psi.CreateNoWindow = true;
              psi.UseShellExecute = false;
              Process.Start(psi);
    
              Console.ReadLine();
           }
        }
    }
    

3 个答案:

答案 0 :(得分:5)

您的MonitorSht不是静态的,因此您需要创建一个实例才能使用其方法。

public static void Main()
{
   var monitorSht = new MonitorSht();
   monitorSht.SetMonitorState(MonitorSht.MonitorState.OFF);

   Console.Write("Jestem jebanym leniem :)\n\n");   
   /*...*/
}

您也可以将枚举放在类的范围之外,最好放在新文件中。

public class MonitorSht
{
 /*...*/
}

public enum MonitorState
 {
     ON = -1,
     OFF = 2,
     STANDBY = 1
 }

然后你可以写:

var monitorSht = new MonitorSht();
monitorSht.SetMonitorState(MonitorState.OFF);

您应该阅读有关课程如何运作的更多信息。

答案 1 :(得分:2)

你需要在主方法中从你的MonitorSht创建实例,如下所示:

MonitorSht somename = new MonitorSht();

然后通过实例调用您的方法,如下所示: somename.SetMonitorState(MonitorState.OFF)

答案 2 :(得分:1)

尝试这样做,因为你的问题是从另一个类调用一个方法

using [projectName].[classLocation];

例如

using projectX.Models;

然后创建一个对象的实例 例如

SetMonitorState Monitor = new SetMonitorState();

然后获取此类中的方法只需调用Monitor 例如

Monitor.ShutDown();