Question How to add a value in a list inside another list that already exists?

MohaMB

New member
Joined
Jan 31, 2021
Messages
1
Programming Experience
Beginner
I have a bug and I don't know how to fix the bug is the following: I have a list, which in turn is of type <List <String>>, with the constructor what I do is save certain info, and what I have coded is what next:



C#:
List<List<string>> opciones = new List<List<string>>();
        public Opcion(){
        var opcionesC = File.ReadAllText(this.dir).Split(";R;").ToList();
        opcionesC.RemoveAll(item => item == "");
        for(int i=0;i<opcionesC.Count;i++){
            this.opciones.Add(new List<string>());
            string[] opc= opcionesC[i].Split("\n");
            foreach(var t in opc){
                if(t.IndexOf("Marc")!=-1){
                    this.opciones[i].Add(t);                     //HERE ITS WORK
                }
                if(t.IndexOf("Model")!=-1){
                    this.opciones[i].Add(t.Remove(0,5));                      //HERE IS THE PROBLEM, NOT ADDING
                }
                if(t.IndexOf("Tip")!=-1){
                    this.opciones[i].Add(t.Remove(0,3));                      //HERE IS THE PROBLEM, NOT ADDING//El problema es esto, no se quiere añadir.
                    break;
                }
            }
        }
    }
Then when doing an output I get this:

C#:
 Marca: Audi
    Marca: BMW
And what I need is an output like that:

C#:
Marca: Audi
    Modelo: A4
    Tipo: Turismo
    Marca: BMW
    Modelo: S3
    Tip:...
Someone could tell me where the bug is, Thanks!
 

Attachments

  • Captura de pantalla 2021-01-31 171352.png
    Captura de pantalla 2021-01-31 171352.png
    45 KB · Views: 23
Last edited by a moderator:
Firs of all, it's rather silly to add a List<string> first and then get that back repeatedly. You should create the inner list and assign it to a variable, then populate it, then add it to the outer list as a final step.

As for the issue, the exception stack trace you attached seems to have little to do with the code you have posted. That stack trace indicates that you are calling Convert.ToInt32 on line 38 in the GetPunt method. Did you even look at that stack trace? You haven't told us where that line is and I don't see Convert.ToInt32 in your code anywhere. I suggest that you actually debug your code, i.e. set a breakpoint and then step through it line by line, confirming that all variables contain what you expect and and that execution follows the path that you expect. The exception you attached indicates that you're trying to convert a string to an int when it's not a valid representation of a number. You need to find out where that's happening and why and that's what the debugger is for.
 
The string Split calls is not right, single argument is for array of chars, first one it seems you want to split by a string: String.Split Method (System)
Second one can be split by a char, but you have to write the char literal '\n' instead of string literal "\n".
 
Back
Top Bottom