Hello,
Thanks for reviewing my question. I am new to C# and
would like to know why I am receiving an error with the
following code:
struct CARS
{
string sText;
int x;
};
CARS[] array = new CARS[] { {"Honda",0}, {"Toyota",0} };
Error:
Array initializers can only be used in a variable or
field initializer. Try using a new expression instead.
It seems that it doesn't like the enclosing curely braces.
Many Thanks
Peter 5 5602
Try the following:
CARS c1 = new CARS();
CARS c2 = new CARS();
c1.sText = "Honda";
c2.sText = "Toyota";
CARS[] array = new CARS[] {c1,c2};
If you use a Class instead of a struct, you can add constructors with
parameters so you could do something like this:
CARS[] a = new CARS[] {new CARS("Toyota"), new CARS("Honda")};
--
Greetz,
Jan
__________________________________
Read my weblog: http://weblogs.asp.net/jan
"Peter" <pn********@hotmail.com> schreef in bericht
news:08****************************@phx.gbl... Hello,
Thanks for reviewing my question. I am new to C# and would like to know why I am receiving an error with the following code:
struct CARS { string sText; int x; };
CARS[] array = new CARS[] { {"Honda",0}, {"Toyota",0} };
Error: Array initializers can only be used in a variable or field initializer. Try using a new expression instead.
It seems that it doesn't like the enclosing curely braces.
Many Thanks Peter
Peter wrote: CARS[] array = new CARS[] { {"Honda",0}, {"Toyota",0} };
CARS[] array = {new CARS("Honda", 0), new CARS("Toyota", 0)};
--
There are 10 kinds of people. Those who understand binary and those who
don't. http://code.acadx.com
(Pull the pin to reply)
Peter <pn********@hotmail.com> wrote: Thanks for reviewing my question. I am new to C# and would like to know why I am receiving an error with the following code:
struct CARS { string sText; int x; };
CARS[] array = new CARS[] { {"Honda",0}, {"Toyota",0} };
Error: Array initializers can only be used in a variable or field initializer. Try using a new expression instead.
It seems that it doesn't like the enclosing curely braces.
No - you can't initialise it like that. You need to provide a
constructor to your struct which initialises the values within it
appropriately, and then use something like:
CARS[] array = new CARS[]
{
new CARS ("Honda", 0),
new CARS ("Toyota", 0)
};
The constructor might look something like:
public CARS (string sText, int x)
{
this.sText = sText;
this.x = x;
}
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jan Tielens <ja*@no.spam.please.leadit.be> wrote: Try the following: CARS c1 = new CARS(); CARS c2 = new CARS(); c1.sText = "Honda"; c2.sText = "Toyota"; CARS[] array = new CARS[] {c1,c2};
That wouldn't work with the struct as shown, as the members are private
(as they should normally be).
If you use a Class instead of a struct, you can add constructors with parameters so you could do something like this: CARS[] a = new CARS[] {new CARS("Toyota"), new CARS("Honda")};
You can add constructors with parameters to structs as well as classes.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Oh, ic, my mistake (blush)!
Thx for correctiong me Jon!
"Jon Skeet [C# MVP]" <sk***@pobox.com> schreef in bericht
news:MP************************@msnews.microsoft.c om... Jan Tielens <ja*@no.spam.please.leadit.be> wrote: Try the following: CARS c1 = new CARS(); CARS c2 = new CARS(); c1.sText = "Honda"; c2.sText = "Toyota"; CARS[] array = new CARS[] {c1,c2};
That wouldn't work with the struct as shown, as the members are private (as they should normally be).
If you use a Class instead of a struct, you can add constructors with parameters so you could do something like this: CARS[] a = new CARS[] {new CARS("Toyota"), new CARS("Honda")};
You can add constructors with parameters to structs as well as classes.
-- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Geoff Cox |
last post by:
Hello,
I am trying to print out the array values for a second time but get
error on page message?
Thanks
Geoff
<html>
|
by: Alan |
last post by:
hi all,
I want to define a constant length string, say 4
then in a function at some time, I want to set the string to a constant
value, say a
below is my code but it fails
what is the correct...
|
by: Ross A. Finlayson |
last post by:
I'm trying to write some C code, but I want to use C++'s std::vector.
Indeed, if the code is compiled as C++, I want the container to
actually be std::vector, in this case of a collection of value...
|
by: purna chandra |
last post by:
Hello,
I have a simple question.Hoping not to take much of
your valuable time...:-). I am trying to get the data
from a string, and am wondering if I get...
|
by: number1.email |
last post by:
Hello, I have a simple Web Page Questionairre in which questions are
read from a database, and the user can indicate the correct answer via
either a radio input control or a dropdown list. The...
|
by: karan.shashi |
last post by:
Hey all,
I was asked this question in an interview recently:
Suppose you have the method signature
bool MyPairSum(int array, int sum)
the array has all unique values (no repeats), your...
|
by: Armand |
last post by:
Hi Guys,
I have a set of array that I would like to clear and empty out.
Since I am using "Array" not "ArrayList", I have been struggling in
finding the solution which is a simple prob for those...
|
by: AndersWang |
last post by:
Hi,
dose anybody here explain to me why memset would be faster than a
simple loop. I doubt about it!
In an int array scenario:
int array;
for(int i=0;i<10;i++) //ten loops
|
by: sam |
last post by:
hI,
I am little confused here
See
i have
int wordlen=10;
when int s is array
s++;
whats the meaning of this
|
by: Ronald Raygun |
last post by:
I want to be able to randomly select the following from an array:
1). An image
2). A piece of text (name of tge image)
3). A piece of text (description of the image)
I want to be able to...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: 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,...
|
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...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |