troubleshooting Question

c# holding a varable after being defined in a block code and passing to another block

Avatar of sydneyguy
sydneyguyFlag for Australia asked on
.NET ProgrammingEditors IDEsC#
5 Comments2 Solutions306 ViewsLast Modified:
have a small code snippet in c# just accessing a varable that has been set the time before but cannot see the varable at block level and cannot seem to set it and hold it.
so the first time through it will return a value of the new record created,

int recVal; // this is the varable that needs to be held and used again
            foreach (DataRow dr in dt.Rows)
            {
               
                // FROM HERE GO AND CYCLE THROUGH THE DOCUMENT AND LOOK FOR LIKE DATA
     
                fndtest = findtext(sqlstringstart, sqlstringFinished, textBox1.Text, dr["Tabletosaveto"].ToString(), dr["fieldtosaveto"].ToString());
                MessageBox.Show(dr["firstfield"].ToString());
               
                if (dr["firstfield"].ToString() == "Y")
                {
// FIRST TIME THROUGH THE SYSTEM FINDS THE NEW REC NUMBER
// AND WILL PLACE IT IN recVal after it has been cnverted to int this works but next time
// through the recVal is not defined as it moves from the block to function, have set the
// recVal out side the block any ideas on what i am doing wrong please

                  IDNumber = SaveField(sqlTabletosaveto, fieldtosaveto, fndtest,'Y',0);
                  recVal = Int32.Parse(IDNumber);

                }
                else{
// SECOND TIME THROUGH IT USES recVal TO FIND THE NEW RECORD AND UPDATE THE FIELD
                    IDNumber = SaveField(sqlTabletosaveto, fieldtosaveto, fndtest, 'N', recVal);

                }
            }
ASKER CERTIFIED SOLUTION
AndyAinscow
Freelance programmer / Consultant

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 2 Answers and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros