Avatar of SLPowers
SLPowers
Flag for United States of America asked on

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

C#

Avatar of undefined
Last Comment
SLPowers

8/22/2022 - Mon
Kyle Abrahams

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

SLPowers

ASKER
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
ASKER CERTIFIED SOLUTION
Kyle Abrahams

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SLPowers

ASKER
OMG if it was a snake it would have bit me.

Works!!!!!

Thanks!
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23