3 Replies - 5742 Views - Last Post: 12 May 2009 - 06:47 AM Rate Topic: -----

#1 papuccino1   User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Modifying a string from a List<string> during a foreach loop.

Post icon  Posted 11 May 2009 - 07:18 PM

This is a console application.

Here's my method:

public static void ModificarDatos()
        {
            List<string> MyList = new List<string>(Persona.LeerArchivo());
            LeerDatos();

            Console.WriteLine("Para modificar el dato de una persona escriba su carnet de identidad, y se jalara su record de datos.");
            string SelectedCarnet = Console.ReadLine();

            foreach (string entry in MyList)
            {
                //DataArray[2] will always hold my carnet value, just to clarify.
                string[] DataArray = entry.Split(';');

                if (DataArray[2] == SelectedCarnet) //<---Here I have verified this is indeed where I want to modify the line in my text file. How can I achieve this.
                {
                    Console.WriteLine(entry);
                }
            }
        }


Is there a way for me to use a numerical value to "get" the position of the verified line and replace it with a line of my own inside a List<string>?

Thanks bros.

Edit: Let me clarify a bit, I want to get the index position of "entry" inside my for each loop only if the condition of == is met, and then access it outside my foreach loop to replace the string with one made by the user.

Pseudo code time!

cycle through each string X in my list
if the carnet typed by the user is the same as the .carnet of X
get index value of string X from list


outside loop, mylist.update(X, "text goes here");



Danke mon friendz-


Edit yet again! Maybe I could make a counter that increments with every passing of the foreach? And when I find the == stop counting and copy that int value? We shall see! Wish me luck, Eclipsed, I'll still wait for your answer. :pirate:

This post has been edited by papuccino1: 11 May 2009 - 07:39 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Modifying a string from a List<string> during a foreach loop.

#2 papuccino1   User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: Modifying a string from a List<string> during a foreach loop.

Posted 11 May 2009 - 07:48 PM

Back with some HORRIBLE NEWS. I divided by zero. Naw, just kidding. Here's my new method, however I seem to be mucking up the .insert method.

foreach (string entry in MyList)
            {
                string[] DataArray = entry.Split(';');
                i++; //Just adding a counter to get the index of the aquired target.
                if (DataArray[2] == SelectedCarnet)
                {
                    Console.WriteLine(entry);
                    Console.WriteLine("Registro encontrado. Escriba nuevos datos y presione Enter.");
                    string UpdatedData = Console.ReadLine();
                    MyList.Insert(i, UpdatedData);//<---Here's where I'm having problems. it doesn't seem to update my list properly.
                    break;
                }
            }


Thank you bros. :pirate:


Edit: Bah! I'm such a damn dolt! This, kids, is why you should program when your on a 18 hour sleep hiatus.

My method is only updating my list, not my actual text file. lolz!

This post has been edited by papuccino1: 11 May 2009 - 07:49 PM

Was This Post Helpful? 0
  • +
  • -

#3 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1533
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: Modifying a string from a List<string> during a foreach loop.

Posted 12 May 2009 - 03:39 AM

try using a for loop instead of a foreach loop, and you will have your index.

also, you can't write to a specific line in a text file. You will have to read in each line from the file, keep track of the line number, then insert/edit/delete when you get to the line number.

This post has been edited by eclipsed4utoo: 12 May 2009 - 04:04 AM

Was This Post Helpful? 0
  • +
  • -

#4 papuccino1   User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: Modifying a string from a List<string> during a foreach loop.

Posted 12 May 2009 - 06:47 AM

This problem is solved. Thank you Eclipsed for trying to help. :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1