free geoip Jayson's Blog - jaysonKnight.com
jaysonKnight.com
A conduit to the voices inside my head.

Jayson's Blog

  • Ramblings...

    how are we american programmers getting displaced by thousands by foreigners? if they program in VB, they almost have to learn the entire english language just to use it! ok, that was a cheap shot...but still...

    come on america...step it up! where else can you not work for pennies a day living in a shanty and be grateful for every second of it!

    whatever
    j
  • T-SQL nightmares...

    So, i've been given the task of scrubbing some data for a group at my work. apparently they have about 50 records w/ either invalid zip codes or invalid SSN's. writing regular expressions in C# is a snap, however i have to load this data into an excel spreadsheet, so it would be a lot easier to try and validate the data on the server with T-SQL. have you ever tried to do string parsing with T-SQL? there is absolutely no way to check and see if it's going to work until you hit the little green play button in SQLQA...

    this is gonna be a fun one...
    j
  • Follow up for a previous post...

    I had previously posted a blog on 10/28 about how to get a class library to dynamically find it's own config file...then i realized that i didn't give any instructions on what to do next as far as loading that config file into some sort of object so that your application can use it!

    Personally, i would load the configuration file into a singleton object. per msdn documentation, the C# code for this would be:

    class Singleton
    {
    public static Singleton Instance() {
    if (_instance == null) {
    lock (typeof(Singleton)) {
    if (_instance == null) {
    _instance = new Singleton();
    }
    }
    }
    return _instance;
    }
    protected Singleton() {}
    private static volatile Singleton _instance = null;
    }

    i personally find this a bit wordy and complex for what we are really trying to accomplish here. we need one object to share common properties with any subscribing assemblies, for the most part this information isn't going to change, so why waste round trips instantiating a new object each time your assembly needs a property! then i remembered that static types are guaranteed to be type safe, so why go through all that tedious locking code when you can simply do it all in one line, like this:

    public sealed class AppConfiguration
    {
    public static readonly AppConfiguration GetConfig = new AppConfiguration();

    ...

    voila! it's as easy as that, you now have a shared instance of your object. bear in mind that all member vars and methods/props will need to be static as well. so now it's time to load our configuration settings. there are numerous ways to parse through an XML document, but again for simplicity (and avoiding looping through the document and storing values in arrays of strings), XPath queries seem to be the easiest way to query for your settings. We go on in our code to creat a *private* constructor (so no one can instantiate your singleton), this will only get called once for the lifetime of the object...

    private AppConfiguration()
    {
    ... [code to load the config file into memory, see my previous post on the 28th...

    XmlDocument getConfig = new XmlDocument();

    try
    {
    getConfig.Load(_configFileName);
    _someMemberVar = getConfig.SelectSingleNode(
    "configuration/appSettings/add[@key='ValueYouWantToLoad']/@value").Value;

    ...repeat for all elements
    }

    now simply expose the member vars as readonly properties, now all calling code can easily read configuration settings.

    more to come, it's 6:05PM and i am still at work :-( friggin' long running queries keeping me here late again.

    cheerio
    j
  • Bedtime thoughts...

    if you are going to concat more than 2 strings, consider using a StringBuilder object...or just use the code below to obtain a nicely formatted string:

    public static string BuildStrings(params string[] stringsToBuild)
    {
    string delimiter = " ";

    // this will be the return string
    string builtString;

    // use a StringBuilder as the internal mechanism to build the string
    StringBuilder localBuilder = new StringBuilder();

    // loop through each string passed in and add it to the string to build
    foreach(string stringToBuild in stringsToBuild)
    {
    localBuilder.Append(delimiter);
    localBuilder.Append(stringToBuild);
    }
    builtString = localBuilder.ToString();
    return builtString;
    }
  • Tip of the day...

    VB.NET supplies the "Optional" keyword for parameters in methods, if none is supplied you can explicitely define a default value in the parameter signature. C# doesn't supply this functionality (and for good reason, it leads to lazy coding). it's good coding practice to always check the values of parameters in your components, and either throw an assertion or an exception if it's not supplied, like so:

    void myMethod(type myType1, type myType2...)
    {
    // check here for params that cannot be null, or "" for strings
    if(myType == null) throw new ArgumentNullException([message], [inner exception]...);
    // now check for validity of the param using regex, etc, you can't count on client code doing these checks
    if(myType != [some sort of criteria]) throw new ArgumentException ([message], [inner exception]...);
    }

    that being said, if you wanted to provide a default value, you can use ternary syntax to provide one

    void myMethod(type myType1, type myType2...)
    {
    myType1 != null ? MyType1 : [your default value here];
    }

    of course if you want to allow null values, you won't need to test for param values. if you follow these guidelines, you will insure that your components get used in the correct way.

    Tip number 2:

    ALWAYS ALWAYS ALWAYS call either string.Trim().ToLower() or string.Trim().ToUpper if you are using/comparing string params in C#...i spent the better part of 2 hours trying to track down a subtle bug, and it turned out to be that i was comparing strings that were spelled the same, however their ASCII values were different. VB.NET of course doesn't care about case, but it's good coding style nonetheless.

    happy coding :-)
    j

  • Quote of the day...

    The less buttons you give end users, the less shit that can go wrong. it's directly proportionate...sometimes i feel like my company should hire a herd of monkeys, they'd probably fuck up less.

    j
  • What a day

    So, today i learned the hard way that .NET class library apps don't intrinsically support .config files. after about 4 hours of head scratching i came up w/ the following bit of code that will dynamically load a config file with the same name as the assembly loading it:

    internal void GetConfigurationFilePath()
    {
    DirectoryInfo currentDir = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + "\\");
    _pathToWorkingDirectory = currentDir.FullName;

    //loop through all assemblies currently loaded
    foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
    {
    string asmName = asm.CodeBase;
    int index = asm.CodeBase.LastIndexOf(@"/");
    string part = asmName.Substring(index + 1);
    //now compare that substring to the name of any config files found in the current directory
    foreach (FileInfo file in currentDir.GetFiles ("*.config"))
    {
    if (part.Trim().ToLower() + ".config" == file.Name.Trim().ToLower())
    {
    _configFileName = file.FullName;
    }
    }
    }
    }

    it's actually quite simple once you look at it. so my question to the engineers at ms is wtf??? class libraries should be able to have dynamic config files just like their big brother .EXE's. moral of the story? not even .NET is perfect :-)

    cheerio
    j
  • Welcome to my Weblog!!!

    i sure as hell wasn't going to pay for one, so i built (read: hijacked) my own. we'll see how it goes, i am pretty good at breaking shit.

    The purpose of this blog is for me to share just how little i have learned i know about coding and .NET technologies...each day i learn a little bit more...

    more to come!
    j

Copyright © :: JaysonKnight.com
External Content © :: Respective Authors

Terms of Service/Privacy Policy