none
Recognize variables of another method RRS feed

  • Question

  • Hi!

    In the code below I have a class and three public methods inside. In the first method I have created an object called opendialog (is in bold) . In the second one, I have a string variable also in bold, folderNa. My problem is that the third method doesn´t recognize "opendialog" and "folderNa". How can I declare them in the third method???  Thanks.

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


        public  class FileUpload
        {

            public string browseDoc(string file)  //FIRST METHOD
            {

                OpenFileDialog opendialog = new OpenFileDialog();

                DialogResult result = opendialog.ShowDialog();

                if (result == DialogResult.OK)
                {

                    file = System.IO.Path.GetFileName(opendialog.FileName);

                    return file;
                }
                else
                {
                    return null;
                }
            }

                public string browseFolder(string folderNa)  //SECOND METHOD
            {

            FolderBrowserDialog fold = new FolderBrowserDialog();

            DialogResult result = fold.ShowDialog();

            if (result == DialogResult.OK)
            {
    folderNa = fold.SelectedPath;
                return folderNa;
            }
            else
            {
                return null;
            }

            }

            private void uploadFile(string selectedFile)  //THIRD METHOD
            {

                int count = 0;
                string[] FilenameName;
                foreach (string item in opendialog.FileNames)
                {
                    FilenameName = item.Split('\\');
                    File.Copy(item, folderNa + FilenameName[FilenameName.Length - 1]);
                    count++;
                }
                MessageBox.Show(Convert.ToString(count) + " File(s) copied");
            }
        }

                                                    

    • Edited by iñaki11 Thursday, April 28, 2016 7:18 AM
    Thursday, April 28, 2016 7:17 AM

Answers

  • Hi,

    Why the variables are given to the functions as parameter?

    You can make the global in the class:

    public class FileUpload
    {
        private string file;
        private string folderNa;
    
        public string browseDoc()  //FIRST METHOD
        {
            OpenFileDialog opendialog = new OpenFileDialog();
            DialogResult result = opendialog.ShowDialog();
    
            if (result == DialogResult.OK)
            {
                file = System.IO.Path.GetFileName(opendialog.FileName);
                return file;
            }
            else
            {
                file = string.Empty;
                return null;
            }
        }
    
        public string browseFolder()  //SECOND METHOD
        {
            FolderBrowserDialog fold = new FolderBrowserDialog();
            DialogResult result = fold.ShowDialog();
    
            if (result == DialogResult.OK)
            {
                folderNa = fold.SelectedPath; ;
                return folderNa;
            }
            else
            {
                folderNa = string.Empty;
                return null;
            }
    
        }
    
        private void uploadFile(string selectedFile)  //THIRD METHOD
        {
            int count = 0;
            string[] FilenameName;
            foreach (string item in opendialog.FileNames)
            {
                FilenameName = item.Split('\\');
                File.Copy(item, folderNa + FilenameName[FilenameName.Length - 1]);
                count++;
            }
            MessageBox.Show(Convert.ToString(count) + " File(s) copied");
        }
    }

    Regards,

    David

    Thursday, April 28, 2016 7:33 AM
  • Hi ,

    You could use the following code like this.

      public class FileUpload
        {
            OpenFileDialog opendialog;
            private string folderNa;
            public string browseDoc(string file)  //FIRST METHOD
            {
    
                opendialog = new OpenFileDialog();
    
                DialogResult result = opendialog.ShowDialog();
    
                if (result == DialogResult.OK)
                {
    
                    file = System.IO.Path.GetFileName(opendialog.FileName);
    
                    return file;
                }
                else
                {
                    return null;
                }
            }

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, April 29, 2016 2:14 AM

All replies

  • Hi,

    Why the variables are given to the functions as parameter?

    You can make the global in the class:

    public class FileUpload
    {
        private string file;
        private string folderNa;
    
        public string browseDoc()  //FIRST METHOD
        {
            OpenFileDialog opendialog = new OpenFileDialog();
            DialogResult result = opendialog.ShowDialog();
    
            if (result == DialogResult.OK)
            {
                file = System.IO.Path.GetFileName(opendialog.FileName);
                return file;
            }
            else
            {
                file = string.Empty;
                return null;
            }
        }
    
        public string browseFolder()  //SECOND METHOD
        {
            FolderBrowserDialog fold = new FolderBrowserDialog();
            DialogResult result = fold.ShowDialog();
    
            if (result == DialogResult.OK)
            {
                folderNa = fold.SelectedPath; ;
                return folderNa;
            }
            else
            {
                folderNa = string.Empty;
                return null;
            }
    
        }
    
        private void uploadFile(string selectedFile)  //THIRD METHOD
        {
            int count = 0;
            string[] FilenameName;
            foreach (string item in opendialog.FileNames)
            {
                FilenameName = item.Split('\\');
                File.Copy(item, folderNa + FilenameName[FilenameName.Length - 1]);
                count++;
            }
            MessageBox.Show(Convert.ToString(count) + " File(s) copied");
        }
    }

    Regards,

    David

    Thursday, April 28, 2016 7:33 AM
  • Thanks, I´ll try it.
    Thursday, April 28, 2016 7:48 AM
  • Hi,

    would be nice if you could close the thread by marking helpful posts as answer in the case you got a solution which works for you.

    Thanks a lot.

    Regards,

    David

    Thursday, April 28, 2016 6:35 PM
  • Hi ,

    You could use the following code like this.

      public class FileUpload
        {
            OpenFileDialog opendialog;
            private string folderNa;
            public string browseDoc(string file)  //FIRST METHOD
            {
    
                opendialog = new OpenFileDialog();
    
                DialogResult result = opendialog.ShowDialog();
    
                if (result == DialogResult.OK)
                {
    
                    file = System.IO.Path.GetFileName(opendialog.FileName);
    
                    return file;
                }
                else
                {
                    return null;
                }
            }

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, April 29, 2016 2:14 AM