简体   繁体   中英

Syntax confusion in C#

So I'm looking at a project that calls a function I've never seen before, and I'm only beginning my familiarity with C# syntax. I've worked with Python and Javascript for some weeks now, and I'm closer to experienced with them, but this syntax still baffles me.

private static void GENERIC_FUNCTION_NAME(string[] args)
    {
        GENERIC_OBJECT_NAME.OTHER_GENERIC_FUNCTION_NAME();

        string path = args == null || args.Length == 0 ?
            @"C:\GENERIC\SYSTEM\PATH" :
            args[1];

        var GENERIC_VARIABLE_NAME = new GENERIC_OBJECT_NAME();
        GENERIC_VARIABLE_NAME.Open(path);
    }

I'm confused by the syntax of the parameters of this function.

You see, this function was written in a C# console application and has lots of C++ components in the project that do the work it's meant to trigger. But I've been told to build a GUI for it that achieves the same goal here. I'm not even sure what gets passed as args ... Because I don't see it anywhere in the script next to this code, which is the only function inside the Program.cs that was written when I got the files to work with.

Because of this, I can't make a button connect to this function. I don't know how to integrate object sender, EventArgs e so that the button can be connected (and if I do it directly, ie, " (object sender, EventArgs e, string[] args) ", I get an error. What am I doing wrong? And how might I integrate the function and make it button addressable?

EDIT: I've moved the questions regarding the ternary operator here for future readers' sanity. If you would like, please repost your input on this, as you guys have already helped me here on it.

i can help with the syntax issue.

string path is equal to either @"C:\\GENERIC\\SYSTEM\\PATH" OR (:) args[1]

to determine which one if args == null OR args.length == 0 then go with @"C:\\GENERIC\\SYSTEM\\PATH" if not then go with args[1] it is the same as the following

if(args == null || args.Length == 0)
{
    string path = @"C:\GENERIC\SYSTEM\PATH" ;
}
else{
    string path = args[1];
}

Here is some further reading http://msdn.microsoft.com/en-us/library/ty67wk28.aspx

I assume your method was called Main , and is by default the only method in Program.cs

This main method takes arguments when the program is executed, such as running through CMD with optional arguments such as app.exe -myArg , and is translated into a string array. See more on command line parameters .

If not, you can right click the method name, and View All References , or Find Call hierarchy to figure out what might be calling it.

For your second issue... What you are seeing is a special conditional operator, the ternary operator . (And here is a nice tutorial)

It is used like so:

condition ? first_expression : second_expression;

Basically if the statement is true, the first expression is executed, if not, the second is. Generally speaking it is a small shortcut for if/else blocks, and should be used for only small statements. Nesting the ternary operator is largely frowned upon.

So if args == null || args.Length == 0 args == null || args.Length == 0 Then path = @"C:\\GENERIC\\SYSTEM\\PATH" , if not, it equals args[1]

It is equivalent to your standard if block

string path;
if(args == null || args.Length == 0)
{
   path = @"C:\GENERIC\SYSTEM\PATH";
}
else
{
   path = args[1];
}

Now that that is taken care of, if you double click your button in the designer, it will take you (or generate) a method that is executed when the button is clicked. From there you can call this method with it's arguments (Which if it is main , I'm not sure why you would be doing this. But I'm not sure if I quite understand the issue)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM