Link to home
Start Free TrialLog in
Avatar of sydneyguy
sydneyguyFlag 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);

                }
            }
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 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      
Have you posted your complete code?
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