Can not set value to variable...

Ok this problem is wird to me, so please help...
I have an Struct...

public struct PointStruct
{
     public Point FirstPoint;
     public Point FinalPoint;
     public string Tipo;
     public string MuebleTipo;
}

[in my form]

PointStruct[] Lista = new PointStruct[99];
// guide
public string tipodibujo = "";
public string tipomueble = "";

then a event

// OnMouseDown
private void CartesianPlane_MouseDown(object sender, MouseEventArgs e)
{
     // if left click
     if (e.Button == MouseButtons.Left)
     {
          // HERE IS THE PROBLEM
          // get mouse coords and set struct
          Lista[j] = new PointStruct();
          Lista[j].FirstPoint.X = e.X;
          Lista[j].FirstPoint.Y = e.Y;
          a.X = e.X;
          a.Y = e.Y;
          // other
          Lista[j].Tipo = tipodibujo;
          if (Lista[j].Tipo == "Furniture")
          {
               Lista[j].MuebleTipo = tipomueble;
          }
          // show line guide
          pinta = true;
     }
     else
     {
          // hide line guide
          pinta = false;
     }
}

[The Problem]

I select another event (menuitem) there i change the values of
// Bath
private void MuebleItemBano_Click(object sender, EventArgs e)
{
     this.tipodibujo = "Furniture";
     this.tipomueble = "Bath";
}
Then i pass to another task, where i call the Event OnMouseDown, there:

the Lista[j].FirstPoint get the eX, e.Y values successfuly...

but Lista[j].Tipo and Lista[j].MuebleTipo dont get any value, they are in Null, why ...

Please help




Answer this question

Can not set value to variable...

  • Mohideen99

     cgraus wrote:

    Where is j being set Are you sure you've stepped through and you're saying the values don't go into the array at all

    Why do you have an array of 99 values It looks to me like you have a dynamic number of structs and should be using a list

     

    Please download the code here for full example code...



  • lcVic

    The first step is to trace through the code by setting breakpoints in both events, to make sure the second one is firing, and check the state of the various variables when the first one runs afterward.



  • FKy

    I don't understand. When you get to Lista[j].Tipo = tipodibujo;, does it equal "Furniture", but it does not branch as you'd expect

  • Philippe.Jung

    OK, I will see that... and tell you what happends... thx

  • Mohammed A.AboBakr

    Where is j being set Are you sure you've stepped through and you're saying the values don't go into the array at all

    Why do you have an array of 99 values It looks to me like you have a dynamic number of structs and should be using a list



  • Pham Vu Khanh

    You should get rid of the array and use a list, so that you can support more than 99 items, and also get rid of j. You should also use more descriptive variable names.

    I think your problem may be here:

    // Get Point

    public Point PuntoPerpendicular(Point a, Point b)

    {

    Point punto = new Point();

    // hace que punto a sea el origen

    b.X = b.X - a.X;

    b.Y = b.Y - a.Y;

    // lo rota 90° y luego lo entrega al formulario siguiendo la formula

    // donde & = 90°

    // x = xcos& - ysen&

    // y = xsen& + ycos&

    punto.X = -b.Y + a.X;

    punto.Y = b.X + a.Y;

    return punto;

    }

    You have a member variable named b, but are you trying to use the member variable, or the b you've passed in In either case, your core problem is poor variable naming. Use this.b to specify you want to set the member variable.

    I suspect if you move to using a List<PointStruct> you'll solve your problem along the way.



  • MBritten

    Indeed, move all to a List<Struct> made it works... thx alot again...

  • Bill CC

    No, I will try to explain it better...

    1 step, select from my Menu, the kind of "Line" to draw ... "Wall" for example, then tipodibujo = "Wall";

    2 step, mouse down, to set the first point (100,100), (here we create the struct) and set the current values:

    Struct.FirstPoint.X = 100,
    Struct.FirstPoint.Y = 100;
    Struct.Tipo = "Wall"; // must be Wall

    3 step, mouseUp to set the FinalPoint (200,200) and increment "j", and show the tipodibujo value in the Statusbar Label this works greate until i want to save the "drawing"...

    4 step, select save option in menu, here i write an xml file ... here my app crash, because every struct have a null value in the Tipo field...
    this means:

    Struct.FirstPoint = 100,100;
    Struct.FinalPoint = 200,200;
    Struct.Tipo = Null; // but we set the string value before...



  • Riekey

    I did, but the value the value has change... i test it with...

    // MouseUp Event
    StatusLabelCoords.Text = e.Location.ToString();
    StatusLabelDibujo.Text = tipodibujo;

    the MenuItem Event works, because in my statusbar Label I can see the value... tipodibujo
    but the Struct in this case Lista[j].Tipo can get the value...



  • rodolfo allendes

    No worries, glad to help.



  • Can not set value to variable...