user2405778
user2405778

Reputation: 497

Type or namespace definition, or end-of-file expected error with same number of brackets

Hi, I am getting this mentioned error after converting a VB program to C#, but I cant seem to find the missing right bracket. I counted them multiple times and it seems to me that I have an equal amount, but I also have been looking for a bit. Here is the code I am working with

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;
using Microsoft.Office.Interop.Excel;
using System.Text.RegularExpressions;

public class Class1
{
public Class1()
{
    private void exportData(DataTable dt, string workpath, string tabName, bool isStmt = false)
    {
        if (dt.Rows.Count > 0 && IO.File.Exists(workpath)) 
        {
            //export data to excel
            exportExcel = true;

            Microsoft.Office.Interop.Excel.Workbook NewWorkbook = default(Microsoft.Office.Interop.Excel.Workbook);
            Microsoft.Office.Interop.Excel.Style style = default(Microsoft.Office.Interop.Excel.Style);
            Microsoft.Office.Interop.Excel.Worksheet CurrentSheet = new Microsoft.Office.Interop.Excel.Worksheet();
            Microsoft.Office.Interop.Excel.Worksheet totalWorkSheets = default(Microsoft.Office.Interop.Excel.Worksheet);
            string excelSheetNames = null;

            if (IO.File.Exists(workpath)) 
            {
                //file exists
                NewWorkbook = Form1.newExcel.Workbooks.Open(workpath);
            } 
            else 
            {
                //file does not exist; create new file
                NewWorkbook = Form1.newExcel.Workbooks.Add();
                style = NewWorkbook.Styles.Add("Style1");
                style.NumberFormat = "@";
            }

            //get all tab names
            foreach (var totalWorkSheets in NewWorkbook.Worksheets) 
            {
                excelSheetNames += totalWorkSheets.Name;
            }

            //does tab exist?
            if (Strings.InStr(excelSheetNames, tabName) > 0) 
            {
                //write to tab
                CurrentSheet = NewWorkbook.Worksheets(tabName);
            } 
            else 
            {
                //create new tab
                CurrentSheet = NewWorkbook.Worksheets.Add();
                CurrentSheet.Name = tabName;
            }

            CurrentSheet.Activate();

            int currentRow = 1;

            //LOAN TYPE
            //ACCOUNT NUMBER
            //CUSTOMER NAME
            //SSN
            //OPEN DATE
            //CHARGE OFF DATE
            //PLACEMENT/SALE DATE
            //RANGE FROM
            //RANGE TO
            //TOTAL REQUESTED

            //FILL EXCEL DATA
            foreach (DataRow row in dt.Rows) 
            {
                currentRow = currentRow + 1;

                CurrentSheet.Cells(currentRow, 1) = row.Item("BUCKET").ToString;
                //LOAN TYPE
                CurrentSheet.Cells(currentRow, 2) = row.Item("ACCOUNT NUMBER").ToString;
                //ACCOUNT NUMBER
                CurrentSheet.Cells(currentRow, 3) = row.Item("CUSTOMER NAME").ToString;
                //CUSTOMER NAME
                CurrentSheet.Cells(currentRow, 4) = row.Item("SS_#").ToString().Replace("-", "").ToString;
                //SSN
                CurrentSheet.Cells(currentRow, 5) = row.Item("OPEN DATE").ToString;
                //OPEN DATE

                if (isStmt) 
                {
                    CurrentSheet.Cells(currentRow, 6) = row.Item("CHARGE OFF DATE").ToString;
                    //CHGOFF DATE
                } 
                else 
                {
                    CurrentSheet.Cells(currentRow, 6) = row.Item("CHGOFF_DATE").ToString;
                    //CHGOFF DATE
                }

                CurrentSheet.Cells(currentRow, 7) = row.Item("SALE DATE").ToString;
                //SALE DATE

                if (isStmt) 
                {
                    CurrentSheet.Cells(currentRow, 8) = row.Item("STATEMENT FROM").ToString;
                    //STMT FROM
                    CurrentSheet.Cells(currentRow, 9) = row.Item("STATEMENT TO").ToString;
                    //STMT TO
                }

                CurrentSheet.Cells(currentRow, 10) = row.Item("ITEMS_REQUESTED").ToString;
            }
            //END EXCEL DATA STREAM

            if (IO.File.Exists(workpath)) 
            {
                NewWorkbook.Save();
            } 
            else 
            {
                NewWorkbook.SaveAs(workpath);
            }
            NewWorkbook.Close();

            System.Runtime.InteropServices.Marshal.ReleaseComObject(CurrentSheet);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(NewWorkbook);

            CurrentSheet = null;
            NewWorkbook = null;

        }
    }
}
}

Upvotes: 1

Views: 7347

Answers (2)

itsme86
itsme86

Reputation: 19486

This is wrong:

public Class1()
{
    private void exportData(DataTable dt, string workpath, string tabName, bool isStmt = false)
    {

It looks like you're try to defined a method within a constructor. They should be separate:

public Class1()
{
    // Do constructor stuff
}

private void exportData(DataTable dt, string workpath, string tabName, bool isStmt = false)
{
     // This method's stuff
}

Upvotes: 3

Reed Copsey
Reed Copsey

Reputation: 564323

You put the methods inside your constructor:

public class Class1
{
public Class1()
{
// Missing close brace here!!!

    private void exportData(DataTable dt, string workpath, string tabName, bool isStmt = false)
    {

This also means you'll end up needing to remove one extra brace from the end.

Upvotes: 7

Related Questions