• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 219
  • Last Modified:

Better Solutions

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:
                    break;
                case 1:
                    break;
                case 2:
                    break;
                case 3:
                    break;
                case 4:
                    break;
                case 5:
                    break;
                case 6:
                    break;
                case 7:
                    break;
}
 
//this is the problem: I have 24 error messages.  I would like to have more efficient code.

Open in new window

0
TroyCrowe
Asked:
TroyCrowe
  • 2
1 Solution
 
ReddgumCommented:
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: http://msdn.microsoft.com/en-us/library/s4ys34ea.aspx

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.

Cheers,

-Red
0
 
TroyCroweAuthor Commented:
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.
0
 
TroyCroweAuthor Commented:
Solution is a good solution.  Thanks
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now