We help IT Professionals succeed at work.
Get Started

question about C# local variables and loops

rmicone
rmicone asked
on
375 Views
Last Modified: 2013-12-17
I have a question about local variables and loops in C# vs2008, the problem is that I can't find a simple way to augment a variable value over 2 different loops.  I come from a Perl/PHP background so I am used to just using the 'local' and 'global' functions to make variables able to be passed within functions.  I am not using any functions or subroutines in this C# console app, so I'm wondering how to use the access modifiers to declare my variables properly so I can use a single variable to keep track of the count as it loops through to different for loops.  I've tried reading the MSDN docs, but it's not specific enough, or I'm missing something... so if anyone can tell me how to do this I'd appreciate it.

Hopefully the code snippet below is not too horrendous to look at... the crux of it is that I need to tally up both "i" and "x" from my for loops
int numLoopLineNum = 0; 
                    for (int i = 0; i <= (arrSKU.Length - 1); i++)
                    {
                        string lineNum = (i + 1).ToString();
                        string strOrderLineItem = String.Format("{0}{1,-39}", "040", lineNum);
                        Console.WriteLine("{0}", strOrderLineItem);
                        numLoopLineNum++;
                    } 
                    Console.WriteLine("current value of numLoopLineNum = {0}", numLoopLineNum); 
                    if (item["Opt SKU"] != null)
                    {
                        string[] arrSKU_OPT;
                        string[] arrQTY_OPT;
                        string[] arrORDERNAME_OPT;
                        arrSKU_OPT = item["Opt SKU"].ToString().Split('\n');
                        arrQTY_OPT = item["Opt QTY"].ToString().Split('\n');
                        arrORDERNAME_OPT = item["Name"].ToString().Split('.');
                        
                        for (int x = 0; x <= (arrSKU_OPT.Length - 1); x++)
                        {
                            string lineNum = (x + 1).ToString();
                            string strOrderLineItem = String.Format("{0}{1,-39}", "040", lineNum);
                            Console.WriteLine("{0}", strOrderLineItem);
                        }
                    }

Open in new window

Comment
Watch Question
Senior Software Engineer
CERTIFIED EXPERT
Top Expert 2009
Commented:
This problem has been solved!
Unlock 1 Answer and 6 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE