-4

I honestly don't get what part is causing me the error. "System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'" is what I get but it shouldn't happen.

class Program
{
    static void Main(string[] args)
    {
        string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\firstrun\config.cfg";

        if (!File.Exists(path))
        {
            Console.WriteLine("First time running");
            File.Create(path);
        }

        nameMethod(path);

        Console.ReadKey();
    }

    static void nameMethod(string path)
    {
        var lines = File.ReadAllLines(path).ToList();

        var idk = lines.Where(x => x.ToLower().Contains("name:"));

        string[] s = idk.ToString().Split(':');
        Console.WriteLine(s[1]);
    }
}

Text from the .cfg: Name: Nala

gunr2171
  • 16,104
  • 25
  • 61
  • 88
Nala Cat
  • 3
  • 4
  • Put your code in the question itself, not as a link to some site. – mason Feb 08 '18 at 20:54
  • What is the value of `s`? I bet it doesn't have 2 or more elements. – gunr2171 Feb 08 '18 at 20:55
  • `Console.WriteLine(s[1]);` will give that error of length is <= 1. – 001 Feb 08 '18 at 20:55
  • 1
    What is the type of `idk`? Do you know? You shouldn't use an implicitly typed variable (`var`) if it's not immediately obvious what the type is. I highly suspect it's not what you think it is. Calling `.ToString()` is often a code smell. If you find yourself doing that, there's often a better way of doing whatever it is you're trying to do. – mason Feb 08 '18 at 20:55
  • 2
    Possible duplicate of [What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?](https://stackoverflow.com/questions/20940979/what-is-an-indexoutofrangeexception-argumentoutofrangeexception-and-how-do-i-f) – gunr2171 Feb 08 '18 at 20:55
  • 1
    @mason oh, I bet `idk` is an `IEnumerable<..>`, and if `ToString()` that you're going to get text _about_ the type, not the contents of the collection. – gunr2171 Feb 08 '18 at 20:57
  • @gunr2171 Exactly. Though of course, I bet the file is some structured data type such as JSON, and they're manually parsing it rather than using an existing parser. – mason Feb 08 '18 at 20:58
  • See https://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager(v=vs.110).aspx – Yuli Bonner Feb 08 '18 at 20:58
  • Have you tried using the debugger to see what the value of s[1] is? – Craig W. Feb 09 '18 at 01:01

1 Answers1

1

The problem is with your variable idk, it holds a IEnumerable<string>, and ToString() will just return its type name.

The following code will grab the first element from the collection, and print it:

var idk = lines.Where(x => x.ToLower().Contains("name:"));
var s = idk.First().Split(':');
Console.WriteLine(s[1]);

Also, as general advice: If you don't know what type a variable is (which the name highly suggest), you can hold your cursor on var in most IDE's (VS and VS Code, at least), and if you don't recognize the type name that pops up, chances are: it will not return what you want it to.


As others have pointed out, though. Don't do text-parsing on your own; it's REALLY hard to do right. Instead, decide on a format, with a reliable parser for C#, I'd suggest JSON, but XML, YML, INI, or any common annotated format will likely do a good job for you!

mausworks
  • 1,607
  • 10
  • 23
  • 1
    @Nala, you thank people by clicking the green checkmark at the upper-left of their answer. And his last paragraph is good advice. – Dour High Arch Feb 08 '18 at 21:04
  • @DourHighArch Ok thanks, which do you suggest? XML, JSON or INI? – Nala Cat Feb 08 '18 at 21:17
  • I know you asked Dour, @Nala, but this is one of those "it depends"-questions. These formats, *basically* do the same thing, but all have their unique pros and cons. Historically, XML has been favored (at least by Microsoft), but JSON has become just as (or even more) standard. I'd say, read a little about both, then decide. You cant... go wrong with either. – mausworks Feb 08 '18 at 21:27