tags:

views:

192

answers:

6

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 = textBox2.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.

A: 

You can store it in a class level variable. In that way it can be accessed by any function.

Yogendra
+2  A: 

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).

cHao
+6  A: 

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('.');    
}   
Fosco
+9  A: 

You should replace your getFilename2 function with

Path.GetFileNameWithoutExtension(textBox2.Text)
SLaks
Which returns a string you can use directly `string filez = Path.GetFileNameWithoutExtension(textBox2.Text);`
ChrisF
This may be good to clean up the code, but doesn't answer the fundamental question by the OP. Not down-voting, but not sure why this is being up-voted.
Metro Smurf
+3  A: 

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
}
Rachel
+1 for actually keeping the voids as requested. Although I agree he should just return the string.
Jack
I also agree that he should just return a string for this scenario, although I have encountered some weird coding problems that require us to get creative so figured I'd post this solution as an alternative :)
Rachel
+3  A: 

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

Metro Smurf
you're a saint.
tenfour