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. How to simulate map c++ collection in c#

How to simulate map c++ collection in c#

Scheduled Pinned Locked Moved C#
csharpc++tutorial
4 Posts 2 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.
  • R Offline
    R Offline
    Rostrox
    wrote on last edited by
    #1

    Hello: Look at the next C++ code: 1) Defining map const int cols=15; struct stTot { string strDesc; double fTots[cols]; } myTot; map mpLis; map::iterator it; 2) Loading map int iCnt = 0; myTot.strDesc = (LPCTSTR)some_string; for (int v=0; j

    H 1 Reply Last reply
    0
    • R Rostrox

      Hello: Look at the next C++ code: 1) Defining map const int cols=15; struct stTot { string strDesc; double fTots[cols]; } myTot; map mpLis; map::iterator it; 2) Loading map int iCnt = 0; myTot.strDesc = (LPCTSTR)some_string; for (int v=0; j

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      Please do not re-post - continue the original thread. What you're code is using is an enumerator. The Hashtable and other collections support this, too. See Hashtable.GetEnumerator, which returns an IDictionaryEnumerator. Other collections and lists (those that implement IEnumerable, which ICollection and IList do, so any of their implements do as well) return an IEnumerable. If the Hashtable doesn't work for you, there are plenty of other collections and lists in System.Collections and System.Collections.Specialized that can. If you don't like the provided implementations, write your own ICollection, IList, or IDictionary implementation.

      -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

      R 1 Reply Last reply
      0
      • H Heath Stewart

        Please do not re-post - continue the original thread. What you're code is using is an enumerator. The Hashtable and other collections support this, too. See Hashtable.GetEnumerator, which returns an IDictionaryEnumerator. Other collections and lists (those that implement IEnumerable, which ICollection and IList do, so any of their implements do as well) return an IEnumerable. If the Hashtable doesn't work for you, there are plenty of other collections and lists in System.Collections and System.Collections.Specialized that can. If you don't like the provided implementations, write your own ICollection, IList, or IDictionary implementation.

        -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

        R Offline
        R Offline
        Rostrox
        wrote on last edited by
        #3

        Heath: Many thanks for your answer. The real problem is with the structure: struct stTot { string strDesc; double fTots[cols]; } myTot; I cannot replicate this in C#. I tried many alternatives but I do not really know how to use it (the structure) during the Hashtable loading and recovery of data. A non elegant solution is to join every field that is in the mentioned structure into an string variable and then split them during recovery. Best regards

        H 1 Reply Last reply
        0
        • R Rostrox

          Heath: Many thanks for your answer. The real problem is with the structure: struct stTot { string strDesc; double fTots[cols]; } myTot; I cannot replicate this in C#. I tried many alternatives but I do not really know how to use it (the structure) during the Hashtable loading and recovery of data. A non elegant solution is to join every field that is in the mentioned structure into an string variable and then split them during recovery. Best regards

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          public struct myTot
          {
          public string strDesc;
          public double[] fTots;
          }

          As you can see, there are a few limitations due to either the language specifications or the .NET Framework. As far as putting this in a collection or Hashtable, the default, non-specialized implementations take objects as both keys and values. Ever class or value type (which a struct is) derives from System.Object, so you can put a struct in a Hashtable:

          myTot tot = new myTot();
          tot.strDesc = "blah";
          tot.fTots = new double[] {1.2, 3.4};
          Hashtable t = new Hashtable();
          t["key1"] = tot;
          // ...
          Console.WriteLine(((myTot)t["key1"]).strDesc);

          Read the documentation for the classes in System.Collections about enumerations, collections, lists, and dictionaries. For instance, to enumerate through all the values of a dictionary using the keys, you could do the following:

          // Use "t" from example above for hashtable.
          IEnumerator e = t.Keys.GetEnumerator();
          while (e.MoveNext())
          {
          Console.WriteLine("{0}: {1}", e.Current, t[e.Current]);
          }

          You could also use the foreach keyword for a shortcut:

          foreach (object key in t.Keys)
          {
          Console.WriteLine("{0}: {1}", key, t[key]);
          }

          -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

          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