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?
Re: How to use a string in a foreach outside of it?
Code:
string autcode;
foreach ()
{
autcode = ...
}
// use autocode here
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'
Re: How to use a string in a foreach outside of it?
yep.. should be....
string autocode = new string();
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.
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
Re: How to use a string in a foreach outside of it?
Quote:
Originally Posted by
BioPhysEngr
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.
Re: How to use a string in a foreach outside of it?
Re: How to use a string in a foreach outside of it?
Show the code you have now.
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();
}
}
}
Re: How to use a string in a foreach outside of it?
Code:
string autocode = string.Empty;
Re: How to use a string in a foreach outside of it?
^ Thanks, it seemed to work :)