在方法之间传递参数 [英] Passing parameters between methods
问题描述
我有一个用Richtextbox编写的不同字符的列表.现在我想使用循环从列表中一个一个地拉出,并想检查它是数字还是特殊字符或单词等.
I have a list of different characters written in a richtextbox.Now i want to pull one by one from the list using a loop and want to examine whether it is number or special character or word etc.
var items = listView1.Items;
for (int i = 0; i < list.Count; i++)
{
matchNumber(list[i].ToString(),""); //this loop isn't complete
}
public void matchNumber(string number,string message)
{
if (number == "0" || number == "1" || number == "2" || number == "3" || number == "4" || number == "5" || number == "6" || number == "7" || number == "8" || number == "9")
message = "number";
}
public void matchSpecialCharacter(string specialCharacter, string message)
{
if (specialCharacter == "," || specialCharacter == ";" || specialCharacter == "{" || specialCharacter == "}" || specialCharacter == "[" || specialCharacter == "]" || specialCharacter == "+" || specialCharacter == "-" || specialCharacter == "*" || specialCharacter == "/" || specialCharacter == "%")
message = "special character";
}
matchNumber和matchSpecialCharacter是用于过滤每个list [i]项目的方法.因此,我需要将每个list [i]作为参数传递给这些方法,并从这些方法中将消息作为另一个参数返回消息以循环打印.因此,我该如何配置循环以成功为每个列表[i]打印它,无论它是数字还是其他字符.
matchNumber and matchSpecialCharacter are methods to filter each list[i] item.So i need to pass each list[i] to those methods as a parameter and return message as another parameter from those methods to print in loop.So how can i configure that loop to print it successfully for each list[i],whether it is number or other character.
推荐答案
为什么将测试分为两个单独的方法.我的意思是,您能否仅使用一种方法,例如:
Why split the test to two separate methods. What I mean is that could you simply have a single method, something like:
public void DetermineType(char character, string message) {
// test the type of the character
}
这样,您就不必决定在循环中调用哪种方法.
另一件事,如果要测试字符是否为数字,则可以使用 char.IsNumber [ ^ ].例如:
This way you wouldn''t have to decide which method to call in the loop.
Another thing, if you want to test if a character is a number, you can use char.IsNumber[^]. For example:
if(char.IsNumber(character)) {
...
}
我宁愿使用正则表达式来搜索它是否包含您提到的任何数字或任何特殊字符.
I would rather use a regular expression to search if it contains any number or any special character you mentioned.
这篇关于在方法之间传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!