Dynamic Field in C#?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hoi Wong

    Dynamic Field in C#?

    In new versions of MATLAB, I can do dynamic field like

    userdata.('user name').electroc ardiogram = 4,

    where 'username' can be replaced by any string variable. This made my
    software design much neater. Does anybody know if C# support that? If so,
    what's the syntax?

    Thanks in advance.

    Cheers,
    Hoi


  • Peter Duniho

    #2
    Re: Dynamic Field in C#?

    On Wed, 30 Apr 2008 14:48:40 -0700, Hoi Wong <wonghoi@stanfo rd.eduwrote:
    In new versions of MATLAB, I can do dynamic field like
    >
    userdata.('user name').electroc ardiogram = 4,
    >
    where 'username' can be replaced by any string variable. This made my
    software design much neater. Does anybody know if C# support that? If so,
    what's the syntax?
    The language doesn't support it directly, no. But you can easily
    implement something using a Dictionary or indexer that does something
    similar, depending on your specific need.

    Pete

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Dynamic Field in C#?

      Hoi Wong <wonghoi@stanfo rd.eduwrote:
      In new versions of MATLAB, I can do dynamic field like
      >
      userdata.('user name').electroc ardiogram = 4,
      >
      where 'username' can be replaced by any string variable. This made my
      software design much neater. Does anybody know if C# support that? If so,
      what's the syntax?
      You need to use a Dictionary<TKey ,TValue>:

      userData["username"].Electrocardiog ram = 4;

      (where userData is defined appropriately, of course).

      --
      Jon Skeet - <skeet@pobox.co m>
      Web site: http://www.pobox.com/~skeet
      Blog: http://www.msmvps.com/jon.skeet
      C# in Depth: http://csharpindepth.com

      Comment

      • Mr. Arnold

        #4
        Re: Dynamic Field in C#?


        "Hoi Wong" <wonghoi@stanfo rd.eduwrote in message
        news:fvapg1$oh8 $1@news.Stanfor d.EDU...
        In new versions of MATLAB, I can do dynamic field like
        >
        userdata.('user name').electroc ardiogram = 4,
        >
        where 'username' can be replaced by any string variable. This made my
        software design much neater. Does anybody know if C# support that? If so,
        what's the syntax?
        >
        I know you can do this with ADO.Net and C# when addressing fields in a
        datatable dynamically addressing fields in the table by name. As a matter of
        fact, I was taking a column/field name from a another table that
        represented the fieldname in the table I wanted to access.

        This just off the top of my head as to how it worked.

        string fieldname = arow[brow["displayfldname "].Tostring()];

        The field in arow was accessed via the name of the field that was in brow --
        "displaynam e". Arow held the data that brow accessed to build dynamic
        screens based on the tblScreenFields access into tblScreenData.

        The ToString() was the key to getting it to work.

        So, I don't see why you couldn't do the same in your example of addressing a
        field dynamically, hopefully, and you can try it.

        Comment

        • Hoi Wong

          #5
          Re: Dynamic Field in C#?

          Sweet. Thanks for the pointer (no pun intended)!


          "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
          news:MPG.2282ff 6c41693bbec9a@m snews.microsoft .com...
          Hoi Wong <wonghoi@stanfo rd.eduwrote:
          >In new versions of MATLAB, I can do dynamic field like
          >>
          >userdata.('use rname').electro cardiogram = 4,
          >>
          >where 'username' can be replaced by any string variable. This made my
          >software design much neater. Does anybody know if C# support that? If so,
          >what's the syntax?
          >
          You need to use a Dictionary<TKey ,TValue>:
          >
          userData["username"].Electrocardiog ram = 4;
          >
          (where userData is defined appropriately, of course).
          >
          --
          Jon Skeet - <skeet@pobox.co m>
          Web site: http://www.pobox.com/~skeet
          Blog: http://www.msmvps.com/jon.skeet
          C# in Depth: http://csharpindepth.com

          Comment

          Working...