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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Using foreach to run thru an arraylist containing a struct

Using foreach to run thru an arraylist containing a struct

Scheduled Pinned Locked Moved C#
databasehelpquestion
3 Posts 2 Posters 1 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.
  • K Offline
    K Offline
    Kyle Wood
    wrote on last edited by
    #1

    I have the following code extract: public arraylist myList = new arraylist(); public struct myDBList { public string strKey; public string strName; public string strDataSetName; } I loop thru a list of databases and add data from each to my list: foreach (database db in dbList) { myDBList.strKey = db.Key.ToString(); myDBList.strActual = db.Name.ToFriendlyString(); myDBList.strDataSetName = db.Path.ToFriendlyString(); myList.Add(myDBList); } this arraylist is then passed to a 2nd form with: Form myLogon = new Form(); myLogon.passedList = myList; In the 2nd form I have declared the same arraylist & struct and I want to loop thru the list and extract the data so I use: ArrayList newList = new ArrayList(); public struct myDBList { public string strKey; public string strName; public string strDataSetName; } string key; public arraylist passedList { get{ newList = value; } } and in the code: foreach( dbList db in newList) { key = db.strKey; . . } While the code compiles, when I come to execute the foreach statement i get the error: InvalidCastException was unhandled - specified cast is not valid What am I doing wrong here ??

    J 1 Reply Last reply
    0
    • K Kyle Wood

      I have the following code extract: public arraylist myList = new arraylist(); public struct myDBList { public string strKey; public string strName; public string strDataSetName; } I loop thru a list of databases and add data from each to my list: foreach (database db in dbList) { myDBList.strKey = db.Key.ToString(); myDBList.strActual = db.Name.ToFriendlyString(); myDBList.strDataSetName = db.Path.ToFriendlyString(); myList.Add(myDBList); } this arraylist is then passed to a 2nd form with: Form myLogon = new Form(); myLogon.passedList = myList; In the 2nd form I have declared the same arraylist & struct and I want to loop thru the list and extract the data so I use: ArrayList newList = new ArrayList(); public struct myDBList { public string strKey; public string strName; public string strDataSetName; } string key; public arraylist passedList { get{ newList = value; } } and in the code: foreach( dbList db in newList) { key = db.strKey; . . } While the code compiles, when I come to execute the foreach statement i get the error: InvalidCastException was unhandled - specified cast is not valid What am I doing wrong here ??

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #2

      Just because the 2 declared structs have the same fields, it doesnt mean you can cast from one to the other. Declare the struct once, in a project that both locations can access.

      K 1 Reply Last reply
      0
      • J J4amieC

        Just because the 2 declared structs have the same fields, it doesnt mean you can cast from one to the other. Declare the struct once, in a project that both locations can access.

        K Offline
        K Offline
        Kyle Wood
        wrote on last edited by
        #3

        Thanks J4amieC. If I'd noticed what the tags were telling me when I hovered over the foreach I'd have seen this for myself. I removed the declaration of the DBList in form 2 and qualified the DBList in the foreach with the form 1 instance and it all works !! A case of not spotting the obvious. :)

        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