Regarding what to type in to the parameter of this function
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Regarding what to type in to the parameter of this function

  1. #1
    Join Date
    Jan 2011
    Posts
    1

    Unhappy Regarding what to type in to the parameter of this function

    Hello, I am just began working on the software development and have few questions about some function as I have not used C# for like a year. Here is my question, In the function at the bottom, I am just wondering what "([Optional, DefaultValue(Constants.SilentLoud.Loud)]" stands for. Is it kinda like a data type? and then, what should I type in to this parameter when I need to call this function? Could you please explain what it stands for and what it does?


    The function is:

    Public object Close([Optional, DefaultValue(Constants.SilentLoud.Loud)] Constants.SilentLoud silent)


    Thank you very much!

  2. #2
    Join Date
    Dec 2007
    Posts
    234

    Re: Regarding what to type in to the parameter of this function

    It isn't the data type. The data type is actually Constants.SilentLoud .... what's in the brackets actually says that the parameter is optional... and if you don't explicitly pass in a value, then use the Constants.SilentLoud.Loud value.

    as for what to pass it, you can pass nothing, or a valid Constants.SilentLoud value.

    -tg
    * I don't respond to private requests for help. It's not conducive to the general learning of others.-I also subscribe to all threads I participate, so there's no need to pm when there's an update.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help - how to remove eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to???
    * On Error Resume Next is error ignoring, not error handling(tm). * Use Offensive Programming, not Defensive Programming.
    "There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN
    MVP '06-'10

  3. #3
    Join Date
    Jan 2011
    Posts
    11

    Re: Regarding what to type in to the parameter of this function

    hhmmm...Didn't know optional values worked like that, or in that syntax anyways in C#. Can someone give an example of a code that will compile which uses optional param?

    As a rule of thumb, for me atleast, I try not to use optional params (I work with vb6 also and have seen it in a lot of procedures). If you feel like you need to pass/return value(s), in most cases, it shouldn't be an optional param but a required param.

  4. #4
    Join Date
    Jan 2010
    Posts
    1,102

    Re: Regarding what to type in to the parameter of this function

    Well, up to C# 4 the language design team was against optional parameters, advocating the use of method overloading instead, but then they changed their mind, probably somewhat influenced by the whining of all the people doing COM interop and having to pass Type.Missing a dozen of times for unused parameters.

    Now, "[Optional, DefaultValue(Constants.SilentLoud.Loud)]" are just 2 attributes combined (like the ones available in VB.NET), but it is somewhat unusual for a developer to write something like that, because there's an alternate syntax that's way more easier to read:

    public object Close(Constants.SilentLoud silent = Constants.SilentLoud.Loud)

    C# 4 now also supports named parameters (in Smaltalk fashion). All in all, it's not such a big deal, it just makes coding more convenient and/or more readable.

    For example:
    Code:
    public static string SayHi(string firstName, string lastName = string.Empty)
    {
        if (lastName == string.Empty)
            Console.WriteLine(Hello there, " + firstName + "!";
        else
            Console.WriteLine(Hello there, " + firstName + " " + lastName + "!";
    }
    
    // and when u call it in main()
    SayHi("Duke");
    
    // or
    SayHi("Duke", "Nukem");
    
    // or with named params
    SayHi(
        firstName: "Duke",
        lastName: "Nukem"    // optional
    );

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center