What are const's for?



  •  Doing some research on how to implement something and stumbled on this blog. [url]http://blah.winsmarts.com/2007-5-Programatically_create_a_SharePoint_site_based_on_a_site_definition.aspx[/url]

    The code is:

    public static bool CreateSite(
    string parentSiteURL, string siteURLRequested,
    string siteTitle, string siteTemplateName)
    {
    bool returnCondition = false; // Assume failure.

    const Int32 LOCALE_ID_ENGLISH = 1033;

    using (SPSite siteCollection = new SPSite(parentSiteURL))
    {
    SPWeb parentWeb = siteCollection.OpenWeb();
    SPWebTemplateCollection Templates =
    siteCollection.GetWebTemplates(
    Convert.ToUInt32(LOCALE_ID_ENGLISH));
    SPWebTemplate siteTemplate = Templates[siteTemplateName];
    if (parentWeb.Webs[siteURLRequested].Exists)
    {
    parentWeb.Webs.Delete(siteURLRequested);
    }

    parentWeb.Webs.Add(
    siteURLRequested,
    siteTitle,
    "",
    Convert.ToUInt32(LOCALE_ID_ENGLISH),
    siteTemplate,
    false, false);

    // All is good?
    returnCondition =
    true;
    }

    return returnCondition;
    }

    So what is the point of defining LOCALE_ID_ENGLISH to be an Int32?



  • You pick an article about instantiating SharePoint site definitions and you complain about a constant?

    SharePoint itself is TRWTF. Whoever designed it should be shot.



  • The const may be copied from elsewhere, and the proper datatype is Int32 (for that set of consts). Those functions may take UInt32, so that is the need for the conversion. If you're asking why the const is declared with a datatype, it could be just to catch mistakes when passing around values. I don't know if that is necessary in this language or not.

     

    I don't really see a wtf.



  • @movzx said:

    The const may be copied from elsewhere, and the proper datatype is Int32 (for that set of consts). Those functions may take UInt32, so that is the need for the conversion. If you're asking why the const is declared with a datatype, it could be just to catch mistakes when passing around values. I don't know if that is necessary in this language or not.

     

    I don't really see a wtf.

    I think this is C#, and the type is certainly not optional in const declarations.

    @C# spec said:

    10.4 Constants

    A constant is a class member that represents a constant value: a value that can be computed at compile-time. A constant-declaration introduces one or more constants of a given type.

    constant-declaration:
    attributesopt   constant-modifiersopt  
    const   type   constant-declarators   ;<o:p></o:p>

    The use of const tells the compiler that it can be optimised away at runtime, and allows it to check you don't accidentally do anything bad like pass it by reference to a function that might try to modify it.

    As for the UInt32 vs. Int32, that's a minor WTF.  TRWTF is copying and pasting the declaration rather than using the proper system header that defines it, but then if you did that (and assuming it's Int32 in the header) you'd still have the same type-signedness conflict.  I don't know if C# has default conversions like C, but if there's one thing worse than copying and pasting declarations around the place that should be shared, it's copying and pasting declarations around the place that should be shared and then modifying them a little bit differently each place you copied them to.




  • "what are const's for?"

    What are apostrophes for?



  • @DaveK said:

    As for the UInt32 vs. Int32, that's a minor WTF.

    I think what he meant as a WTF was the fact that the constant had been defined as Int32 when UInt32 would have been more appropriate. Also instead of Convert.ToUInt32() a simple cast would have been sufficient.

    @DaveK said:

    TRWTF is copying and pasting the declaration rather than using the proper system header that defines it, but then if you did that (and assuming it's Int32 in the header) you'd still have the same type-signedness conflict.  I don't know if C# has default conversions like C, but if there's one thing worse than copying and pasting declarations around the place that should be shared, it's copying and pasting declarations around the place that should be shared and then modifying them a little bit differently each place you copied them to.


    C# doesn't use header files and includes, so this number would have to come from some i18n classes; however since this number seems to be well known and probably will never change, it is more simpler to just write it where it is needed (the scope of the const is only inside the method it was declared in).

    Looking further into the subject, I discovered that you can get the locale ID (LCID) from the CultureInfo class:

    // note that it is a signed type, but it cannot be const anymore.
    int englishID = CultureInfo.GetCultultureInfo("en-US").LCID;

    // you can also get it by using the LCID:
    int alsoEnglishID = CultureInfo.GetCultultureInfo(1033).LCID; // which gives the same result as = 1033

    It seems like 1033 is linked to "en-US" at a fundamental level in .NET (windows?) and it seems that they are interchangeable in most aspects. Take a look at this table



  • @Flatline said:

    "what are const's for?"

    What are apostrophes for?

    Apostrophes are for grocer's!



  • @DaveK said:

    I think this is C#, and the type is certainly not optional in const declarations.

    @C# spec said:

    10.4 Constants

    A constant is a class member that represents a constant value: a value that can be computed at compile-time. A constant-declaration introduces one or more constants of a given type.

    constant-declaration:
    attributesopt   constant-modifiersopt  
    const   type   constant-declarators   ;

    In this example, however, it's not a class member, so you cited the wrong section.



  • @Spectre said:

    @DaveK said:

    I think this is C#, and the type is certainly not optional in const declarations.

    @C# spec said:

    10.4 Constants

    A constant is a class member that represents a constant value: a value that can be computed at compile-time. A constant-declaration introduces one or more constants of a given type.

    constant-declaration:
    attributesopt   constant-modifiersopt  
    const   type   constant-declarators   ;

    In this example, however, it's not a class member, so you cited the wrong section.

     

    ROFL.  The risks of late-night posting.

    @C# spec said:

    8.5.2 Local
    constant declarations

    A local-constant-declaration declares one or more local constants.

    local-constant-declaration:
    const   type   constant-declarators

    There ya go.



  • TRWTF is that the function never returns false. it starts out with returnCondition = false, but then changes it to true at some point and returns that value. There are no forks, no if/thens that cause it to return anything but true.

     



  • @DaveK said:

    Apostrophe's are for grocer's!

    There, FTFY



  • @SlyEcho said:

    @DaveK said:

    As for the UInt32 vs. Int32, that's a minor WTF.

    I think what he meant as a WTF was the fact that the constant had been defined as Int32 when UInt32 would have been more appropriate. Also instead of Convert.ToUInt32() a simple cast would have been sufficient.

     

     

    Well that is the point of the WTF. If the const was defined as a UInt, then no conversion/typecasting would be needed. The variable is used in three places in this function, 1 is the declaration, and the other 2 are in the function calls. 

     Yes I realize(or assume) that the functions expect a UInt, so why not create a const uint?



  •  @Flatline said:

    "what are const's for?"

    What are apostrophes for?

    An apostrophe is used by some writers to form a plural for abbreviations, acronyms, and symbols where adding just
    s rather than 's may leave things ambiguous or inelegant.

    Now is "consts" ambiguous? Probably not, as I don't think people will think I was referring to a new C# keyword "consts" instead of multiple instances "const".



  • @chrismcb said:

    So what is the point of defining LOCALE_ID_ENGLISH to be an Int32?

     

    What else would you like it to be?  A Double?  That would be a real WTF?



  • @chrismcb said:

     @Flatline said:

    "what are const's for?"

    What are apostrophes for?

    An apostrophe is used by some writers to form a plural for abbreviations, acronyms, and symbols where adding just
    s rather than 's may leave things ambiguous or inelegant.

    According to the very same page you failed at linking to: abbreviations yes, acronyms no, only plain (non-acronymic) initialisms.

    Thus, when discussing the Australian and American broadcasting companies, we can refer to "The two ABC's", but when strapping James Bond to a table and slicing him in half with multiple beams we do not refer to "The many laser's". 




  • @dwilliss said:

    @chrismcb said:

    So what is the point of defining LOCALE_ID_ENGLISH to be an Int32?

     

    What else would you like it to be?  A Double? 

    About time you got your round in!  Mine's a Glenmorangie, thanks!  Cheers!



  • @DaveK said:

    Thus, when discussing the Australian and American broadcasting companies, we can refer to "The two ABC's",
     

    Personally I feel that is still wrong and I would write "The two ABCs" as it is not ambiguous as "ABC" is in capitals and the "s" is not. I don't care if it is technically right or wrong :)

    I'm currently in "Surfers Paradise" which should (according to grammar rules) have an apostrophe as it is the surfer who has discovered "paradise". Place names shouldn't (can't?) have punctuation.


     



  • @Zemm said:

    @DaveK said:

    Thus, when discussing the Australian and American broadcasting companies, we can refer to "The two ABC's",
     

    Personally I feel that is still wrong and I would write "The two ABCs" as it is not ambiguous as "ABC" is in capitals and the "s" is not. I don't care if it is technically right or wrong :)

    Personally I'd do it that way too, and for the exact same reason (that's why I also always write decades as e.g. "the 90s"), but the point the WP article was making is that it's an accepted exception to the rule in this case which is an initialism, but not for acronyms.

    @Zemm said:

    I'm currently in "Surfers Paradise" which should (according to grammar rules) have an apostrophe as it is the surfer who has discovered "paradise".

    Possibly it is paradise for many surfers, in which case it would be ""Surfers' paradise".

    @Zemm said:

    Place names shouldn't (can't?) have punctuation.

    Heh, big debate about that recently over here in the UK (must have been a slow news day) when some local councils decided they wanted to drop apostrophes from similar place names that had traditionally always had them.

    I think you'll find they're very common in places named after e.g. saints or other prominent people.  Did you know that the famous Lord's Cricket Ground is located in the St. John's Wood area of London? 


Log in to reply