Maybe I asked this is a bad way so let me try to explain.
I have a public static string[]
public static string[] MyInfo {get;set;}
I set this via
MyInfo = File.ReadAllLines(@"C:\myfile.txt");
What I want to do is a foreach on each of those lines and modify it based on x number of rules. One the line has been modified change that exisiting line in MyInFo
so
foreach(string myI in MyInfo)
{
string modifiedLine = formatLineHere(myI);
//return this to MyInfo somehow.
}
Glory Raj
17.7k28 gold badges115 silver badges216 bronze badges
asked Dec 11, 2011 at 14:18
user222427
4 Answers 4
You can't do this in a foreach loop. Use for instead.
answered Dec 11, 2011 at 14:20
Fyodor Soikin
81.4k10 gold badges134 silver badges179 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Use a for:
for(int i = 0; i < MyInfo.Length; i++)
{
MyInfo[i] = formatLineHere(MyInfo[i]);
}
answered Dec 11, 2011 at 14:20
Tudor
62.6k13 gold badges105 silver badges148 bronze badges
Comments
var myInfoList = new List<string>();
foreach (var myI in MyInfo)
{
var modifiedLine = formatLineHere(myI);
myInfoList.Add(modifiedLine);
}
MyInfo = myInfoList.ToArray();
Update
You will need a reference to System.Linq in order to use the ToList() and ToArray() extension methods.
answered Dec 11, 2011 at 14:22
danludwig
47.4k27 gold badges164 silver badges240 bronze badges
Comments
Or with Linq you could do:
MyInfo = MyInfo.Select(x => formatLineHere(x)).ToArray();
answered Dec 11, 2011 at 14:29
Russell Troywest
8,7583 gold badges37 silver badges40 bronze badges
Comments
lang-cs