我怎么称呼我的方法? [英] how can i call my method?

查看:85
本文介绍了我怎么称呼我的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





这是我的公共结构

Hi,

This is my public struct.

struct tpoint{public int x,y;} //can access in all of my project



这是我的方法


And this is my method

void MyDrawpoly(int numpoint,tpoint[] p)
{
    int i;
    for (i=0; i <numpoint-1 mode="hold" />            {
     bresline(p[i].x,p[i].y,p[i+1].x,p[i+1].y);
     bresline(p[i].x, p[i].y, p[0].x, p[0].y);
    }
}



如何在一个按钮中调用此方法?



调用我的时遇到问题struct 。:(



请帮帮我。


How can I call this method in a button?

I have problem with call my struct. :(

Please help me.

推荐答案

方法本身完全错误。第一个参数过多。 numpoints 应该从 p 取为 p .Length 。如果有人在参数中调用不匹配,你会遇到麻烦:要么不处理数组的所有元素,要么会出现超出范围的异常。在循环之前声明look变量(参见代码示例)。



此外,该方法不使用任何实例成员。所以,它应该是静态。为了获得更好的样式和可维护性,请用属性替换字段:

The method itself it totally wrong. First parameter is excessive. numpoints should be taken from p as p.Length. You will get into trouble is someone call it with mismatch in the parameters: either not all elements of array will be processed, or there will be the out-of-range exception. Also, never declare look variable before the loop (see the code sample).

Moreover, the method does not use any instance members. So, it should be static. For better style and maintainability, replace fields with properties:
struct Point {
   public int X { get; set; } 
   public int Y { get; set; } 
   public static void MyPoly(Point[] points) { // why such weird name?
      if (points == null) return; // always check up for null
      for (int index = 0; index < points; ++index) { /* ... */ }
   }
}

//...

Point.MyPoly(new Point[] {1, 2, 3, 4,});






or

struct Point {
   public int X { get; set; } 
   public int Y { get; set; } 
}

static class SomeOtherType {
   public static void MyPoly(Point[] points) { // why such weird name?
      if (points == null) return; // always check up for null
      for (int index = 0; index < points; ++index) { /* ... */ }
   }
}

//...

SomeOtherType.MyPoly(new Point[] {1, 2, 3, 4,});





这些短代码有太多错误和设计错误。看起来你没有学习语言和.NET的语法和语义。从头开始,更好地重新开始,更多关注,减少尝试和错误的方法。



-SA


tpoint[] tp = new tpoint[3];
tp[0]= new tpoint();
tp[0].x = 0; tp[0].y = 0;
tp[1]= new tpoint();
tp[1].x = 100; tp[1].y = 190;
tp[2]= new tpoint();
tp[2].x = 0; tp[2].y = 190;

MyDrawpoly(tp.Length, tp);


这篇关于我怎么称呼我的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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