Question

i have to create a programme that will handle complaints i have to track how many times i complete the process, this means i will need a count that increases when i return to the first form in the process however the number always resets when the first form is opened

runNum is set to a public int this is the code i have for entering data ito the form automatically then increaseing the runmber before opening the next form so far:

        private void autoFill()
        {
            TitlePage titlepage = new TitlePage();

            if (titlepage.getRun() == true)
            {
                int priority = runNum % 4;

                if (priority == 0)
                         priority = 2;

                txtDate.Text = DateTime.Today.ToString("dd/MM/yyyy");
                txtRef.Text = (100 + runNum).ToString();
                txtPriority.Text = priority.ToString();
                txtCustName.Text = firstNames[runNum] + " " + secondNames[runNum];
                txtCustAddress.Text = (runNum + 5).ToString() + " " + Address1[runNum] + " " + Address2[(runNum % 7)];
                txtCustEmail.Text = firstNames[runNum] + secondNames[runNum] + "@" + ISP[(runNum % 10)] + "." + Suff[(runNum % 10)];
                txtCustTel.Text = TelNo[runNum];               

                //TBD- fill this in with actual data  

                rtbComplaint.Text = "Complaint type,                         Sevarity,                         soultion exsiting,                         employee who dealt with complaint,                           Date";

                rtbComplaint.Enabled = false;
                txtPriority.Enabled = false;
                txtDate.Enabled = false;
                txtRef.Enabled = false;
                txtCustName.Enabled = false;
                txtCustName.Enabled = false;
                txtCustEmail.Enabled = false;
                txtCustAddress.Enabled = false;
                txtCustTel.Enabled = false;


                if (runNum % 2 == 0)
                {
                    rdoNo.Checked = true;
       enter code here         }
                else
                {
                    rdoYes.Checked = true;
                }



                getTimer();
                runNum++;
            }
        }
Was it helpful?

Solution

A public int will always be reset in this case. You should declare a static int on your parent page if you want global access to the variable. But global variables are to be avoided. Rather try to save your iterations in a central point such as a database or file which you can retrieve the value from or even use a server cached object which you can update and get the value from again.

OTHER TIPS

i would go with a static variable but be aware that even static variables can be reset if the appdomain is restarted or the webpage class gets recompiled.

see Lifetime of ASP.NET Static Variable

edited: sorry i assumed this question was a for an asp.net application. is this not the case?

if its a normal windows form then you will need to store it in some server side, whether DB, shared file on a server or something similar

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top