I'm new to C# programming and I'm trying to use good code practices. I know it is poor coding to use global variables in my example below, but I'm having a hard time figuring this out. So,I'm trying to accomplish two things with this question.
first of all, I am trying to figure out how to pass the text from a multi-line textbox to a function and have it return an array that I can then pass to another function for output (display/printing/saving to a file).
Second, Make my code more re-usable (by moving the globals inside the function that they are actually used in).
My question is.. How do I pass a string to a function and return an array that can then be passed to another function?
public partial class Form1 : Form
{
string[] SignalStrengthInputArray450;
string[] SignalStrengthOutputVar450 = new string[7];
// cut out other functions
private void Submit_450_Click(object sender, EventArgs e)
{
// ensure that input textbox is not null then call Load function
// SignalStrenthInput_450 is the object name of a multi-line textbox
if (!String.IsNullOrWhiteSpace(SignalStrengthInput_450.Text))
{
Load_Signal_Strength_Array();
}
else
{
// do something different
}
// additonal code for other textboxes
}
private void Load_Signal_Strength_Array()
{
// Processing Signal Strength textbox
SignalStrengthInputArray450 = SignalStrengthInput_450.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
foreach (string a in SignalStrengthInputArray450)
{
// loads some stuff into the SignalStrengthOutputArray450 array
}
}
}
You need a parameter and return type (string array), you may need to read more about Passing Parameters and return statement for returning values.
private string[] Load_Signal_Strength_Array(string signalStrengthInput_450)
{
string[] SignalStrengthInputArray450 = SignalStrengthInput_450.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
foreach (string a in SignalStrengthInputArray450)
{
// loads some stuff into the SignalStrengthOutputArray450 array
}
return SignalStrengthInputArray450;
}
Method call would be like
string[] signalStrengthArray = Load_Signal_Strength_Array(SignalStrengthInput_450.Text);
You can return array from function:
public string[] f1(string s)
{
return s.Split('/');
}
You can pass return value to anoter function:
public void f2(string[] p)
{
foreach(var item in p)
Console.WriteLine(item);
}
Use like:
public void main()
{
f2(f1("some/delimited/string");
}
Why not
Load_Signal_Strength_Array(SignalStrengthInput_450.Text)
Example:
private string[] Load_Signal_Strength_Array(string text)
{
// Processing text
var inputs = text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
var outputs = new List<string>();
foreach (string a in inputs)
{
// loads some stuff into the output array
// example:
if (!string.IsNullOrEmpty(a)) outputs.add(a);
}
return outputs.ToArray();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.