-
February 26th, 2012, 09:10 PM
#1
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?
-
February 26th, 2012, 09:15 PM
#2
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.......
-
February 26th, 2012, 09:18 PM
#3
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'
-
February 26th, 2012, 09:31 PM
#4
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.......
-
February 26th, 2012, 09:34 PM
#5
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.
-
February 26th, 2012, 09:37 PM
#6
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
-
February 26th, 2012, 09:42 PM
#7
Re: How to use a string in a foreach outside of it?
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.
-
February 27th, 2012, 10:15 AM
#8
Re: How to use a string in a foreach outside of it?
-
February 27th, 2012, 10:42 AM
#9
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.
-
February 27th, 2012, 04:03 PM
#10
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();
}
}
}
-
February 27th, 2012, 04:25 PM
#11
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.......
-
February 27th, 2012, 08:05 PM
#12
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|