2

I'm creating a quick project which takes a .txt from the user, saves each line to an array and then makes a request for each ID, depending if the response contains 'The profile could not be found', or a valid profile, act accordingly.

Here's my code;

static void Main(string[] args)
    {

        string[] lines = System.IO.File.ReadAllLines(@"C:\\users\\louis\\Desktop\\d.txt");
        string[] avaliableIds = { };
        string errorMessage = "The specified profile could not be found.</h3><br><br>";

        foreach (string c in lines)
        {
            WebRequest req = WebRequest.Create("http://steamcommunity.com/id/" + c);

            WebResponse res = req.GetResponse();
            StreamReader sr = new StreamReader(res.GetResponseStream());

            string returne = sr.ReadToEnd();
            System.Threading.Thread.Sleep(100);

            if (returne.Contains(errorMessage))
            {
                int avaliableArrayLength = avaliableIds.Length;
                int shouldGo = avaliableArrayLength + 1;

                avaliableIds[shouldGo] = c;

            } // Here is where I'm getting the error
        }

        for(int i = 0; i == avaliableIds.Length; i++)
        {
            Console.WriteLine(avaliableIds[i]);
        }
    }

From what I understand, 'shouldGo' has a higher value than 'avaliableIds', so something is wrong with my declaration, it's supposed to be an array which can hold as high as or as low any number possible.

Please help, thanks.

levaa
  • 65
  • 10
  • you are trying to assign value to array item with array.Length + 1 index, how can it be possible? – yolo sora Dec 25 '17 at 12:58
  • I'm trying to add one to the length of that array, so when it add's it's self to the available ids, it's not going to overlap any other values already in there – levaa Dec 25 '17 at 12:59
  • you have so wonderfully self-explaining variable names. it should be obvious that **one more** than the **available** array length should **not** go. – René Vogt Dec 25 '17 at 13:00
  • you can't increase array size by simply adding 1 to another `int` – René Vogt Dec 25 '17 at 13:01
  • @JohnKap so array isn't kind of container you are looking for – yolo sora Dec 25 '17 at 13:01
  • What would you suggest me using? – levaa Dec 25 '17 at 13:04
  • @JohnKap as Heinzi mentioned - `List` should work fine here. You can simply add new item by using `Add` method. Better look at msdn documentation, this is very simple. – yolo sora Dec 25 '17 at 13:13

2 Answers2

1

In C#, arrays have a fixed size. You declare availableIds as an empty (zero-length) array, so you won't be able to store any data in it.

Use a List<string> instead, that's a variable-size data structure.

Heinzi
  • 167,459
  • 57
  • 363
  • 519
0

i == avaliableIds.Length
should be
i < avaliableIds.Length

Sander
  • 37
  • 5