Every application has to inform the user about certain actions. Most of the applications accomplish this by simply displaying the message on the label control. Usually when taking this approach we find our self repeating a lot of code. In this article we will see that how we can make a simple class that is responsible for displaying the messages to the user. We will make a simple application that will allow only zip files to upload. In this article we are not going to perform the actual upload. If you want the upload feature you can check my article Uploading Images to Server Folder and SQL Server 2000 Database.
Introduction:
Every application has to inform the user about certain actions. Most of the applications accomplish this by simply displaying the message on the label control. Usually when taking this approach we find our self repeating a lot of code. In this article we will see that how we can make a simple class that is responsible for displaying the messages to the user. We will make a simple application that will allow only zip files to upload. In this article we are not going to perform the actual upload. If you want the upload feature you can check my article Uploading Images to Server Folder and SQL Server 2000 Database.
First Approach:
Let's see how most of the developers perform this action. Below is the screen shot of the user interface.
Now we want to validate the file when the "Upload File" is clicked. Let's see the Upload button code:
string filePath = File1.PostedFile.FileName;
bool result = BLL.IsZipFile(filePath); if(result) { lblMsg.Text = "File is valid"; } else { lblMsg.Text = "File is Invalid"; } |
I have made bad code in bold. The reason is that we will repeat the same code if we need to print some other message.
And here is the IsZipFile method:
public static bool IsZipFile(string filePath)
{ string fileExtension = System.IO.Path.GetExtension(filePath); if(fileExtension.Equals(".zip")) { return true; } else { return false; } } |
Second Approach:
In this approach we will make a class that is responsible for displaying the messages. The sole purpose of the class is to send the messages to the client. We will call this class "Messages". As you can see I have defined string constants to hold the actual message. This is because if in future some message is changed I only need to change it on one place. The method IsFileValid simply returns the message back caller.
public class Messages
{ private const string MSG_FILE_VALID = "Valid: Zip file uploaded"; private const string MSG_FILE_NOT_VALID = "InValid: File is not a zip file"; // returns the message depending if the file is valid or not public static string IsFileValid(bool isZipFile) { if(isZipFile) { return MSG_FILE_VALID; } else { return MSG_FILE_NOT_VALID; } }
|
Another approach is to use the Resource Files to hold the messages. The advantage of Resource Files is that you can display your message in different languages depending on the culture of the user. I will discuss this approach in my new article.
I hope you like the article, happy coding!