none
how can i pass my string from 1 private void to another? RRS feed

  • Question

  • hi guys. help will be appreciated. 

                           

    private void Browsebtn_Click(object sender, EventArgs e)
            {

                int size = -1;
                DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
                FilePathLabel1.Text = openFileDialog1.FileName;

                Excel.Application excelApp = new Excel.Application();
                excelApp.Visible = false;
                excelApp.Workbooks.Open(FilePathLabel1.Text);

                string file1 = openFileDialog1.FileName;



                try
                {
                    string text1 = File.ReadAllText(file1);
                    size = text1.Length;
                }
                catch (IOException)
                {
                }                 
            }

    private void Startbtn_Click(object sender, EventArgs e)
            {
                
                Excel.Application app = new Excel.Application();
                Excel.Workbook curWorkBook = null;
                Excel.Workbook destWorkbook = null;
                Excel.Worksheet workSheet = null;
                Excel.Worksheet newWorksheet = null;
                Object defaultArg = Type.Missing;
                          
                try
                {
                    // Copy the source sheet
                    curWorkBook = app.Workbooks.Open("file1");
                    workSheet = (Excel.Worksheet)curWorkBook.Sheets[1];
                    workSheet.UsedRange.Copy(defaultArg);

    system say file1.xls cant be found. 

    Thursday, January 28, 2016 7:55 AM

Answers

  • Hi ryan,

    The scope of variables defined in a function is in the function. That means, we are not able refer to these variables out of the function which define them.

    If you want to share the variables between the functions, we need to define the variables at class-level.

    And about the error "say file1.xls cant be found", it is not relative to the string you defined where. This issue caused the Excel application can't find the file1.xls. By default, the Excel application will try to find the file at "Default local file location" which you can find this option via Excel->Option->Save.

    If the "file1.xls" is not loaded at the default location, please try to open the spreadsheets with full path of the file.

    Regards & Fei


    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, January 29, 2016 3:14 AM
    Moderator