Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. assigning objects a name programmatically

assigning objects a name programmatically

Scheduled Pinned Locked Moved C#
helpcomquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    usernamed
    wrote on last edited by
    #1

    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!

    G 1 Reply Last reply
    0
    • U usernamed

      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!

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      You can't. There are no dynamic filenames in C#. Put the datatables in a collection, like a HashTable. --- b { font-weight: normal; }

      U 1 Reply Last reply
      0
      • G Guffa

        You can't. There are no dynamic filenames in C#. Put the datatables in a collection, like a HashTable. --- b { font-weight: normal; }

        U Offline
        U Offline
        usernamed
        wrote on last edited by
        #3

        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

        G L 2 Replies Last reply
        0
        • U usernamed

          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

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          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; }

          U 1 Reply Last reply
          0
          • U usernamed

            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

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Yeah, that would work unless the worksheets have the same names - Hashtables do not allow duplicate keys. But you might also use a simple List<Worksheet> regards

            1 Reply Last reply
            0
            • G Guffa

              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; }

              U Offline
              U Offline
              usernamed
              wrote on last edited by
              #6

              Guffa, You are correct. I am an idiot. I have fixed that small oversight, and am hopeful it will be plain(ish) sailing from here on in. Thanks very much for your help!

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups