473,394 Members | 1,869 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,394 software developers and data experts.

initializing an array in C#

Hello everyone,

I want to put values inside array in if statement. so I declared an array

string[] ar = new ar[5];
if (x=="10)
ar={"c", "d", "e","f"}
else
ar={"a", "b", "c","d"}

this gives me an error Invalid expression term "{"

can anyone tell me what am I doing wrong.

Jun 14 '07 #1
3 8950
On Thu, 14 Jun 2007 11:10:02 -0700, Vinki
<Vi***@discussions.microsoft.comwrote:
[...]
if (x=="10)
ar={"c", "d", "e","f"}
else
ar={"a", "b", "c","d"}

this gives me an error Invalid expression term "{"

can anyone tell me what am I doing wrong.
Try:

if (x=="10)
ar= new string[] {"c", "d", "e","f"};
else
ar=new string[] {"a", "b", "c","d"};

Note the addition of the "new" operator, along with semicolons.

Pete
Jun 14 '07 #2
Vinki,

This syntax isn't available until C# 3.0. The only way you will get
this to work is if you are using VS.NET Orcas (which is currently in beta
right now).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Vinki" <Vi***@discussions.microsoft.comwrote in message
news:BF**********************************@microsof t.com...
Hello everyone,

I want to put values inside array in if statement. so I declared an array

string[] ar = new ar[5];
if (x=="10)
ar={"c", "d", "e","f"}
else
ar={"a", "b", "c","d"}

this gives me an error Invalid expression term "{"

can anyone tell me what am I doing wrong.

Jun 14 '07 #3


"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:OZ**************@TK2MSFTNGP03.phx.gbl...
Vinki,

This syntax isn't available until C# 3.0. The only way you will get
this to work is if you are using VS.NET Orcas (which is currently in beta
right now).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Vinki" <Vi***@discussions.microsoft.comwrote in message
news:BF**********************************@microsof t.com...
>Hello everyone,

I want to put values inside array in if statement. so I declared an
array

string[] ar = new ar[5];
if (x=="10)
ar={"c", "d", "e","f"}
else
ar={"a", "b", "c","d"}

this gives me an error Invalid expression term "{"

can anyone tell me what am I doing wrong.

Yeah, but using the new keyword, it will work in any version :)

ar = new string[] { "c", "d", "e", "f" };

Also, there is an error in the if statement...missing a quote:

if (x == "10") ...

Lastly, these are all chars in the example so the OP may want to declare the
array as a character array instead of string...:

char[] ar;
if (x == "10") {
ar = new char[] { 'c', 'd', 'e', 'f' };
} else {
ar = new char[] { 'a', 'b', 'd', 'e' };
}

HTH,
Mythran
Jun 14 '07 #4

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

Similar topics

5
by: pmatos | last post by:
Hi all, I have a vector of vector of ints, I could use C approach by using int but I think C++ vector<vector<int> > would be easier to manage. So I have a function which creates and initializes...
13
by: simondex | last post by:
Hi, Everyone! Does anyone know how to initialize an int array with a non-zero number? Thank You Very Much. Truly Yours, Simon Dexter
10
by: Bart Goeman | last post by:
Hi, I have a question about how to put redundant information in data structures, initialized at compile time. This is often necessary for performance reasons and can't be done at run time (data...
3
by: farseer | last post by:
If i have an array of a certain type, is there away of initializing with without knowing it's type? for example (forgive me if some of this is syntactically incorrect) if i have a procedure...
4
by: jayharris | last post by:
I'm having a ton of trouble initializing a multi-dimensional array inside a constructor, largely because I don't know the size of the array until runtime. I have a class that looks like this: ...
7
by: nk | last post by:
Hi, I'm a newbie on this language. I would be very happy if you help me about the following issue: The code below, reads some names(strings), stores them, and stores the addresses in the pointer...
11
by: sg71.cherub | last post by:
Hi All, I have encapsulate CvMat of OpenCV into my own matrix class as the following: class CVMatrix { //== Fields private: unsigned m_Width;
13
by: WaterWalk | last post by:
Hello. When I consult the ISO C++ standard, I notice that in paragraph 3.6.2.1, the standard states: "Objects with static storage duration shall be zero-initialized before any other...
6
by: Jai Prabhu | last post by:
Hi All, Consider the following piece of code: void func (void) { static unsigned char arr = "\x00\xAA\xBB"; fprintf (stderr, "0x%x\n", arr); fprintf (stderr, "0x%x\n", arr);
4
by: Peskov Dmitry | last post by:
class simple_class { int data; public: simple_class() {data=10;}; simple_class(int val) : data(val){} }; int main() {
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
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...
0
Oralloy
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,...
0
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...

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.