views:

63

answers:

3

I'm wondering how others deal with trying to centralize MessageBox function calling. Instead of having long text embedded all over the place in code, in the past (non .net language), I would put system and application base "messagebox" type of messages into a database file which would be "burned" into the executable, much like a resource file in .Net. When a prompting condition would arise, I would just do call something like

MBAnswer = MyApplication.CallMsgBox( IDUserCantDoThat )

then check the MBAnswer upon return, such as a yes/no/cancel or whatever.

In the database table, I would have things like what the messagebox title would be, the buttons that would be shown, the actual message, a special flag that automatically tacked on a subsequent standard comment like "Please contact help desk if this happens.". The function would call the messagebox with all applicable settings and just return back the answer. The big benefits of this was, one location to have all the "context" of messages, and via constants, easier to read what message was going to be presented to the user.

Does anyone have a similar system in .Net to do a similar approach, or is this just a bad idea in the .Net environment.

+1  A: 

We used to handle centralized messages with Modules (VB). We had one module with all messages and we call that in our code. This was done so that we change the message in one place (due to business needs) and it gets reflected everywhere. And it was also easy to handle change in one file instead of multiple files to change the message. Also we opened up that file to Business Analysts (VSS) so that they can change it. I don't think it is a bad idea if it involves modules or static class but it might be a overkill to fetch it from DB.

HTH

Raja
This was the similar purpose, to centralize elements. The reason for in a DBF was I was developing with VFP for years, and it was EXTREMELY EASY to just burn the table into the EXE and query from it directly based on an ID and have all elements available.
DRapp
oh that makes sense :-)...Our primary reason to do that is to get away from testers asking us to change the message so much...lol. So gave it to Business Analyst (who at the end of the project has got very little to do) to fix that stuff.
Raja
When you mentioned doing it with Modules... Do you mean you had a class of all the different messagebox calls you COULD possibly call within a system (or subsystem if submodules)? And in each individual method would contain its own text, button settings, etc. If so, what memory impact is a static class of such footprint of all embedded messages vs using a resource file?
DRapp
A: 

You could use resource files to export all text into there (kinda localization feature as well). Resharper 5.0 really helps in that highlighting text that can be moved to resource.

Usually it looks like this:

  1. Before: MessageBox.Show(error.ToString(), "Error with extraction");
  2. Suggestion: Localizable string "Error with extraction"
  3. Right click Move to Resource
  4. Choose resource file and name (MainForm_ExtractArchive_Error_with_extraction), also check checkbox Find identical items in class ...
  5. Call it like this MessageBox.Show(error.ToString(), Resources.MainForm_ExtractArchive_Error_with_extraction);

Best of all it makes it easy to translate stuff to other languages as well as keeping text for MessageBox in separate Resource. Of course Resharper does it all for you so no need to type that much :-)

MadBoy
This is close to what I was thinking, but not the Resharper... trying to restrict any sub licensing items for distribution purposes and other reasons too.
DRapp
General idea is doable without Resharper, but Resharper just makes it very easy.
MadBoy
A: 

I suppose you could use a HashTable to do something similar like this, this can be found in:

using System.Collections;

To keep it globally accessable i was thinking a couple of functions in a class holding the hashtable to get/set a certain one. lets see now.

public class MessageBoxStore
{
    private HashTable stock;
    public string Get(string msg)
    {
        if (stock.ContainsKey(msg))
            return stock[msg];
        else
            return string.Empty;
    }

    public string Set(string msg, string msgcontent)
    {
        stock[msg] = msgcontent;
    }
}

or something like that, you could keep multiple different information in the hashtable and subsequently compose the messagebox in the function too.. instead of just returning the string for the messagebox contents... but to use this it would be quite simple.

call a function like this on program load.

public LoadErrorMessages()
{
    storeClass = new MessageBoxStore();
    storeClass.Set("UserCantDoThat", "Invalid action. Please confirm your action and try again");
}

for example, and then.

MessageBox.Show(storeClass.Get("UserCantDoThat"));

i put this in a new class instead of using the HashTable get/set methods direct because this leaves room for customization so the messagebox could be created in the get, and more than 1 piece of information could be stored in the set to handle messagebox title, buttontype, content, etc etc.

Skintkingle
Thanks for input, its kind of a cross between your input and MadBoy's... I'll putts around with what I've started and probably use similar principles offered.
DRapp