0

I'm trying to open a form from another form. This results in an ArgumentOutOfRangeException.

I think the error is here because I added this lists and my program stopped working:

private static List<string> proxies = ps.proxies;
private static int proxyCounter = 1;
private static string currentProxy = proxies[proxyCounter];
public int TokenTimes = 0;

the lists go to:

class proScanner
{
    public List<string> proxies = new List<string>();

    public void ScanProxies()
    {
        var fileText = File.ReadAllLines(@"settings\proxies.txt");
        foreach (var s in fileText) proxies.Add(s);
    }
}
Void
  • 45
  • 9
  • 1
    Can you please provide the call stack of the exception so we can see which functions we called. – KingOfArrows Apr 15 '20 at 09:44
  • You are trying to access empty List before you call ScanProxies() as its static. private static string currentProxy = proxies[proxyCounter]; – Sandris B Apr 15 '20 at 09:45
  • 2
    What's the value of `proxies.Count` when this happens? – 500 - Internal Server Error Apr 15 '20 at 09:45
  • It’s also worth noting that indexes are 0 based, so initializing `proxyCounter` with a value of `1` means you’re requesting the second record in your `proxies` collection. As such, you’re assuming that there are _at least_ two records—an assumption your `ArgumentOutOfRangeException` suggests is, in fact, not true. – Jeremy Caney Apr 15 '20 at 09:52

1 Answers1

0

Debugging can really help you track down your problem, view the values, and solve the problem.

I would guess that your problem is here:

proxyCounter = 1;
currentProxy = proxies[proxyCounter];

And the proxies has less than 2 values (Index starts at zero!).

But some of the reasons why you are having problems tracking down the errors is because you are doing many assignments in the class declaration

private static List<string> proxies = ps.proxies;
private static int proxyCounter = 1;
private static string currentProxy = proxies[proxyCounter];

You should only declare variables and set default values on the class level. Not assign them values from other variables, this should be done inside your class, inside the constructor for example.

You should also look to see if you really need to define all of these statically. I would suggest that you consider a different method.

jason.kaisersmith
  • 8,712
  • 3
  • 29
  • 51