none
Access value from a class to use in a form.cs RRS feed

  • Question

  • Hello guys,

    I am trying to get access to a value that happens in another class and make use of that value to run a while loop in Form1.cs I have tried a few things but have no success and unsure how to use getters/setters in a situation like such. Ultimately I want to take that 750 value in CountValue and use it as the basis for my loop. Any help/direction would be greatly appreciated.

    Here is my Class:

    public class LabJackT4
        {
            #region Public Property
            private double myCountValue = 0;
            #endregion
    
            #region .ctor
            public LabJackT4()
            {
                //Set Default Value
                //this.CountValue = 0;
            }
            #endregion
            
            public double MyCount
            {
                get { return myCountValue; }
                protected set { myCountValue = CountValue; }
            }
            protected
    
            #region T4 Handle Definitions
            int handle = 0;
            int devType = 0;
            int conType = 0;
            int serNum = 0;
            int ipAddr = 0;
            int port = 0;
            double voltage = 0;
            public double CountValue = 0;
            int maxBytesPerMB = 0;
            string ipAddrStr = "";
            #endregion
    
            #region Public Methods
            public void ConvertFromDAC()
            {
                try
                {
                    LJM.OpenS("ANY", "ANY", "ANY", ref handle);
                    LJM.GetHandleInfo(handle, ref devType, ref conType, ref serNum, ref ipAddr, ref port, ref maxBytesPerMB);
                    LJM.NumberToIP(ipAddr, ref ipAddrStr);
    
                    
                }
                catch (LJM.LJMException c)
                {
                    showErrorMessage(c);
                }
                //if (devType == LJM.CONSTANTS.dtT4)
                //{
                    string name = "AIN0";
                    LJM.eReadName(handle, name, ref voltage);
                //CountValue = voltage / 0.0048828125;
                CountValue = 750;
                //}
            }

    Here is my form1.cs

    private void AtgmButton_Click(object sender, EventArgs e)
            {
                AdjustLaser();
            }
            
            private void AdjustLaser()
            {
                //double intensity;
                double mycount = labjackT4.myCountValue;
                while (labjackT4.myCountValue < 2000)
                    
                labjackT4.ConvertFromDAC();
                if (labjackT4.myCountValue > 500)
                {
                    //intensity = 0.3;
                    //laser.setLdCurrSetpoint(intensity);
                    //Console.WriteLine("This button works for 3.3v!");
                }
                else
                {
                    //intensity = 0.2;
                    //laser.setLdCurrSetpoint(intensity);
                    //Console.WriteLine("This button works for 2.0!");
                }
                Console.WriteLine("This button works!");
                Console.ReadLine();
            }

    Tuesday, July 16, 2019 6:15 PM

All replies

  • In programming there are a couple of different ways to get data from one instance to another. The first option is to pass the data as a parameter. This is generally the preferred approach and allows you to define the instance wherever you want. In your case that makes the most sense.

    public class MyForm : Form
    {
       ..
    
       public LabJackT4 LabJack { get; set; }
    
       private void AdjustLaster ()
       {
           …
           while (LabJack.myCountValue < 2000)
           ...
       }
    }

    In this case I'm using a public property which makes senese in many cases. Note however that the property could be changed by calling code so you should always verify the property is set before using it.

    var form = new MyForm();
    form.LabJackT4 = ...;
    form.ShowDialog();

    If you don't want the instance to be changed after creation then you would normally pass it as part of the constructor. In the case of a Form this is a little trickier since the designer must be able to create an instance of the form so you would need multiple constructors.

    public class MyForm : Form
    {
       //Generated when creating the form, called by the designer
       public MyForm ()
       {
          InitializeComponents();
       }
    
       //This constructor is used by calling code
       public MyForm ( LabJackT4 labJack ) : this()
       {
          _labJack = labJack;
       }
    
       private void AdjustLaser ()
       {
          while (_labJack.myCountValue < 2000)...
       }
    
       private readonly LabJackT4 _labJack;
    }

    In this approach you're just storing the value in a field. Since the field is initialize when the form is created you don't have to worry about it changing. But calling code may not use this constructor so you still need to handle the fact it may not be set.

    For values you need only inside a single method then passing the data as a parameter is the best option.

    public class MyForm : Form
    {
       //This is callable outside the form
       public void AdjustLaser ( LabJackT4 labJack )
       {
          while (labJack.myCounterValue < 2000)
              ...
       }
    }


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, July 16, 2019 7:56 PM
    Moderator
  • Hi skular,

    Thank you for posting here.

    Based on your description, you want to achieve the Mycount value in the button_click event.

    I simplified the code, you could have a look.

      public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Button1_Click(object sender, EventArgs e)
            {
                AdjustLaser();
            }
            private void AdjustLaser()
            {
                LabJackT4 lj = new LabJackT4();
                lj.ConvertFromDAC();
                Console.WriteLine(lj.MyCount);
            }
        }
        public class LabJackT4
        {
            #region 
            private double myCountValue = 0;
    
            #endregion
            public double MyCount
            {
                get { return myCountValue; }
                protected set { myCountValue = value; }
            }
            public void ConvertFromDAC()
            {
               MyCount= 750;
            }
        }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 17, 2019 3:26 AM
    Moderator