Click here to Skip to main content
16,006,378 members
Home / Discussions / C#
   

C#

 
GeneralRe: How to handle a null enum retrieved from web service Pin
Brian Triplett26-Aug-09 5:08
Brian Triplett26-Aug-09 5:08 
QuestionSuggestions on how to model a simple validation program Pin
neualex26-Aug-09 2:53
neualex26-Aug-09 2:53 
AnswerRe: Suggestions on how to model a simple validation program Pin
Super Lloyd26-Aug-09 3:12
Super Lloyd26-Aug-09 3:12 
GeneralRe: Suggestions on how to model a simple validation program Pin
neualex26-Aug-09 3:48
neualex26-Aug-09 3:48 
GeneralRe: Suggestions on how to model a simple validation program Pin
Super Lloyd26-Aug-09 3:56
Super Lloyd26-Aug-09 3:56 
AnswerRe: Suggestions on how to model a simple validation program Pin
N a v a n e e t h26-Aug-09 5:20
N a v a n e e t h26-Aug-09 5:20 
QuestionRe: Suggestions on how to model a simple validation program Pin
neualex26-Aug-09 17:49
neualex26-Aug-09 17:49 
AnswerRe: Suggestions on how to model a simple validation program Pin
N a v a n e e t h26-Aug-09 18:25
N a v a n e e t h26-Aug-09 18:25 
neualex wrote:
If I understand this correctly, once I have all possible validation classes implemented with the same interface... should I just leave the *.cs files by themselves or do I need to compile those as well so the main class can load them?


Here is what I meant,

1 - You have created the application with 3 validation classes all implements IValidator interface.
2 - I assume you will have a configuration file. In windows applications, app.config can be used or a settings file.
3 - You need to add all the validation classes to this configuration file so that reflection can load it. By full name, I meant the fully qualified name including the assembly name where the classes resides.
4 - Read the configuration file and load the specified classes with Activator.CreateInstance() and cast it to IValidator to call Validate() on it.

neualex wrote:
If you happen to have a small working project similar to what we are trying to accomplish here, can you share it?


Unfortunately, I don't have.

Here is a sample from which you can understand what I meant. For simplicity, I assume all your validation methods has a signature like bool Validate(object itemToValidate). So the first step is to define the interface.
C#
public interface IValidator
{
   bool Validate(object itemToValidate);
}
Let us create a simple validation class named FirstValidation.
C#
public sealed class FirstValidation : IValidator
{
    public bool Validate(object itemToValidate)
    {
        /* Your code for validation */
    }
}
Next validation class
C#
public sealed class SecondValidation : IValidator
{
    public bool Validate(object itemToValidate)
    {
        /* Your code for validation */
    }
}
Here is the entry in app.config looks like. I am using settings file with the application scope. So it will generate entries like the below. Text in bold are the class names which we have to load using reflection.
<applicationSettings>
    <TestApplication.Settings1>
        <setting name="ValidationClasses" serializeAs="Xml">
            <value>
                <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                    <string>Test.First</string>
                    <string>Test.Second</string>
                </ArrayOfString>
            </value>
        </setting>
    </TestApplication.Settings1>
</applicationSettings>
In this case, Test is my namespace name. When you specify the class name, it has to follow a format like NamespaceName.ClassName. Here is how you activate the class instance
C#
foreach (string s in Settings1.Default.ValidationClasses)
{
    Type type = Type.GetType(s);
    IValidator validator = (IValidator)Activator.CreateInstance(type);
    validator.Validate();
}
When the validation classes are in different assembly, you need to use the other overload of Activator.CreateInstance which takes assembly name.

Hope this made it clear.


QuestionRe: Suggestions on how to model a simple validation program Pin
neualex27-Aug-09 1:56
neualex27-Aug-09 1:56 
AnswerRe: Suggestions on how to model a simple validation program Pin
N a v a n e e t h27-Aug-09 2:23
N a v a n e e t h27-Aug-09 2:23 
GeneralRe: Suggestions on how to model a simple validation program Pin
neualex27-Aug-09 2:30
neualex27-Aug-09 2:30 
Questionproblem with data table Pin
myinstincts26-Aug-09 2:39
myinstincts26-Aug-09 2:39 
AnswerRe: problem with data table Pin
baranils26-Aug-09 4:47
baranils26-Aug-09 4:47 
Questionsyntax error...plz help me Pin
ankitjain111026-Aug-09 2:02
ankitjain111026-Aug-09 2:02 
AnswerRe: syntax error...please help me Pin
Tony Richards26-Aug-09 2:14
Tony Richards26-Aug-09 2:14 
GeneralRe: syntax error...please help me Pin
ankitjain111026-Aug-09 2:22
ankitjain111026-Aug-09 2:22 
AnswerSame question Pin
Not Active26-Aug-09 2:15
mentorNot Active26-Aug-09 2:15 
GeneralRe: Same question Pin
Tony Richards26-Aug-09 2:18
Tony Richards26-Aug-09 2:18 
QuestionDisplay DataGridView Dynamically Pin
Ankit At Codeproject26-Aug-09 1:44
Ankit At Codeproject26-Aug-09 1:44 
AnswerRe: Display DataGridView Dynamically Pin
stancrm26-Aug-09 1:51
stancrm26-Aug-09 1:51 
QuestionListView Context Menu Pin
kanchoette26-Aug-09 1:43
kanchoette26-Aug-09 1:43 
AnswerRe: ListView Context Menu Pin
stancrm26-Aug-09 1:48
stancrm26-Aug-09 1:48 
GeneralRe: ListView Context Menu Pin
kanchoette26-Aug-09 1:49
kanchoette26-Aug-09 1:49 
QuestionProblem in connecting with SQL database on client machine Pin
Affan Toor26-Aug-09 1:29
Affan Toor26-Aug-09 1:29 
AnswerRe: Problem in connecting with SQL database on client machine Pin
Henry Minute26-Aug-09 3:48
Henry Minute26-Aug-09 3:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.