All i want to do is pass a string from one void to another.
private void getFilename2()
{
if (textBox2.TextLength.Equals("0"))
{
}
else
{
string inputString = te开发者_JAVA技巧xtBox2.Text.ToString();
string last = inputString.Substring(inputString.LastIndexOf('\\') + 1);
string[] filename2 = last.Split('.');
}
}
private void button1_Click(object sender, EventArgs e)
{
getFilename1();
getFilename2();
string filez = filename2;
}
I know this doesn't work but I'm very unfamiliar with how to move strings around in voids.
You should replace your getFilename2
function with
Path.GetFileNameWithoutExtension(textBox2.Text)
Your best bet would be to use a class field/property, or a function that returns a value.
string filez = GetFilename2();
private string GetFilename2() {
{
if (textBox2.TextLength.Equals("0")) return "";
string inputString = textBox2.Text.ToString();
string last = inputString.Substring(inputString.LastIndexOf('\\') + 1);
return last.Split('.');
}
You could pass the string by reference as a parameter to your functions
private void button1_Click(object sender, EventArgs e)
{
string fileName;
getFilename1(ref string fileName);
}
private void getFilename1(ref string fileName)
{
// Whatever you do with fileName here will be reflected in the other function
}
Let's start with the name of your method: getFilename2
.
- The prefix of "get" implies the method should have a return type
- A more appropriate name may be
SetFileName
I'm assuming there is a getFileName1
method that is retrieving the file name from textBox1
and has the exact same code as getFileName2
, but uses textBox1
instead of textBox2
. This would be a good place to refactor your code and create a common method that can be reused:
private string GetFileName(string str)
{
if (string.IsNullOrEmpty(str)) return string.Empty;
string last = str.Substring(str.LastIndexOf('\\') + 1);
return last.Split('.');
}
But, we can refactor again and just use a built-in .NET method:
private string GetFileName(string str)
{
return Path.GetFileNameWithoutExtension(str);
}
And now that there is a common method, we can re-use it as needed:
private void button1_Click(object sender, EventArgs e)
{
string filez = GetFileName(textBox2.Text);
}
Now we have a method of GetFileName(); all it is doing is calling a built-in .NET method of GetFileNameWithoutExtension(). So, instead of even having a method, we should just use the built-in .NET method for returning a file name:
private void button1_Click(object sender, EventArgs e)
{
string filez = Path.GetFileNameWithoutExtension(textBox2.Text);
}
Now, let's look at passing a string from one void to another. Typically, you'd want to do this with an internal field or property. Since I'm partial to properties, I'll use them as an example:
private string FileName1 {get; set;}
private string FileName2 {get; set;}
private void SetFileName1()
{
FileName1 = Path.GetFileNameWithoutExtension(textBox1.Text);
}
private void SetFileName2()
{
FileName2 = Path.GetFileNameWithoutExtension(textBox2.Text);
}
private void button1_Click(object sender, EventArgs e)
{
SetFileName1();
SetFileName2();
string filez1 = FileName1;
string filez2 = FileName2;
}
However, if you did not want to use internal fields or properties, you could set the values by ref as answered by Rachel
If you're passing strings around, ideally you should be explicitly passing them around. IE: make your functions take and/or return the values they'll work with, especially if the return values aren't intended to be used by anything but the code that calls getFilename2
. If you can't do that, however, you can declare a private string filename1 = null; public string[] filename2 = null
inside the class, but outside any of your methods.
If you go that route, make sure to remove any string filename1
or string[] filename2
from your methods, or you'll end up declaring a local variable with the same name (and never setting the instance variables).
You can store it in a class level variable. In that way it can be accessed by any function.
精彩评论