-1

Trying to get the index position of a string in a List but it keeps returning zero(0) as the index integer variable.

Any ideas what is wrong? I have tried changing to public int and so on.

        public int GetLangPos(string cultureCode)
    {


        CsvRead csvRead = new CsvRead();


        int index = 0;
        foreach (string line in csvRead.headerNames)
        {
            if (line.StartsWith(cultureCode)) { return index; }
            index++;
        }
        return index;
    }

CsvRead class:

class CsvRead
{

    public List<CsvLine> _csvLines = new List<CsvLine>(); //stores the csv lines
    public List<string> headerNames = new List<string>(); //stores the headers of the csv file

    public void GetValue()
    {

        /*
        Key|Name|es-ES|fr-FR|ru-RU
        web_key_001|Message|Mensaje|Message|Сообщение //row0
        web_key_002|Close|Cerca|Fermer|Закрыть //row1
        web_key_003|Administration|Administración|Administration|Aдминистрация //row2
        web_key_004|Confirm|Confirmar|Confirmer|подтвердить //row3
        web_key_005|Success|Éxito|Succès|Yспех //row4
        */

        FileStream f = new FileStream(ConfigurationManager.AppSettings["TranslationsCsv"], FileMode.Open);
        StreamReader streamReader = new StreamReader(f);
        CsvConfiguration config = new CsvConfiguration();
        config.Delimiter = "|";
        CsvReader csvReader = new CsvReader(streamReader, config);


        using (csvReader)
        {
            while (csvReader.Read())
            {
                headerNames = csvReader.FieldHeaders.ToList<string>();



                CsvLine csvLine = new CsvLine();

                for (int i = 0; i < headerNames.Count(); i++)
                {
                    csvLine.fieldValues.Add(csvReader.GetField<string>(headerNames.ElementAt(i)));
                }

                _csvLines.Add(csvLine);


            }


            //test section start



            //test section end

        }
    }
}
  • csvRead has any headerNames in it? It looks like you aren't reading anything there. – PseudoAj May 15 '16 at 17:09
  • please add simple data better understanding – reza.cse08 May 15 '16 at 17:10
  • headerNames just contains 5 elements. And the cultureCode variable is always present somewhere in the list of headerNames. –  May 15 '16 at 17:11
  • Your code looks good, index is 0 either there are no headerNames in cvsRead or the first line starts with cultureCode – Roman May 15 '16 at 17:11
  • Please provide a [mcve]. Without anything concrete, this is just a guessing game. – Charles Mager May 15 '16 at 17:12
  • There is definitely headerNames inside csvRead and elements inside the list too. Just test printed the elements at a good point in the program with a breakpoint and it's full of cultureCodes. :( –  May 15 '16 at 17:14
  • Added the CsvRead class :) –  May 15 '16 at 17:15
  • Are they in the same case? the cultureinfo and the headers? – Oogway May 15 '16 at 17:24
  • different classes. :) –  May 15 '16 at 17:25
  • 1
    It looks an awful lot like you're just creating a new `CsvRead` instance and never calling `GetValue`, so I'd expect `headerNames` to be empty. You need to step through this with a debugger. – Charles Mager May 15 '16 at 17:26
  • I did not mean classes. Are they in upper case or lower case? You are not using string.startswith with ignorecase set to true. – Oogway May 15 '16 at 17:27
  • They are a mix of upper and lower case in the cultureCode. –  May 15 '16 at 17:28
  • GetValue is called before that method is called in the main :) –  May 15 '16 at 17:29
  • and the header is also in the same mixed case? just trying to rule out a simple case mismatch. – Oogway May 15 '16 at 17:30
  • 2
    @TingAli you create a new instance, you then read the `headerNames`. You aren't calling `GetValue` between them, so `headerNames` will be empty. The fact you might have another instance you call `GetValue` on isn't relevant, *this* instance won't have *that* instance's state. Stick a break point before your loop and look at it. – Charles Mager May 15 '16 at 17:31
  • @CharlesMager You're a genius! Thank you!! I didn't realise that I had to call the GetValue again due to the new instance! Post a answer with that comment and I'll give you the answer thing on here! Cheers mate! :) –  May 15 '16 at 17:37
  • Don't ask people to find error of your application. https://support.microsoft.com/en-us/kb/815788 – mohsen May 15 '16 at 17:39
  • @TingAli ideally, you shouldn't do this. You've already read the file once, you should be using the same instance of `CsvRead` to find this column rather than reading it a second time. – Charles Mager May 15 '16 at 17:40

2 Answers2

0

there are 2 possible reasons:

  1. you are not filling headerNames
  2. you are not creating or initializing the headerNames list

Please post all the code to check the CsvRead internals

---- Edit ----

you have to take the line:

headerNames = csvReader.FieldHeaders.ToList<string>();

out of the while loop, that is reading the headers in all iterations and the line at the end of file is empty.

csvReader.Read(); // read the headers
headerNames = csvReader.FieldHeaders.ToList<string>();
while (csvReader.Read())
{
...
}
acamro
  • 97
  • 4
0

The issue seems to be a misunderstanding in how object instances work.

When you create an instance of a class using the new operator, you create a new instance of that object. The values of that object's instance fields (those without the static modifier) will belong to that instance only.

If you've called GetValue on some instance in Main, as you suggest, then the 'new' instance you create at GetLangPos will not share any of the state of this other instance.

As you've already read the file, you should probably re-use the results of that by re-using that instance rather than creating a new one in GetLangPos and reading the file again.

Charles Mager
  • 25,735
  • 2
  • 35
  • 45