Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

This is my public struct.
C#
struct tpoint{public int x,y;} //can access in all of my project

And this is my method
C#
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);
    }
}

How can I call this method in a button?

I have problem with call my struct. :(

Please help me.
Posted
Updated 3-Dec-13 9:08am
v2
Comments
Sergey Alexandrovich Kryukov 3-Dec-13 17:30pm    
No, "public" means not "access in all my project". It means "can access in any assembly, the current one or referencing the current one". If you want to access it only in current assembly, use "internal".
Look, was that so hard to read standard MSDN documentation?
—SA

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:
C#
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
C#
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,});


You have too many bugs and design mistake for such short code. It looks like you did not learn syntax and semantic of the language and .NET. Better start again, from scratch, pay more attention, use less of trial-and-error approach.

—SA
 
Share this answer
 
C#
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);
 
Share this answer
 
Comments
ali_vampire 3-Dec-13 15:33pm    
can u help me to give my number in textbox with a for?
my soloution have an error :-s

private void txtLenght_TextChanged(object sender, EventArgs e)
{
n = int.Parse(txtLenght.Text);
}
tpoint[] tp = new tpoint[n];

for (int i = 0; i < n; i++)
{
tp[i] = new tpoint();
tp[i].x = int.Parse(txtX.Text);
tp[i].y = int.Parse(txtY.Text);
txtX.Clear();
txtY.Clear();
}
Adam Zgagacz 3-Dec-13 15:58pm    
Looks roughly Ok. it should execute as long as content of txtLength.Tex, txtX.Text and txtY.Text contain numnerics only (no decimal points on letters).
and of couse you need to remove lines:
txtX.Clear();
txtY.Clear();

because in the second iteration it will fail becuase content onf txtY and txtY will be "" and int.Parse will throw an exception.
ali_vampire 3-Dec-13 16:18pm    
i cant pass my var in step 2 or step 3 :-\
ali_vampire 3-Dec-13 16:26pm    
i solved it,,,thanks bro,,,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900