C#将变量从一种方法发送到另一种方法? [英] C# Sending variables from one method to another ?

查看:110
本文介绍了C#将变量从一种方法发送到另一种方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

c#windows form:



我正在尝试创建一个小绘图程序。



我无法将Form1_MouseClick字符串变量和Form1_MouseUp字符串变量发送到DrawLinesPoint。如何发送变量?





c# windows form:

I'm trying to create a little drawing program.

I can't send the Form1_MouseClick string variable and Form1_MouseUp string variable to the DrawLinesPoint. How do I send the variables ?


public void DrawLinesPoint(object sender, PaintEventArgs e)
      {

          // Create pen.
          Pen pen = new Pen(Color.Black, 3);
          // Create array of points that define lines to draw.
          Point[] points =
          {
          new Point(point1),
          new Point(point2)
          };
          //Draw lines to screen.
          e.Graphics.DrawLines(pen, points);
      }

      private void Form1_MouseClick(object sender, MouseEventArgs e)
      {

          string point1 = e.X + "," + e.Y;

      }

      private void Form1_MouseUp(object sender, MouseEventArgs e)
      {
          string point2 = e.X + "," + e.Y;

          this.Paint += new PaintEventHandler(DrawLinesPoint);
          Invalidate();
      }

推荐答案

发送字符串实际传递几个整数参数是一个很大的滥用。很难解释为什么这么多初学者表现出这种用于表示数据而不是数据的字符串的死胡同趋势。



并且传递参数是编程的基础之一,甚至在OOP之前。我会认真地告诉你:在熟悉基础知识之前,不要浪费时间开发任何UI,图形或任何其他高级编程元素。



它应该类似

"Sending" a string to pass actually a couple of integer parameters is a big, big abuse. It's hard to explain why so many beginners show this dead-end trend to work with strings representing data instead of data.

And passing parameters is one of the basics of programming, even before OOP. I'll tell you seriously: don't wast time on developing any UI, Graphics or any other advanced elements of programming before getting comfortable with the fundamentals.

It should be something like
void MyHandler(x, y) { /* ... */ }
// or
void AnotherHandler(System.Drawing.Point point) { /* ... */ }

//...

MyHandler(e.X, e.Y);
AnotherHandler(new System.Drawing.Point(e.X, e.Y));



等等on ...



-SA


您传递参考从一个方法/上下文/对象到另一个方法/上下文/对象的变量中的。是传递引用还是值,将取决于传递的对象的类型,引用或值。



有一种特殊的语法用于指示通过引用传递变量的方式是它的值可以被传递给它的对象改变('out,'ref),默认情况是通过引用传递的变量的Value Type内容将被复制。



你要意识到的第一件事就是当你在一个方法中声明一个变量时,那个变量只存在里面那个方法;从技术上讲,这表示当Method终止时变量超出范围。解决方法是在表单的级别(范围)声明变量。



其次,每次获得'MouseUp时,您都会声明一个新的PaintEventHandler:与声明变量不同,这将创建一个新的Event对象并将其添加到EventHandler的Invocation List中,因此您最终会得到一个非常臃肿的EventHandler!解决方法是在设计时或在表单加载事件的运行时将EventHandler 一次分配给Paint事件。



因此,您在'MouseClick和'MouseUp EventHandlers中声明和设置值的'point1和'point2变量永远不能在这些方法之外使用。



最后,你使用'MouseClick,而不是'MouseDown是一个错误;为什么WinForms的创建者让MouseClick没有像MouseDown那样注册它的位置...在你点击,然后拖动,然后鼠标向上的情况下...你最好问问微软。



试试这个:
You pass either a reference to a variable, or the value in a variable from one method/context/object to another. Whether a reference is passed, or a value, will depend on the Type of the object that is passed, reference, or value.

There is a special syntax for indicating passing a variable by reference in a way that its value(s) can be changed by the object it is passed to ('out, 'ref), the default case being that the Value Type contents of a variable passed by reference will be copied.

The first thing for you to realize is that when you declare a variable inside a Method, that variable "exists" only inside that Method; technically, this is expressed as saying the variable goes out of scope when the Method terminates. The remedy is to declare the variables at the level (the scope of) the Form.

Second, you are declaring a new PaintEventHandler every time you get a 'MouseUp: unlike declaring a variable this will create a new Event Object and add it to the Invocation List of the EventHandler, so you are going to end-up with a very bloated EventHandler ! The remedy is to assign the EventHandler once to the Paint Event, either at design-time, or, at run-time in the Form Load Event.

So, the 'point1, and 'point2 variables you declare and set the values of in the 'MouseClick and 'MouseUp EventHandlers can never be used outside those Methods.

Finally, your use of 'MouseClick, rather than 'MouseDown is a mistake; why the creators of WinForms made the MouseClick not register its location in the same way that MouseDown does ... in the case where you click, then drag, then mouse-up ... you better ask Microsoft about that.

Try this:
private void Form1_Load(object sender, EventArgs e)
{
    this.Paint += Form1_Paint;
}

private Point[] twoPoints = new Point[2];

private Pen pen = new Pen(Color.Black, 3);

private void Form1_Paint(object sender, PaintEventArgs e)
{ 
    //Draw lines to screen.
    e.Graphics.DrawLines(pen, twoPoints);
}

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
    twoPoints[0] = e.Location;
}

private void Form1_MouseUp(object sender, MouseEventArgs e)
{
    twoPoints[1] = e.Location;
    Invalidate();
}


使用类级变量,即make point1 point2 班级。

DrawLinesPoint 可以访问 point1 point2
Use class level variables i.e. make point1 and point2 class level.
DrawLinesPoint will have access to point1 and point2.


这篇关于C#将变量从一种方法发送到另一种方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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