2

I have some code below which is a URL Parser and the if statement doesn't work instead of executing the code when it is true it executes all the code in all the if statements.

    public static class var
    { //Global variables
        public static string stre = System.Uri.UnescapeDataString(Environment.CommandLine);
        public static string[] str = stre.Remove(stre.Length - 1).Split(new string[] { "cmd:" }, StringSplitOptions.None);
    }
    public CMD()
    { //I suppose this class runs first so all my is statements are here
        AClsMsgBox.Show("Parsing...", "CMD Parser", 1000); //msgbox
        if (var.stre.Length > 5) { InitializeComponent();} //This executes even if I type "cmd:" which should trigger the statement below. (Always executes) This should run when typing "cmd:echo a command"
        else if (var.stre.Length == 4) { Process.Start("cmd.exe"); } //This should run when typing "cmd:"(Never execute)
        else { AClsMsgBox.Show("This is a URL Parser use it by typing 'cmd:<command>' in the URL adress bar field of a browsers.","CMD Parser",60000); } //Always executes
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        string stra = var.str[1].Replace("&", "&&");
        label1.Text += stra;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Process.Start("cmd.exe", " /k " + var.str[1]);
        this.Close();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        this.Close();
    }
    private void label1_Click(object sender, EventArgs e) { }
}

This is C# GUI so I am running InitializeComponent() only if I need to display the rest of buttons and stuff. (I think it should stop the GUI)

And there are no errors! But appear after building the code.

On running debugger:

System.IndexOutOfRangeException
HResult=0x80131508
Message=Index was outside the bounds of the array.
Source=CMD Parser GUI
StackTrace:
at CMD_Parser_GUI.CMD.Form1_Load(Object sender, EventArgs e) in D:\bat\CMD Parser GUI\Form1.cs:line 61
line 61 is "string stra = var.str[1].Replace("&", "&&");"
Enter Nam
  • 35
  • 6
User not found
  • 479
  • 3
  • 15
  • 1
    Have you debugged it to see what's really going on? – Nick.Mc Oct 01 '21 at 05:07
  • It says exeption Thrown Index was out of bounds of array and it highlights this line `string stra = var.str[1].Replace("&", "&&");` it looks fine to me. – User not found Oct 01 '21 at 05:12
  • 2
    This code is so convoluted it's perhaps unsurprising that there are bugs.. but I think it's reasonable to say in an argument over whether a string is longer than 5 chars where you say it isn't and c# behaves as if it is, that you're the one that is mistaken. What are you trying to do, exactly? – Caius Jard Oct 01 '21 at 05:31
  • So there's nothing wrong with your IF statement, it's actually an error in your code. So what does the variable `var` contain? Clearly there is something wrong with that code, most likely due to variable values – Nick.Mc Oct 01 '21 at 05:31
  • OT `var` is a reserved word. Please use a more descriptive name in the future – Hans Kesting Oct 01 '21 at 05:54
  • How many items does `var.str` contain? Apparently not more than 1. Note that the index of the first item is 0 – Hans Kesting Oct 01 '21 at 05:56
  • I'm seeing `if () {} if () {} else {}`. Did you intend to have an `else if` in the middle? – Jeremy Lakeman Oct 01 '21 at 06:01
  • var is a class that I've used to define global variables. – User not found Oct 01 '21 at 06:16
  • @JeremyLakeman I'm not to sure about that what's the difference. Well if the first `if` is true when the input is "cmd:echo command" then why does the else also activate? – User not found Oct 01 '21 at 06:19
  • 1
    Because the first if is unrelated to the others. The first if may run or it may not. But either the second if or the else will always run. – Jeremy Lakeman Oct 01 '21 at 06:23
  • Now it works better the `else` doesn't get triggered when input is "cmd:echo command" (by replacing 2nd if with else if). But if I type "cmd:" it doesn't trigger the 2nd `else if`. (Also replaced `var` with `gvar` just in case). – User not found Oct 01 '21 at 06:28
  • @CaiusJard I'm trying to make a URL parser so when I open the url "cmd:" it should give me a prompt asking me if i want to run the command, also if url "cmd:" is opened it should launch cmd.exe without a prompt. And if I click on the `exe` file it should advise me how to use it. (I've written it all in the if and else statements). – User not found Oct 01 '21 at 06:41
  • You mean you want to be able to type `cmd:format%20c%3a` into a browser address bar and destroy your computer? – Caius Jard Oct 01 '21 at 07:23
  • Like that but without destruction! Thats why I have a confirmation prompt. – User not found Oct 01 '21 at 07:25
  • Related: [What is an IndexOutOfRangeException](https://stackoverflow.com/questions/20940979/what-is-an-indexoutofrangeexception-argumentoutofrangeexception-and-how-do-i-f) – Hans Kesting Oct 01 '21 at 08:35
  • @HansKesting Yeah It will be throwing that error cause there are no parameters but if there are no parameters it should run the code in the `else` statement and should not run the rest of the code but it does and how do I stop that? Cause if there are no parameters `else` should be triggered if parameter is "cmd:" `else if` should be triggered if parameter is "cmd:" `if` should be triggered and then only the rest of code. – User not found Oct 01 '21 at 09:09
  • What *actually is* the value of `var.stre.Length`? It might start with the full pathname of the executable - way more than 5 chars. See [Example](https://learn.microsoft.com/en-us/dotnet/api/system.environment.commandline?view=net-5.0#examples) – Hans Kesting Oct 01 '21 at 11:25
  • @HansKesting Yes it does begin with the path thats. So thats giving the problem all along. – User not found Oct 01 '21 at 11:58
  • FYI the [Main method](https://learn.microsoft.com/en-US/dotnet/csharp/fundamentals/program-structure/main-command-line) (=startpoint of your application) might be a better place to intercept commandline arguments than some Form – Hans Kesting Oct 01 '21 at 12:10

0 Answers0