C # Pass multi line string to function and return array
I am new to C # programming and I try to use good coding techniques. I know bad coding is using globals in my example below, but I'm having a hard time figuring it out. So, I am trying to accomplish two things with this question.
First of all, I'm trying to figure out how to pass text from a multi-line textbox to a function and return an array to it, which I can then pass to another function for output (display / print / save to file).
Second, make my code more reusable (by moving global blocks within the function in which they are actually used).
My question is, how can I pass a string to a function and return an array, which 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
}
}
}
source to share
You need a parameter and a return type (string array), you may need to read more about Passing Parameters and the return statement to return 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;
}
The method call will be like
string[] signalStrengthArray = Load_Signal_Strength_Array(SignalStrengthInput_450.Text);
source to share
You can return an array from a function:
public string[] f1(string s)
{
return s.Split('/');
}
You can pass the return value to the 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");
}
source to share
Why not
- has a Load_Signal_Strength_Array function that takes a local string as a parameter and returns the processed output (since the array is here, but IList could be better)
- calling this function with a simple
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();
}
source to share