473,769 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Equivalent to table-driven code

Hi.

In C/C++, I'm used to using a table-driven approach for some problems to
avoid large case statements. For example (off the top of my head, may
contain syntax errors and error checking excluded):

typedef struct
{
const char *name;
unsigned int id;

} ANIMALMAPPING;

static ANIMALMAPPING animalIdMapping [] =
{
{"frog", 174},
{"cow", 206},
{"bird", 92}
};

int GetAnimalIdFrom Name (const char *animalName)
{
for (int animal = 0; animal < ARRAYSIZE (animalIdMappin g); animal++)
if (stricmp (animalName, animalIdMapping [animal].name) == 0)
etc...
}

In C#, I seem to have trouble defining the animalIdMapping array - I've got:

private AnimalEffectMap ping [] animalEffectMap ping
= new AnimalEffectMap ping [3]
{
{"frog", 174},
{"cow", 206},
{"bird", 92}
};

but I get the following compile error:

error CS0623: Array initializers can only be used in a variable or field
initializer. Try using a new expression instead.

But when I try putting 'new AnimalEffectMap ping' in front of each line
in the table, the compiler thinks I'm trying to make an array out of
each line.

What do I need to change to get this working, and does C#/.Net offer a
better approach to solving this kind of problem than table-driven code?

Thanks.
--
Remove the blah's from my email address to reply.
Feb 17 '07 #1
2 3580
Richard Drysdall <dr************ **@fastmail.fmw rote:
In C#, I seem to have trouble defining the animalIdMapping array - I've got:

private AnimalEffectMap ping [] animalEffectMap ping
= new AnimalEffectMap ping [3]
{
{"frog", 174},
{"cow", 206},
{"bird", 92}
};

but I get the following compile error:

error CS0623: Array initializers can only be used in a variable or field
initializer. Try using a new expression instead.
You need to change each of the lines inside the array to something
like:

new AnimalEffectMap ping ("frog", 174),

Note the round brackets instead of braces. You're calling a constructor
- there's no equivalent of the sort of "automatic" struct
initialization in C.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 17 '07 #2
Jon Skeet [C# MVP] wrote:
Richard Drysdall <dr************ **@fastmail.fmw rote:
>In C#, I seem to have trouble defining the animalIdMapping array - I've got:

private AnimalEffectMap ping [] animalEffectMap ping
= new AnimalEffectMap ping [3]
{
{"frog", 174},
{"cow", 206},
{"bird", 92}
};

but I get the following compile error:

error CS0623: Array initializers can only be used in a variable or field
initializer. Try using a new expression instead.

You need to change each of the lines inside the array to something
like:

new AnimalEffectMap ping ("frog", 174),

Note the round brackets instead of braces. You're calling a constructor
- there's no equivalent of the sort of "automatic" struct
initialization in C.
Additionally, you might to look into the HashTable or Dictionary<>
classes, that offer far quicker searching than looping through all the
elements.

--
Göran Andersson
_____
http://www.guffa.com
Feb 17 '07 #3

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

Similar topics

7
4793
by: KingGreg | last post by:
All, Oracle 9i provides a "USING" clause option for inner joins, that allows me to say: SELECT * FROM TBL1 JOIN TBL2 USING KeyColumn assuming KeyColumn is in both TBL1 and TBL2. This is HIGHLY desirable for our software make use of, but we also support SQL Server. There is no USING option available, and
1
3816
by: Vannela | last post by:
Is there any equivalent control in .NET for the Power builder DataWindow control? I am explaining the Datawindow architecture to some extent. Power Builder DataWindow Control has got different presentation styles and different data sources. Presentation styles like tabular format , graph format, grid format, freeform format, Composite format(somthing
3
58253
by: effendi | last post by:
Hi Can any tell me what is the javascript equivalent of CSS border? I would like to change the border of my cell when it is set on focus. I have tried onFocus="style.border='3px'" but it is not working. Thanks
14
10873
by: hilz | last post by:
Hi all, What is the equivalent of VARCHAR2 in access? thanks hilz
27
6694
by: junky_fellow | last post by:
Is *ptr++ equivalent to *(ptr++) ?
2
3509
by: Sunkam Santhoshi | last post by:
Is there any equivalent control in .NET for the Power builder DataWindow control? I am explaining the Datawindow architecture to some extent. Power Builder DataWindow Control has got different presentation styles and different data sources. Presentation styles like tabular format , graph format, grid format, freeform format, Composite format(somthing
0
2153
by: donnet | last post by:
To those familiar with Java beans, question on how to do with ASP.NET: I come from J2EE and starting to learn ASP.NET and C#. I want to find a way to display a web form filled with data taken from a SQL table. I need to repeat the layout of my web form in one single page, each time with data taken from the SQL table row. I don't want to put those data rows into a datagrid because I have my own form that I want to populate with a specific...
3
3515
by: Fred Morrison | last post by:
Does DB2 have anything equivalent to Oracle's PRAGMA AUTONOMOUS_TRANSACTION that allows a stored procedure to BEGIN, COMMIT or ROLLBACK a totally independent transaction while possibly (but not necessarily) nested inside a different ("parent") transaction? I need to write (INSERT) data to an EVENT_LOG table and COMMIT that INSERT without COMMIT'ing any other work in progress in the transaction my usp_write_event_log might be called from...
13
5666
by: Jim C | last post by:
Is there an equivalent in mssql to SQL Anywhere's GET_IDENTITY which reserves the next autoinc value for a table? Yes I know about @@Identity and SCOPE_IDENTITY. I need to get the next autoinc value _before_ I insert the record due to the way the existing application works. I've seen DBCC CHECKIDENT mentioned but that seems kludgy to me. TIA, Jim
2
2748
by: Dan | last post by:
Hello, all! New here, so please forgive if this has been answered before. I use MySQL most often, and can use the commands: show tables in ; show columns in ; which lists all the tables in the specified database, and columns/ fields in the specified table. Is there an SQL equivalent for use in
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.