Sometimes I leave presents for myself to find later:



  • Here's what I've just found while strolling an old source file:

    	Private Enum DE	'Days Enum
    Su = 1
    M = 2
    T = 3
    W = 4
    Th = 5
    F = 6
    Sa = 7
    End Enum

    ...
    
    Private Function DayString(ByVal Day As DE) As String
    	If Day = DE.F Then Return "Friday"
    	If Day = DE.M Then Return "Monday"
    	If Day = DE.Sa Then Return "Saturday"
    	If Day = DE.Su Then Return "Sunday"
    	If Day = DE.T Then Return "Tuesday"
    	If Day = DE.Th Then Return "Thursday"
    	If Day = DE.W Then Return "Wednesday"
    
    	Return "Christmas" 'LOOK IT'S SNOWING!
    End Function
    


  • I like your error handling.



  • Why are you checking those in alphabetical order?



  • Intellisense, why else?



  • Sadly, I was wondering why this was a WTF, then I realized that you could have simply had an array of strings that would do the same thing much more efficiently.  I guess I was thinking of the case I've seen in C++ where you need to go string -> integer and integer -> string.  Oh well.



  • I don't think that's why he posted it: he posted it because of the "Christmas" error handling...



  • Sadly, I was wondering why this was a WTF, then I realized that you could have simply had an array of strings that would do the same thing much more efficiently.  I guess I was thinking of the case I've seen in C++ where you need to go string -> integer and integer -> string.  Oh well.

    Yeah, no, that's stupid.  Using a string instead of an enum would have been majorly stupid.  It's the error handling.

    Edit: Now I get it, yeah, I could have had an array of strings, length 7 -- that would have worked also (I thought you had meant, passing in a string instead of an enum), but then I'd get array out of bounds exceptions when really, it's christmas.



  • @BrainSlugs83 said:

    Sadly, I was wondering why this was a WTF, then I realized that you could have simply had an array of strings that would do the same thing much more efficiently.  I guess I was thinking of the case I've seen in C++ where you need to go string -> integer and integer -> string.  Oh well.

    Yeah, no, that's stupid.  Using a string instead of an enum would have been majorly stupid.  It's the error handling.

    Edit: Now I get it, yeah, I could have had an array of strings, length 7 -- that would have worked also (I thought you had meant, passing in a string instead of an enum), but then I'd get array out of bounds exceptions when really, it's christmas.

     

    Yes, you see what I mean.  A character array of length 7.  Sometimes you do need a way to go from input string back to the integer/enum value.  Now that I relook at the code, though, do you not get a runtime error if you try to set an enum to a value not defined in the enum type? 



  • 		W = 4
    Th = 5
    F = 6
    W
        MAC 
    T
       STORE
    hehe. 
    <bF></bF>



  • const string[] DaysOfWeek = { "Monday", ... "Sunday", "Bugday" };

    const string[] Months = { "January", ... "December", "Faultember" };
     



  • @IMil said:

    const string[] DaysOfWeek = { "Monday", ... "Sunday", "Bugday" };

    const string[] Months = { "January", ... "December", "Faultember" }; 

    VB does not allow those evil constructs.

     



  • Explain why they are evil.



  • @dhromed said:

    Explain why they are evil.
    Because I forgot sarcasm doesn't work on the interblags.

    It's actualy very irritating that VB does not allow array initalization. 



  • @Daid said:

    @dhromed said:

    Explain why they are evil.
    Because I forgot sarcasm doesn't work on the interblags.

    It's actualy very irritating that VB does not allow array initalization. 

    Maybe you could "accidentally" create empty arrays then... which VB doesn't allow either... which is even more irritating... 



  • @Daid said:

    @dhromed said:

    Explain why they are evil.
    Because I forgot sarcasm doesn't work on the interblags

    When in doubt, go orange.



  • @vt_mruhlin said:

    Why are you checking those in alphabetical order?

    Yeah, since the days of the week are weighted towards the end of the alphabet, you should clearly optimize it by checking them in reverse alphabetical order.   

     



  • @Daid said:

    @IMil said:

    const string[] DaysOfWeek = { "Monday", ... "Sunday", "Bugday" };

    const string[] Months = { "January", ... "December", "Faultember" }; 

    VB does not allow those evil constructs.

    I almost didn't believe you until I utterly failed at doing this. Well, as constant values, which sucks. I suppose you could initialize static string arrays at runtime, though, which seems to work.



  • @PSWorx said:

    @Daid said:

    @dhromed said:

    Explain why they are evil.
    Because I forgot sarcasm doesn't work on the interblags.

    It's actualy very irritating that VB does not allow array initalization. 

    Maybe you could "accidentally" create empty arrays then... which VB doesn't allow either... which is even more irritating... 

    Public MySillyStringArray(0 to 6) AS String

    One empty array comming up. Oh, and you can make dynamic arrays, which most people also don't seem to know:

    Public MySillyDynamicArray() AS String

     
    And then somewhere:

    ReDim Preserve MySillyDynamicArray(0 to 124) AS String 



  • @Daid said:

    Public MySillyStringArray(0 to 6) AS String

    One empty array comming up. Oh, and you can make dynamic arrays, which most people also don't seem to know:

    Public MySillyDynamicArray() AS String

     
    And then somewhere:

    ReDim Preserve MySillyDynamicArray(0 to 124) AS String 

    I meant arrays that have zero elements, like String[] {} in sane languages.

    Public A() as String

    Redim A(0) 'Index out of bounds error

    Public B as Variant

    B = Array() 'Index out of bounds error 



  • @PSWorx said:

    @Daid said:

    Public MySillyStringArray(0 to 6) AS String

    One empty array comming up. Oh, and you can make dynamic arrays, which most people also don't seem to know:

    Public MySillyDynamicArray() AS String

     
    And then somewhere:

    ReDim Preserve MySillyDynamicArray(0 to 124) AS String 

    I meant arrays that have zero elements, like String[] {} in sane languages.

    Public A() as String

    Redim A(0) 'Index out of bounds error

    Public B as Variant

    B = Array() 'Index out of bounds error 

    Uh...using VB.NET or something? (Which is silly for me, as I get the problems from VB combined with learning the new .NET framework. VB.NET is not VB IMHO)

    This runs fine in VB6:

     

    Option Explicit

    Private A() As String
    Private X As Variant

    Private Sub Form_Load()
    ReDim A(0) 'Makes an array from 0 to 0, thus 1 element!
    A(0) = "X"

    X = Array()

    Debug.Print LBound(X), UBound(X)
    Debug.Print LBound(A), UBound(A)
    End Sub

    Outputs:
    0    -1
    0    0


    Looks fine to me (fine as fine can be within VB limits, because as always, the real WTF is VB)



  • @Daid said:

    @dhromed said:

    Explain why they are evil.
    Because I forgot sarcasm doesn't work on the interblags.

    It's actualy very irritating that VB does not allow array initalization. 

    VB or VB.net

    I know VB.net allow you to initialize an array.

     Dim arrayNames() As String = New String() {"Frank", "Stan"}



  • though, do you not get a runtime error if you try to set an enum to a value not defined in the enum type?
    You don't, at runtime they're just treated as integers.

    For the record:

    You CAN initialize arrays in VB.NET, and you can have zero length arrays too (Dim A(0) as String works! as does Redim A(0), and Redim Preserve A(0))...

     And you can box arrays too.<FONT color=#0000ff size=2>

    Dim</FONT><FONT size=2> A </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Object

    </FONT><FONT size=2>

    A = </FONT><FONT color=#0000ff size=2>New</FONT><FONT size=2> List(</FONT><FONT color=#0000ff size=2>Of</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>String</FONT><FONT size=2>)

    </FONT>

     



  • In C# you could do this...

    private enum Weekday
    {
    Sunday = 0,
    Monday = 1,
    Tuesday = 2,
    Wednesday = 3,
    Thursday = 4,
    Friday = 5,
    Saturday = 6
    }

    private string WeekdayToString(Weekday day)
    {
    return day.ToString();
    }

    private string WeekdayToInt(Weekday day)
    {
    return (int)day;
    }

    private Weekday StringToWeekday(string day)
    {
    return Enum.Parse(typeof(Weekday), day, false);
    }

    private Weekday IntToWeekday(int day)
    {
    return (Weekday)day;
    }


    The funny thing about enums though is it's all smoke and mirrors... Using the enum in the previous example, the following statements are true:

    ((Weekday)0).ToString() == "Sunday"
    ((Weekday)8).ToString() == "8"
    


    Neat! It's like magic!


  • If you want to localize, you could do this!

    private string ToLocalDay(Weekday day)
    {
    DateTime dt = new DateTime(2007, 8, 5); // any sunday will do
    dt = dt.AddDays((int)day);
    return dt.ToString("dddd");
    }

    I'm having way too much fun with this aren't I?



  • But what if the first day of the week is Monday, not Sunday? Like in, I don't know, the rest of the world?

    Your precious localization wont work in with my regional settings! Bah.



  • @Mr. Lurker said:

    But what if the first day of the week is Monday, not Sunday? Like in, I don't know, the rest of the world?

    Your precious localization wont work in with my regional settings! Bah.


    So it's not tuesday over there when it's tuesday over here?

    The localization will work (return the correct names), but the enum will be off by 1 ordinal... (Adds a third-world country delta int to add to my int conversions)



  • @IMil said:

    const string[] DaysOfWeek = { "Monday", ... "Sunday", "Bugday" };

    const string[] Months = { "January", ... "December", "Faultember" "Checkuary" };
     

    That should work better.  Helps it pass the Turing Test too.

     
     


     



  • Some presents I left for myself and just discovered:

    form#header_search * { /* take THAT you silly unformattable elements! PWN! */
       display:block;
       float:left;
    }

    And:

    <%
       //BAH BAH BAH
       //But the JS solution is worse.
       writeLeftNavSpacers();
    %>

    I expected more references to Smurf, though. 


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.