assigning objects a name programmatically
-
Hi, I'm hoping someone can help me with a problem I'm having with an app that reads in an Excel spreadsheet. For each worksheet in the spreadsheet I want to create a DataTable that has the same name as the Excel worksheet. My code currently looks like this:
foreach (Microsoft.Office.Interop.Excel.Worksheet ws in wrkBook.Worksheets) { if (ws.Name != null) { this.lstSheets.Items.Add(ws.Name); strWorksheet = ws.Name.ToString(); System.Data.DataTable ws.Name = new System.Data.DataTable(); } }
This doesn't work as it thinks I'm trying to reassign an existing object, rather than create a new object with the name of an existing object's property. Can anyone explain how I can create as many datatables as exists in the spreadsheet file I open, and programmatically give each DataTable the same name as the worksheet from the spreadsheet? Any assistance would be gratefully recieved! -
Hi, I'm hoping someone can help me with a problem I'm having with an app that reads in an Excel spreadsheet. For each worksheet in the spreadsheet I want to create a DataTable that has the same name as the Excel worksheet. My code currently looks like this:
foreach (Microsoft.Office.Interop.Excel.Worksheet ws in wrkBook.Worksheets) { if (ws.Name != null) { this.lstSheets.Items.Add(ws.Name); strWorksheet = ws.Name.ToString(); System.Data.DataTable ws.Name = new System.Data.DataTable(); } }
This doesn't work as it thinks I'm trying to reassign an existing object, rather than create a new object with the name of an existing object's property. Can anyone explain how I can create as many datatables as exists in the spreadsheet file I open, and programmatically give each DataTable the same name as the worksheet from the spreadsheet? Any assistance would be gratefully recieved! -
You can't. There are no dynamic filenames in C#. Put the datatables in a collection, like a HashTable. --- b { font-weight: normal; }
Guffa, thanks for your response. I'm new to C# and I've not used HashTables before, could I just clarify how this would work in my example? From looking at what I can find on HashTables, would my code look like this?
System.Collections.Hashtable h = new Hashtable(); foreach (Microsoft.Office.Interop.Excel.Worksheet ws in wrkBook.Worksheets) { if (ws.Name != null) { this.lstSheets.Items.Add(ws.Name); strWorksheet = ws.Name.ToString(); h.Add(strWorksheet, ws); } }
For anyone else reading this post looking for further information, I found a short tutorial on HashTables here: http://abstractvb.com/code.asp?A=1026 -
Guffa, thanks for your response. I'm new to C# and I've not used HashTables before, could I just clarify how this would work in my example? From looking at what I can find on HashTables, would my code look like this?
System.Collections.Hashtable h = new Hashtable(); foreach (Microsoft.Office.Interop.Excel.Worksheet ws in wrkBook.Worksheets) { if (ws.Name != null) { this.lstSheets.Items.Add(ws.Name); strWorksheet = ws.Name.ToString(); h.Add(strWorksheet, ws); } }
For anyone else reading this post looking for further information, I found a short tutorial on HashTables here: http://abstractvb.com/code.asp?A=1026 -
Guffa, thanks for your response. I'm new to C# and I've not used HashTables before, could I just clarify how this would work in my example? From looking at what I can find on HashTables, would my code look like this?
System.Collections.Hashtable h = new Hashtable(); foreach (Microsoft.Office.Interop.Excel.Worksheet ws in wrkBook.Worksheets) { if (ws.Name != null) { this.lstSheets.Items.Add(ws.Name); strWorksheet = ws.Name.ToString(); h.Add(strWorksheet, ws); } }
For anyone else reading this post looking for further information, I found a short tutorial on HashTables here: http://abstractvb.com/code.asp?A=1026 -
Now you are putting the work sheets in the hash table. Didn't you say that you wanted to create data tables? --- b { font-weight: normal; }