Click to See Complete Forum and Search --> : [2005] Creating subroutines
Pasvorto
Jul 20th, 2007, 02:24 PM
OK, so I have a bunch of fields that I need to validate (numeric). Does anyone have sample code of a subroutine where I could just send it the field name and it could test the field for non-numeric value?
In the spirit of 'good programming', this validation code should only exist in one place. In the C# world, would that be a class? I thought I read somewhere that there aren't any 'global' subroutines (like VB)
This is my current (repeated many times) code:
string strScore;
int intScore;
strScore = txtPayment.Text;
bool blnInput;
blnInput = Int32.TryParse(strScore, out intScore);
If (blnInput == false)
.....MessageBox.Show("Field must be numeric", "TVAL", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
nmadd
Jul 20th, 2007, 03:04 PM
If that code won't return anything and you're just checking for an integer, you can place that code in a void method. Just as you would place it in a subroutine in VB.
// Check for an integer.
private void CheckForInteger(string score)
{
int result;
if (!Int32.TryParse(score, out result))
{
MessageBox.Show("Not an integer.");
}
}
Then, again like in VB, call it whenever you need to:
// Call my method.
private void button1_Click(object sender, EventArgs e)
{
CheckForInteger(this.textBox1.Text);
}
Here is some great help for C#:
http://msdn2.microsoft.com/en-us/library/ms186211(VS.80).aspx
Hope that helps.
jmcilhinney
Jul 20th, 2007, 06:49 PM
As has been said, the C# equivalent of a VB procedure (Sub) is a function with a void return type. Also, the C# equivalent of a VB module is a static class.
I'd be inclined to use a boolean function in this case though:public bool ValidateInteger(string val)
{
bool result = false;
int number;
if (int.TryParse(val, number))
{
result = true;
}
else
{
MessageBox.Show("Please enter an integer value",
"Invalid Input",
MessageBoxButtons.OK,
MessageBoxIcon.Warning);
}
return result;
}That combines validation and returning the result with displaying the error message if it fails. What it doesn't do is give you the numeric value, which is the problem with centralised validation like that. You can't get an integer and a boolean returned. That's why TryParse has the number passed by reference, but if you were going to try that yourself then you may as well just use TryParse directly each time.
VBGuy
Jul 22nd, 2007, 12:20 PM
Here is a general validation class I have been using. It uses regex for the actual validations.
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions ;
namespace Utilities
{
/// <summary>
/// Basic data validation class
/// </summary>
public class Validation
{
/// <summary>
/// Enumeration of validation types
/// </summary>
public enum PatternTypes
{
/// <summary>
/// General regex numeric test
/// </summary>
Numeric,
/// <summary>
/// General regex phone number test
/// </summary>
Phone,
/// <summary>
/// General regex zip code test
/// </summary>
Zipcode,
/// <summary>
/// General regex email address test
/// </summary>
Email
}
#region REGEX Patterns
private const string Numeric = @"(^[-+]?\d+(,?\d*)*\.?\d*([Ee][-+]\d*)?$)|(^[-+]?\d?(,?\d*)*\.\d+([Ee][-+]\d*)?$)";
private const string Phone = @"^\d{10}$";
private const string Zipcode = @"^\d{5}$";
private const string Email = @"^(([^<>()[\]\\.,;:\s@\""]+"
+ @"(\.[^<>()[\]\\.,;:\s@\""]+)*)|(\"".+\""))@"
+ @"((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
+ @"\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+"
+ @"[a-zA-Z]{2,}))$";
#endregion
/// <summary>
/// Main validation method. Depending on valiation type
/// it sets a regex pattern and validates the data against it.
/// </summary>
/// <param name="Value">String value to be validated</param>
/// <param name="Type">Item from PatternTypes enumeration</param>
/// <returns>Boolean - Success/Fail</returns>
public bool Validate(string Value, PatternTypes Type)
{
string pattern = "";
switch (Type)
{
case PatternTypes.Numeric:
pattern = Numeric;
break;
case PatternTypes.Phone:
pattern = Phone ;
break;
case PatternTypes.Zipcode:
pattern = Zipcode ;
break;
case PatternTypes.Email:
pattern = Email ;
break;
default:
break;
}
string patternStrict = pattern;
Regex reStrict = new Regex(patternStrict);
if (!reStrict.IsMatch(Value))
return false;
return true;
}
/// <summary>
/// Very basic IsNumeric method
/// </summary>
/// <param name="Value">String value to be validated</param>
/// <returns>Boolean - Success/Fail</returns>
public bool IsNumeric(string Value)
{
string patternStrict = Numeric;
Regex reStrict = new Regex(patternStrict);
if (!reStrict.IsMatch(Value))
return false;
return true;
}
/// <summary>
/// Combines an isnumeric check with an empty value check
/// </summary>
/// <param name="Value">String value to be validated</param>
/// <returns>Boolean - Success/Fail</returns>
public bool ValidateGeneral(string Value)
{
if (IsNumeric(Value) || Value.Length == 0)
return false;
else
return true;
}
}
}
Pasvorto
Jul 23rd, 2007, 09:01 AM
Thanks for the input folks. Most helpful. Got to play with "subroutines" now.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.