C# 如何验证输入的数组数和验证空白数组
今天我想问一个问题。我正在尝试找出如何验证可以输入的数组数。例如,我试图验证输入为6的最大数组数。当我尝试时,它有一些错误C# 如何验证输入的数组数和验证空白数组,c#,arrays,validation,C#,Arrays,Validation,今天我想问一个问题。我正在尝试找出如何验证可以输入的数组数。例如,我试图验证输入为6的最大数组数。当我尝试时,它有一些错误 if (productcode == null || productcode.Length == 0) { MessageBox.Show("Enter array for productcode"); } if (productcode[] &
if (productcode == null || productcode.Length == 0)
{
MessageBox.Show("Enter array for productcode");
}
if (productcode[] > productcode[5])
{
MessageBox.Show("Array for productcode exceeded");
}
if (productcode == null || productcode.Length == 0)
{
MessageBox.Show("Enter array for product code");
}
if (productdesc[] > productdesc[5])
{
MessageBox.Show("Array for productdesc exceeded");
}
if (quantity == null || quantity.Length == 0)
{
MessageBox.Show("Enter array for quantity");
}
if(quantity[] > quantity[5])
{
MessageBox.Show("Array for quantity exceeded");
}
if (batchnumber == null || batchnumber.Length == 0)
{
MessageBox.Show("Enter array for batch number");
}
if (batchnumber[] > batchnumber[5])
{
MessageBox.Show("Array for batch number exceeded");
}
if ( dod == null || dod.Length == 0)
{
MessageBox.Show("Enter array for dod");
}
if (dod[] > dod[5])
{
MessageBox.Show("Array for dod exceeded");
}
if (unitcost == null || unitcost.Length == 0)
{
MessageBox.Show("Enter array for unit cost");
}
if (unitcost[] > unitcost[5])
{
MessageBox.Show("Array for unitcost exceeded");
}
这是可能的还是有更好的方法来做这件事?不是对你问题的回答,但我认为你需要一个产品类别和产品列表:
public class Product
{
public string Code {get;set;}
public string Description {get;set;}
public int Quantity {get;set;}
public int BatchNumber {get;set;}
public string Dod {get;set;}
public decimal UnitCost {get;set;}
}
名单如下:
var products = new List<Product>();
var产品=新列表();
验证器:
public void Validate(List<Product> products)
{
if (products == null || products.Length == 0)
{
MessageBox.Show("Too few products");
return;
}
if (products.Length > 5)
{
MessageBox.Show("Too many products");
return;
}
// do something useful with the products
}
公共作废验证(列出产品)
{
if(乘积==null | |乘积.长度==0)
{
MessageBox.Show(“产品太少”);
返回;
}
如果(产品长度>5)
{
MessageBox.Show(“产品太多”);
返回;
}
//对产品做些有用的事情
}
不是对你问题的真正回答,但我认为你需要一个产品类别和一个产品列表:
public class Product
{
public string Code {get;set;}
public string Description {get;set;}
public int Quantity {get;set;}
public int BatchNumber {get;set;}
public string Dod {get;set;}
public decimal UnitCost {get;set;}
}
名单如下:
var products = new List<Product>();
var产品=新列表();
验证器:
public void Validate(List<Product> products)
{
if (products == null || products.Length == 0)
{
MessageBox.Show("Too few products");
return;
}
if (products.Length > 5)
{
MessageBox.Show("Too many products");
return;
}
// do something useful with the products
}
公共作废验证(列出产品)
{
if(乘积==null | |乘积.长度==0)
{
MessageBox.Show(“产品太少”);
返回;
}
如果(产品长度>5)
{
MessageBox.Show(“产品太多”);
返回;
}
//对产品做些有用的事情
}
不要复制自己!提取一种方法:
private static Boolean showArrayErrorText(String name, Array array, int maxLength = 5) {
if (array == null || array.Length <= 0) {
MessageBox.Show(String.Format("Enter array for {0}", name));
return false;
}
else if (array.Length > maxLength) {
MessageBox.Show(String.Format("Array for {0} exceeded", name));
return false;
}
return true;
}
...
showArrayErrorText("productcode", productcode);
showArrayErrorText("productdesc", productdesc);
showArrayErrorText("batchnumber", batchnumber);
...
不要抄袭你自己!提取一种方法:
private static Boolean showArrayErrorText(String name, Array array, int maxLength = 5) {
if (array == null || array.Length <= 0) {
MessageBox.Show(String.Format("Enter array for {0}", name));
return false;
}
else if (array.Length > maxLength) {
MessageBox.Show(String.Format("Array for {0} exceeded", name));
return false;
}
return true;
}
...
showArrayErrorText("productcode", productcode);
showArrayErrorText("productdesc", productdesc);
showArrayErrorText("batchnumber", batchnumber);
...
productcode[]>productcode[5]
不编译我想你的意思是productcode.Length>5
而不是productcode[]>productcode[5]
。不知道你想做什么。抱歉bool ValidateArraySize(object[]input,string arrayName){//your logic here}
?无论如何,不清楚你在问什么。如果您需要帮助来编译此文件,请解释您遇到的“一些错误”。如果你正在寻找另一种方式来编写你的节目,请解释你期望它做什么以及你找到了什么替代方案。productcode[]>productcode[5]
不是编译我认为你的意思是productcode.Length>5
而不是productcode[]>productcode[5]
。不知道你在尝试做什么。抱歉bool ValidateArraySize(object[]input,string arrayName){//your logic here}
?无论如何,不清楚你在问什么。如果您需要帮助来编译此文件,请解释您遇到的“一些错误”。如果你正在寻找另一种方式来写你的节目,解释一下你期望它做什么,以及你找到了什么替代方案。