C# newbie question on a variable

I understand PowerShell but this is my first ever attempt at C# using Visual studio 2013.
I want to be able to click one of my programs, get a description of it and then choose to run it.
So far I have most of what I want but I am stuck on the variable portion.
I think the issue is the variable does not carry into the next code block for lack of a better understanding.
Hope the picture is shown.

Screen shot of mt first C# program
I click FindTST radio button and I get my description of what it does in the text box. That part works fine.
After I see what it I want to click run it and have it start the program.
So I have stored it in a variable called path1 but the button that says RunIT does not seem to recognize the variable path1.
My code is below.
Thanks


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication8
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void RBFindTST_CheckedChanged(object sender, EventArgs e)
        {
            
            string  path1 = @"C:\Users\9201401145\Google Drive\Scripts\PowerShell\FIND THE TST.exe";
            
            TXTInfo.Text = "Runs the below program." + "\r\n\r\n" + " " + path1;
            // C:\Users\9201401145\Google Drive\Scripts\PowerShell\FIND THE TST.exe
            

        }

        private void TXTInfo_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            TXTInfo.Text = "";
        }

        private void BTNRunIT_Click(object sender, EventArgs e)
        {
            TXTInfo.Text = path1;

        }
    }
}

Open in new window

SLPowersEngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
That's due to variable scoping.

This link is for javascript but most variables across all languages work the same way.
https://msdn.microsoft.com/en-us/library/ie/bzt2dkta%28v=vs.94%29.aspx

to fix it declare this outside of your function and  RBFindTST_CheckedChanged shold look like this after.
string  path1 = "";
  private void RBFindTST_CheckedChanged(object sender, EventArgs e)
        {
            
            path1 = @"C:\Users\9201401145\Google Drive\Scripts\PowerShell\FIND THE TST.exe";
            
            TXTInfo.Text = "Runs the below program." + "\r\n\r\n" + " " + path1;
            // C:\Users\9201401145\Google Drive\Scripts\PowerShell\FIND THE TST.exe
            

        }

Open in new window

0
SLPowersEngineerAuthor Commented:
when comparing your suggested change with what i posted i only see the one addition of the line
string  path1 = "";

This does not solve the issue of how to make that sting visible outside of that function.  When i click run it it sees path1 as equal to nothing.  

I did look at the link you provided but i have not yet figured out what i am missing.

The idea is what ever button i click it would set the path1 variable to the location of the file i want to run.  then i can click RunIT and it will run that file externally.  Thanks
0
Kyle AbrahamsSenior .Net DeveloperCommented:
There were 2 changes.

The first is to add string path =""; outside of the function.

The second is within the function . . . remove the keyword STRING from path one so that inside the function uses the external variable.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SLPowersEngineerAuthor Commented:
OMG if it was a snake it would have bit me.

Works!!!!!

Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.