Better Solutions

Posted on 2009-04-21
Last Modified: 2013-12-17
Hey everyone,

I am looking for a better way of accomplishing my task.  My thought process today is a little out of wack and I am getting frustrated so I am searching for answers.  I have created a custom error class.  When applying an error message, for example account not created, I am passing an int value through the query string.  The problem I am dealing with is how to keep the retrieving of the message value to display in a label's text without having to use a cumbersome switch statement.  I tried assigning the values to an enum, which I do not know enough about, but I have found that not to be a good way to handle it; at least it does not seem to be.  The code below is a smaller version of what I have going on.
public class Messages


    public static string ItemConfigNotFound = "Items configuration section could not be found";

    public static string ItemProviderInstantiationError = "Items default provider could not be instantiated";

    public static string CategoryIdUndefined = "Category 'Id' is not defined";

    public static string CategoryTitleUndefined = "Category 'Title' is not defined";

    public static string ItemIdUndefined = "Item 'id' not defined";

    public static string ItemTitleUndefined = "Item 'title' not defined";

    public static string ItemTitleIsNull = "Title must be a non null, non-empty string";

    public static string ItemRequiredAttributesMissing = "Some of the required attributes of Item are NULL in the Items table";

    public static string CategoryRequiredAttributesMissing = "Some of the required attributes of Category are NULL in the Category table";

    public static string NewsConfigSectionNotFound = "News configuration section could not be found";


//Error page receives a query string with an interger value named errMsg

if (Request.QueryString["Message"] != "")


                errMsg = Request.QueryString["Message"];


            switch (errMsg)


                case 0:


                case 1:


                case 2:


                case 3:


                case 4:


                case 5:


                case 6:


                case 7:



//this is the problem: I have 24 error messages.  I would like to have more efficient code.

Open in new window

Question by:TroyCrowe
    LVL 3

    Accepted Solution

    I think what you may find useful here is removal of the hard-coded strings and implementing an Dictionary array. You can find a fairly straightforward example and explanation (with sample code) on the MSDN site here:

    Using a key-value array makes your messages / errors et al somewhat more scalable. You can change them outside of the code using a table in a database or simply placed in a config file. Either way, using a Dictionary style array or a List<> will give you the ability to index to the appropriate message.

    A List<> is simpler (single-dimensional) but I'd use a Dictionary array instead, because that way the error codes don't have to follow a linear sequence. You just place the code in the INT and the appropriate text in the STRING and store it.



    Author Comment

    That sounds good.  I will check it out.  I am only a little familiar with the dictionary so implementing one will be something I will need to check out.  However, I do think you are correct.  I was trying to avoid changing something already implemented for I am appending to an existing application.

    Author Closing Comment

    Solution is a good solution.  Thanks

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now