Archives

Archives / 2004 / June
  • Passing bitwise OR enum values

    Everyone who has ever used a Regex knows this form of parameter:

    1Regex r = new Regex("", RegexOptions.Singleline | RegexOptions.IgnoreCase);
    I often wondered how to do that as well and because nobody ever taught me that at school, I had to figure it out myself. And today I did, and I'm sharing :)

    This is very usefull if you have a class that takes a lot of options and you don't want to add a bool for each possible option.

    First you need to create an enum with the possible options, and number them binary. (eg: 1, 2, 4, 8, 16, ...)
    1  public enum Pars {
    
    2 One = 1,
    3 Two = 2,
    4 Three = 4,
    5 Four = 8,
    6 Five = 16,
    7 Six = 32,
    8 Seven = 64
    9 } /* Pars */
    Next you need to have a method which takes this enum as a parameter, after which you can extract each of the possible options from it with a bitwise AND:
    1  private static void TestPars(Pars options) {
    
    2 if ((options & Pars.One) == Pars.One) { Console.WriteLine("One"); }
    3 if ((options & Pars.Two) == Pars.Two) { Console.WriteLine("Two"); }
    4 if ((options & Pars.Three) == Pars.Three) { Console.WriteLine("Three"); }
    5 if ((options & Pars.Four) == Pars.Four) { Console.WriteLine("Four"); }
    6 if ((options & Pars.Five) == Pars.Five) { Console.WriteLine("Five"); }
    7 if ((options & Pars.Six) == Pars.Six) { Console.WriteLine("Six"); }
    8 if ((options & Pars.Seven) == Pars.Seven) { Console.WriteLine("Seven"); }
    9 } /* TestPars */
    When all this is done, you call the method, passing the options you want with a bitwise OR between them, like this:
    1  static void Main(string[] args) {
    
    2 TestPars(Pars.Three | Pars.Five | Pars.Seven);
    3 }
    This example will print Three, Five and Seven.

    How does it work internally?

    Here are the rules for the bitwise operators:

    Bitwise OR:
    0101 (expression1)
    1100 (expression2)
    ----
    1101 (result)

    Bitwise AND:
    0101 (expression1)
    1100 (expression2)
    ----
    0100 (result)

    Now, our enum has the following binary values:

    1 - 0001
    2 - 0010
    3 - 0100
    4 - 1000
    ...

    If for example we pass the options One and Two along, we combine them with the bitwise OR, creating 0011.

    And if we want to check if Four was passed along, in our if we check if the options combined bitwise with Four result in Four.

    0011
    0100
    ----
    0000

    Four was not passed.

    If we check with Two we get the following:

    0011
    0010
    ----
    0010

    And 0010 = Two, our if = true, and Two was passed along.

    A lot of you will say "well duh, that's basics". Could be true, but I never learnt it at school, and never ever had the need for it, but now I wanted to know, and I guess there are more people out there who haven't learnt it either.

  • Troubleshooting DotNetNuke 2.1.2 Installation

    I had a lot off trouble installing DotNetNuke. This popular portal application kept me busy for an entire afternoon and night.

    First I got ASP.NET errors about columns missing in tables, then about stored procedures. When those were gone, the application itself managed to hang IE.

    So, for everyone else who might be suffering from this, or will give it a try and encounters the same, here's my installation guide for 2.1.2:

    The SQL errors occured when using SQL Server as Data Provider, the IE errors always occur.

    This step is for everyone using SQL Server:

    • Download DotNetNuke_2.0.4.zip.
    • Extract this, follow the normal installation procedure, this version doesn't give any problems.
    • Open the site so that it creates the tables and stored procedures.
    • Download DotNetNuke_2.1.2.zip.
    • Delete all files from 2.0.4 but keep your SQL Server tables.
    • Extract 2.1.2 in the same location.
    • Open the site, now you got passed the missing columns errors!
    This step is for everyone:
    • Download DotNetNuke_2.1.1.zip.
    • Extract this to a temporary location and get the file spmenu.js from controls\SolpartMenu.
    • Copy this file to 2.1.2 overwriting it.
    • You now fixed the IE errors!
    And at this point you got a working DotNetNuke 2.1.2 version, which you can begin skinning etc.

    Let's hope future versions don't require so much hasstle.

  • Won at the DigiKids 2004 Awards!

    I'm happy! A site I created for someone has won a price at the DigiKids 2004 Awards.

    I heard there were 120 submissions, and the one I created ended up with the 10 best sites! This is a very nice extra as a student, now I can tell something I coded lasted between 120 others! :)

    The results are in Dutch, but if anyone is interested: Prijs van de Minister van Onderwijs - Communicatorprijs.

    Some extra info about the site: Connectedly

  • Unable to launch notepad from 'Run' or view HTML source

    Today I experienced something weird.
    Normally I run a lot of files just from 'Run' (Win+R), but today I did 'Run', 'notepad', and nothing happened...

    This was weird.. As I could still open .txt files with Notepad.
    I could not run notepad from 'Run'! I was amazed, I mean,.. what could break notepad? :)

    First I checked if the file was still there (ofcourse, how could I still view files otherwise..), and if my PATH variable still included Windows. Everything seemed ok.

    But then I found it, it only seemed as I could not run notepad.

    What happened was the following: There was a notepad.com file in the system32 dir all of the sudden (spyware, virus, don't know). And Windows decided to run that one instead of notepad.exe

    So it seems Windows prefers .com over .exe when trying to run a file without an extension.

    Why does this happen?

    There's another environment variable, called PATHEXT, with the following data:

    PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH

    Now you can clearly see the order of the files. I tried the same thing with a .bat and .exe file, and changing the env var, and it worked.

    I'm not sure if the .COM has to be the first in line for normal system operations?

    One side-effect of this was that when I wanted to View Source of a HTML page, it would also launch the notepad.com which did nothing, and thus not showing any source! I remember a lot of people complaining about not being able to view the source of their pages, this could be what's causing it!

    Hopefully I'm telling something 'new' here, that's usefull ;) I never knew that order of extensions.

  • It's just an act of God, nothing special...

    Sometimes you wonder if there is someone who reads the End User License Agreement...

    If found this on the McAfee Security website:

    YOU HEREBY ACKNOWLEDGE THAT THE SOFTWARE MAY NOT BE AVAILABLE OR BECOME UNAVAILABLE DUE TO ANY NUMBER OF FACTORS INCLUDING, WITHOUT LIMITATION, PERIODIC SYSTEM MAINTENANCE, SCHEDULED OR UNSCHEDULED, ACTS OF GOD, TECHNICAL FAILURE OF THE SOFTWARE, TELECOMMUNICATIONS INFRASTRUCTURE, OR DELAY OR DISRUPTION ATTRIBUTABLE TO VIRUSES, DENIAL OF SERVICE ATTACKS, INCREASED OR FLUCTUATING DEMAND, ACTIONS AND OMISSIONS OF THIRD PARTIES, OR ANY OTHER CAUSE REASONABLY BEYOND THE CONTROL OF THE COMPANY.

    Since when do we include acts of God in an End User License Agreement?