473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Dynamic Field in C#?

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

userdata.('username').electrocardiogram = 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
Jun 27 '08 #1
4 3256
On Wed, 30 Apr 2008 14:48:40 -0700, Hoi Wong <wo*****@stanford.eduwrote:
In new versions of MATLAB, I can do dynamic field like

userdata.('username').electrocardiogram = 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
Jun 27 '08 #2
Hoi Wong <wo*****@stanford.eduwrote:
In new versions of MATLAB, I can do dynamic field like

userdata.('username').electrocardiogram = 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"].Electrocardiogram = 4;

(where userData is defined appropriately, of course).

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #3

"Hoi Wong" <wo*****@stanford.eduwrote in message
news:fv**********@news.Stanford.EDU...
In new versions of MATLAB, I can do dynamic field like

userdata.('username').electrocardiogram = 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 --
"displayname". 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.

Jun 27 '08 #4
Sweet. Thanks for the pointer (no pun intended)!
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Hoi Wong <wo*****@stanford.eduwrote:
>In new versions of MATLAB, I can do dynamic field like

userdata.('username').electrocardiogram = 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"].Electrocardiogram = 4;

(where userData is defined appropriately, of course).

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

Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Jack | last post by:
Hi, I am trying to test a sql statement in Access which gives me the error as stated in the heading. The sql statement is built as a part of asp login verification, where the userid and password...
1
by: Simon Gare | last post by:
Hi I need to compare a dynamic field in an asp page to a field in another table, if there is no match then i would like to chane the row colour ( see code below). The problem Im having is...
1
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to...
1
by: Giloosh | last post by:
Hello i have been trying to figure out this problem for a very long time now, and thought maybe its worth asking for some help. I want to figure out if there is a way to create a field in a...
3
by: NateDawg | last post by:
I'm reposting this. I'm kinda in a bind untill i get this figured out, so if anyone has some input it would sure help me out. Ok, I’ve noticed a few gridview problems floating around the forum....
2
by: Luis Arvayo | last post by:
Hi, In c#, I need to dynamically create types at runtime that will consist of the following: - inherits from a given interface - will have a constructor with an int argument
4
by: Bongard | last post by:
I have a dynamic range that I would like to use as a linked table into Access. The problem is that Access doesn't seem to want to to recognize the dynamic range when you click on "show named...
20
by: sirsnorklingtayo | last post by:
hi guys please help about Linked List, I'm having trouble freeing the allocated memory of a single linked list node with a dynamic char* fields, it doesn't freed up if I use the FREE()...
1
by: jmartmem | last post by:
Greetings, I have a nagging problem with client-side dynamic dependent list boxes that perhaps someone can help me troubleshoot. I have a form with a series of dynamic dependent list boxes....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.