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. 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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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
|
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
|
by: hilz |
last post by:
Hi all,
What is the equivalent of VARCHAR2 in access?
thanks
hilz
|
by: junky_fellow |
last post by:
Is *ptr++ equivalent to *(ptr++) ?
| |
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
|
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...
|
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...
|
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
|
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
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |