CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Sep 2011
    Posts
    69

    Arrow How to use a string in a foreach outside of it?

    Hi, I have a foreach statement that parses data. I have a string in it but I want to use it outside of the foreach loop. How do I do it?

    Heres my code:

    Code:
       System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex("<input name=\"authenticity_token\" type=\"hidden\" value=\".*\" />");
                        MatchCollection matches = r.Matches(accountstatus);
                        foreach (Match itemcode in matches)
                        {
    
    
                          string autcode = UrlEncode((itemcode.Value.Split('\"').GetValue(5)));
                        }
    I want to use autcode string out of the foreach loop. Can you help?

  2. #2
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: How to use a string in a foreach outside of it?

    Code:
    string autcode;
    foreach ()
    {
        autcode = ...
    }
    
    // use autocode here
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  3. #3
    Join Date
    Sep 2011
    Posts
    69

    Re: How to use a string in a foreach outside of it?

    I tried that but this is the error Im getting: Use of unassigned local variable 'autcode'

  4. #4
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: How to use a string in a foreach outside of it?

    yep.. should be....

    string autocode = new string();
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  5. #5
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: How to use a string in a foreach outside of it?

    Can you post the code that errors out (and what goal your subroutine is solving)?

    The error usually occurs when you try to read the value of an variable you haven't given a value yet.

    Code:
    string foo; //Not yet assigned
    string bar = foo + "!; //Will error out because foo has no value
    This would be fine though:
    Code:
    string foo; //Not yet assigned
    foo = "Hooray"; //Give foo a value
    string bar = foo + "!; //Bar now has the value "Hooray!"
    Why are you trying to run a foreach loop and then use the value outside the loop? There ARE reasons for doing that (for example, if you wanted to compute a sum), but your example didn't' seem to make clear that that made sense.
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  6. #6
    Join Date
    Sep 2011
    Posts
    69

    Re: How to use a string in a foreach outside of it?

    Now I got: 'string' does not contain a constructor that takes 0 arguments

  7. #7
    Join Date
    Sep 2011
    Posts
    69

    Re: How to use a string in a foreach outside of it?

    Quote Originally Posted by BioPhysEngr View Post
    Can you post the code that errors out (and what goal your subroutine is solving)?

    The error usually occurs when you try to read the value of an variable you haven't given a value yet.

    Code:
    string foo; //Not yet assigned
    string bar = foo + "!; //Will error out because foo has no value
    This would be fine though:
    Code:
    string foo; //Not yet assigned
    foo = "Hooray"; //Give foo a value
    string bar = foo + "!; //Bar now has the value "Hooray!"
    Why are you trying to run a foreach loop and then use the value outside the loop? There ARE reasons for doing that (for example, if you wanted to compute a sum), but your example didn't' seem to make clear that that made sense.
    I am using the string autcode outside of the foreach code in this line in a webrequest:

    Pd.Append("&post_authenticity_token=" + autcode);

    The foreach loop is to parse the autcode string.

  8. #8
    Join Date
    Sep 2011
    Posts
    69

    Re: How to use a string in a foreach outside of it?

    Bump................

  9. #9
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: How to use a string in a foreach outside of it?

    Show the code you have now.
    Always use [code][/code] tags when posting code.

  10. #10
    Join Date
    Jun 2011
    Location
    .NET4.0 / VS 2010
    Posts
    70

    Re: How to use a string in a foreach outside of it?

    It all depends on how you plan on using the code and how long you plan to store the variable for. rliq's code should have worked with the details you have posted you just need to initialize the string when you create it.

    Small example of different scopes
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Variable_Levels
    {
        class Program
        {
            // Class level variable can be accessed anywhere within class scope
            static string classString = "String created at class level";
    
            static void Main(string[] args)
            {
                // Variables created in a method can only be used inside its scope unless
                // you pass the values to another method.
                string mainString = "String created in main method";
    
                for (int i = 0; i < 3; i++)
                {
                    // Variables created in a loop can only be used while the loop goes through its iterations
                    string loopString = "This is loop: " + i.ToString();
    
                    // Write out strings
                    Console.WriteLine(classString);
                    Console.WriteLine(mainString);
                    Console.WriteLine(loopString);
                    Console.WriteLine();
                }
                
                // Wait for user input
                Console.Write("Press any key to continue...");
                Console.ReadKey();
                Console.Clear();
    
                // loopString does not exist in this context since the loop is completed it is discarded
                Console.WriteLine(classString);
                Console.WriteLine(mainString);
                //Console.WriteLine(loopString); //<- Does not exist in current context
                Console.WriteLine();
    
                // Wait for user input
                Console.Write("Press any key to continue...");
                Console.ReadKey();
                Console.Clear();
    
                // Run example 1
                Example1();
    
                // Wait for user input
                Console.Write("Press any key to continue...");
                Console.ReadKey();
                Console.Clear();
    
                // Pass value of mainString to example2 so it can be used in the method
                Example2(mainString);
                
                // Wait for user input
                Console.Write("Press any key to continue...");
                Console.ReadKey();
                Console.Clear();
            }
    
            static void Example1()
            {
                Console.WriteLine("Example1:\n");
    
                // mainString does not exist in this context since it was created in the Main method and no
                // value has been passed down if you try to access it you will get an error 
                Console.WriteLine(classString);
                //Console.WriteLine(passedString); //<- Does not exist in current context
                //Console.WriteLine(loopString); //<- Does not exist in current context
                Console.WriteLine();
            }
    
            static void Example2(string passedString)
            {
                Console.WriteLine("Example2\n");
    
                // In this method you passed the value so you can use it here
                Console.WriteLine(classString);
                Console.WriteLine(passedString); //<- Same value as mainString but mainString does not exist in current context
                //Console.WriteLine(loopString); //<- Does not exist in current context
                Console.WriteLine();
            }
        }
    }

  11. #11
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: How to use a string in a foreach outside of it?

    Code:
    string autocode = string.Empty;
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  12. #12
    Join Date
    Sep 2011
    Posts
    69

    Re: How to use a string in a foreach outside of it?

    ^ Thanks, it seemed to work

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured