Problem getting back Results from Classes in a different namespace.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • raulbolanos
    New Member
    • Apr 2009
    • 31

    Problem getting back Results from Classes in a different namespace.

    Hello guys, I got this problem.

    I have a Window Form project within on it I have 2 Buttons and 1 Label

    btSet (button)
    btGet (button)
    lbResult (label)

    When I click on btSet I save the value in other class in a different namespace.
    Code:
    private void btSet_Click(object sender, EventArgs e)
            {
                RailLib.Alarm aux = new RailLib.Alarm();
                aux.AlarmResetMask = 5;
            }
    Code:
    namespace RailLib
    {
        public class Alarm
        {
            public int AlarmResetMask;        
        }
    }
    The problem is that when I want to get back the value with the get button, the class is empty.. what I can do to dont lose the saved values:

    Code:
    private void btTry_Click(object sender, EventArgs e)
            {
                RailLib.Alarm aux = new RailLib.Alarm();
                lbResult.Text = aux.AlarmResetMask.ToString();
                Refresh();
            }
    The return value in the label field its 0. And I really saved a lot of values within the RailLib namespace and when I want to get them back with other button, it seems to be that they are not there anymore.

    Preveously I had to move all my classes to RailLib because if I leave them in the same namespace as the form class is I got this error:

    The class Form1 can be designed, but is not the first class in the file. Visual Studio requires that designers use the first class in the file. Move the class code so that it is the first class in the file and try loading the designer again.

    What do you think?
  • cloud255
    Recognized Expert Contributor
    • Jun 2008
    • 427

    #2
    Hi

    When you try to access the class to read the data you say:

    Code:
     RailLib.Alarm aux = new RailLib.Alarm();
    This means that you now have a NEW instance, so it doesn't contain any data...
    The other problem is that the first instance you declared is now out of scope so you will not be able to access it.

    The variable only lives as long as the scope you declared it in.

    You could use one global instance of the object declared within the form class,
    You could create a static class,
    You could save the data in a database/file for later use.

    Comment

    • Bassem
      Contributor
      • Dec 2008
      • 344

      #3
      Or you could make a singleton just like a static class but using a static field instead to avoid the static class restrictions.
      Sure the easiest is a global instance.

      Comment

      Working...