Avatar of sydneyguy
sydneyguy
Flag for Australia asked on

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

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);

                }
            }
.NET ProgrammingEditors IDEsC#

Avatar of undefined
Last Comment
sydneyguy

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
AndyAinscow

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
anugu

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
sydneyguy

ASKER
i am not gettig a chance to run ir single strp it just will not compile
"  Error      23      Use of unassigned local variable 'recVal'  "
just throws up an error, before i do any thing      
AndyAinscow

Have you posted your complete code?
sydneyguy

ASKER
no it was just the snippet but the major component, have gone back and re done the =varables and made sure that they were all adopting the right types as they went through and its compiling and running now thanks for you help
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes