• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 131
  • Last Modified:

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

0
SLPowers
Asked:
SLPowers
  • 2
  • 2
1 Solution
 
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
 
SLPowersEngineerAuthor Commented:
OMG if it was a snake it would have bit me.

Works!!!!!

Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now