You need to create an instance of the class that semomaniz posted in order to use it's methods.
This goes back to the basics on classes and objects. A class is like a blue print for a house: it defines the rooms, doors, and properties of the house. An instance of the class is the actual house. You cannot use the rooms and doors of the house until you create an instance of it.
So, like I was saying, you need to create an instance of the class that semomaniz posted:
-
Utility validatorUtility;
-
validatorUtility = new Utility(); //<--creates a new instance of the class
Now you can use the properties and methods:
-
Utility validatorUtility;
-
validatorUtility = new Utility(); //<--creates a new instance of the class
-
-
//Setting the ValidationText property of the vaidatorUtility Utility Object
-
validatorUtility.ValidationText = myTextBox.Text;
-
-
//Now that you have set the ValidationText property
-
//of the validatorUtility Utility Object you can validate the text
-
-
-
//To validate the text you call the validate method:
-
if(validatorUtility.IsValid){
-
//the text is valid
-
}else{
-
//the text is not valid
-
}
-
What I was suggesting was something a little different different.
I was suggesting that you the Utility class only contains
static methods. That way you wouldn't have to create an instance of the class every time you want to validate something.
The class would look like:
-
using System;
-
using System.Data;
-
using System.Collections;
-
-
public static class Utility
-
{
-
//constructor
-
private static Utility() { }
-
-
//method
-
public static bool IsValid(string textToValidate)
-
{
-
bool result = false;
-
//creating a list of strings that are
-
//VALID entries
-
ArrayList blacklist = new ArrayList();
-
-
//now add all the VALID options to the whiteList ArrayList
-
whitelist.Add("abc");
-
whitelist.Add("ggc");
-
-
-
foreach (string validOption in whitelist)
-
{
-
if ( String.Compare(validOption, textToValidate, true) == 0 )
-
{
-
return result = true;
-
}
-
}
-
return result;
-
}
-
-
}
I changed the proposed validation method to use a
whitelist instead of a
blacklist. A whitelist is a list of valid options that the user can provide. A black list is a list of invalid options. It is much easier to maintain a list of valid options instead of a list of invalid options....
Anyways....to use my validation method you would not have to create an instance of the class. You'd just use the method:
-
if(Utility.IsValid(myTextBox.Text)){
-
//the text is valid
-
}else{
-
//the text is not valid
-
}
-
Static classes and methods are advanced topics though.
So for now I would recommend using semomaniz's class since it's easier to understand for now. I would probably change semomaniz's validation method to use a whitelist though ;)
-Frinny