2
votes

I started learning C# a few days ago and I'm having a problem with public strings, I'm currently trying to write a program that copies and replaces files for practice but I'm having a problem with public strings no matter how much I try to change up the code I couldn't figure it out myself so I came here for help

What am I doing wrong?

Here's the code:

namespace Extractor
{
    public partial class Form1 : Form
    {
        public string s
        {
            get;
            set;
        }
        public string sSelectedPath
        {
            get;
            set;
        }
        public string beckup
        {
            get;
            set;
        }
        public Form1()
        {
            InitializeComponent();
        }
        private void direc_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.Description = "Select a folder";

        if (fbd.ShowDialog() == DialogResult.OK)
        {
            string sSelectedPath = fbd.SelectedPath;
        }

    }

    private void choof_Click(object sender, EventArgs e)
    {
        OpenFileDialog choofdlog = new OpenFileDialog();
        choofdlog.Filter = "All Files (*.*)|*.*";
        choofdlog.FilterIndex = 1;
        choofdlog.Multiselect = true;

        if (choofdlog.ShowDialog() == DialogResult.OK)
        {
             string s = choofdlog.FileName;
        }

    }

    private void button3_Click(object sender, EventArgs e)
    {
        ReplaceFile( s, sSelectedPath, beckup);
    }

    public static void ReplaceFile(string FileToMoveAndDelete, string FileToReplace, string BackupOfFileToReplace)
    {
        File.Replace(FileToMoveAndDelete, FileToReplace, BackupOfFileToReplace, false);

    }

    private void button1_Click(object sender, EventArgs e)
    {
        FolderBrowserDialog fbb = new FolderBrowserDialog();
        fbb.Description = "Select a folder";

        if (fbb.ShowDialog() == DialogResult.OK)
        {
            string beckup = fbb.SelectedPath;
        }
    }
}

3
According to guidelines - you should use Pascal Case in properties naming - msdn.microsoft.com/en-us/library/fzcth91k%28v=vs.71%29.aspxDmitry Zaets

3 Answers

3
votes

Your mistake is here :

if (choofdlog.ShowDialog() == DialogResult.OK)
{
    string sSelectedPath = choofdlog.FileName;
}

You're using a local variable, not the member variable. So, write :

if (choofdlog.ShowDialog() == DialogResult.OK)
{
    sSelectedPath = choofdlog.FileName;
}

Or, better, if you doesn't want to make mistake, use this. when you write a member variable or method :

if (choofdlog.ShowDialog() == DialogResult.OK)
{
    this.sSelectedPath  = choofdlog.FileName;
}
5
votes
if (fbd.ShowDialog() == DialogResult.OK)
{
        string sSelectedPath = fbd.SelectedPath;
}

You are declaring a new variable here by using the string keyword. It's not setting the property. Just remove string. E.g.

if (fbd.ShowDialog() == DialogResult.OK)
{
        sSelectedPath = fbd.SelectedPath;
}
1
votes

Replace

 string s = choofdlog.FileName;

with

  s = choofdlog.FileName;