We help IT Professionals succeed at work.

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

303 Views
Last Modified: 2013-12-16
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);

                }
            }
Comment
Watch Question

Freelance programmer / Consultant
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
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      
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:
Have you posted your complete code?

Author

Commented:
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
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.