如何使表单应用程序中的代码在控制台应用程序中工作? [英] How do you make code that worked in a form app work in a console app?

查看:75
本文介绍了如何使表单应用程序中的代码在控制台应用程序中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码适用于表单应用。我将代码转移到控制台应用程序以进行多次分析,并对输入来自表单控件的变量进行了所有更正。所有代码都在控制台应用程序中工作,但与事件处理程序关联的代码除外。在下面的代码中,我在显示的位置得到以下2个错误:

错误1:CS0120非静态字段,方法或属性'Program.OnIterationEvent'<需要对象引用br />
错误2:CS0026关键字'this'在静态属性,静态方法或静态初始化程序中无效



我还没有能够使用谷歌搜索找到任何有用的东西。为什么事件处理程序在表单应用程序中工作但在控制台应用程序中不工作?非常感谢任何帮助。



我尝试过:



我有以下单独的课程:

Class IterationEventArgs.cs

//正常使用语句

 命名空间限制
{
IterationEventArgs:EventArgs
{
public int IterationNumber { get ; set ; }

public IterationEventArgs( int aNumber)
{
IterationNumber = aNumber;
}
}
}





主程序包括以下内容:



//正常使用语句加上:

//有或没有'public delegate void EventHandler();'



 命名空间限制
{
课程计划
{
public event EventHandler< IterationEventArgs> OnRaiseIterationEvent;
静态 void Main( string [] args)
{
// code
< span class =code-keyword> void RaiseIterationEvent( int iterationNumber)
{
if (OnRaiseIterationEvent!= null // 错误1
OnRaiseIterationEvent( this new iterationEventArgs(iterationNumber) ); // 错误1,2
else
return ;
}
}
}
}

我在Main方法中有一个调用RaiseIterationEvent方法的方法

for循环使用:



RaiseIterationEvent(iterationNumber1);

解决方案

请注意,main方法标记为静态



<前lang =c#> 静态 void Main( string [] args)





最初的问题是你试图使用的EventHandler是非静态的。

  public  事件 EventHandler< IterationEventArgs> OnRaiseIterationEvent; 





静态方法Main(),无法访问非静态成员OnRaiseIterationEvent。



可以只需将项目设为静态即可解决此问题。

  static   event  EventHandler< IterationEventArgs> OnRaiseIterationEvent; 





但是,可能还有其他含义。



导致第二个错误是因为静态方法没有这个变量的概念。这仅适用于实例对象(非静态)。


最简单的方法是记住Form是一个Class,尽管有额外的属性和方法。



因此,请使用基于表单的代码并将其全部放入类中,然后在 Main 方法中创建该类的实例并使用这一切都以类似的方式 - 但传递它所需的参数而不是从文本框中获取它们。

尝试强制代码进入静态上下文所涉及的变化要少得多 - 而且因为你写了基于表单的代码你已经理解它是如何工作的所以它应该是非常简单的,是吗?


如果你没有处理sender,这通常是事件中的第一个参数处理程序的签名,然后只传递null(而不是你正在尝试的)。



你可以传递任何你想要的东西;你不仅限于真正的发件人。


I have code that works in a form app. I transferred the code to a console app in order to do multiple analyses and have made all the corrections for inputting variables that came from the form controls. All the code works in the console app, except that which is associated with an event handler. In the code below, I get the following 2 errors in the locations shown:
Error 1: CS0120 An object reference is required for the non-static field, method, or property 'Program.OnIterationEvent'
Error 2: CS0026 Keyword 'this' is not valid in a static property, static method, or static initializer

I haven't been able to find anything using Google searches that helps. Why would the event handler work in a form app but not in a console app? Any help is much appreciated.

What I have tried:

I have the following separate class:
Class IterationEventArgs.cs
// normal "using" statements

namespace Limits
{
    class IterationEventArgs : EventArgs
    {
        public int IterationNumber { get; set; }

        public IterationEventArgs(int aNumber)
        {
            IterationNumber = aNumber;
        }
    }
}



The main program includes the following:

// normal "using" statements plus:
// with or without 'public delegate void EventHandler();'

namespace Limits
{
   Class Program
   {
      public event EventHandler<IterationEventArgs> OnRaiseIterationEvent;
      static void Main(string[] args)
      {
         // code
         void RaiseIterationEvent(int iterationNumber)
         {
            if (OnRaiseIterationEvent != null) // Error 1
                OnRaiseIterationEvent(this, new iterationEventArgs(iterationNumber)); //Error 1, 2
            else
                return;
         }
      }
   }
}

I have a method within the Main method that calls the RaiseIterationEvent method
within a for loop using:

RaiseIterationEvent(iterationNumber1);

解决方案

Notice that the main method is marked as static

static void Main(string[] args)



The initial problem is that the EventHandler you are attempting to use is non-static.

public event EventHandler<IterationEventArgs> OnRaiseIterationEvent;



The static method Main(), cannot access the non-static member OnRaiseIterationEvent.

You may be able to fix this simply by making the item static.

static event EventHandler<IterationEventArgs> OnRaiseIterationEvent;



But, there may be other implications to that.

Your second error is caused because your static method has no concept of the this variable. this is only available on instance objects (non-static).


The simplest way is to remember that a Form is a Class, albeit with extra properties and methods.

So take your Form based code and put it all in a class, then create an instance of that class in your Main method and use it all in a similar way - but passing in the parameters it needs instead of fetching them from textboxes.
There are a lot less changes involved that trying to force the code into a static context - and since you wrote the Forms based code you already understand how it works so it should be pretty trivial, yes?


If you're not handling "sender", which is usually the first parameter in an event handler's signature, then just pass "null" (instead of what you're trying).

You can pass anything you want; you're not restricted to the "real" "sender".


这篇关于如何使表单应用程序中的代码在控制台应用程序中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆