473,507 Members | 6,295 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with assigning the result of [string].IndexOf into a const int?

Having a cpp background I'm curious why I can't do the following:

const int openElementPosition = strXmlLine.IndexOf("<RacingNumber");

without getting the following error:

Error 1 The expression being assigned to 'closeElementPosition' must
be constant

If I drop the const then everything is fine, but I don't ever want to
change the value of openElementPosition so it would be better/safer as
a const. Assuming IndexOf returns a scalar integer value, why
shouldn't I be free to store it in a const variable?

Confused,

Duncan.

Feb 6 '07 #1
5 1524
try to specify the field as readonly instead of const - these can be set
once with a non-constant value

"Duncan Smith" <DS********@googlemail.comwrote in message
news:11**********************@v33g2000cwv.googlegr oups.com...
Having a cpp background I'm curious why I can't do the following:

const int openElementPosition = strXmlLine.IndexOf("<RacingNumber");

without getting the following error:

Error 1 The expression being assigned to 'closeElementPosition' must
be constant

If I drop the const then everything is fine, but I don't ever want to
change the value of openElementPosition so it would be better/safer as
a const. Assuming IndexOf returns a scalar integer value, why
shouldn't I be free to store it in a const variable?

Confused,

Duncan.

Feb 6 '07 #2
On Feb 6, 5:06 pm, "Lebesgue" <lebes...@gmail.comwrote:
try to specify the field as readonly instead of const - these can be set
once with a non-constant value
Apparently this only works on member variables and not variables local
to the method. For if I try:

readonly int openElementPosition =
strXmlLine.IndexOf("<RacingNumber");

I get the error:

Error 2 The modifier 'readonly' is not valid for this item

Feb 6 '07 #3
You can not give any specifiers to local variables (in-method) in C#. It's
up to you to know what you are doing in your method.

"Duncan Smith" <DS********@googlemail.comwrote in message
news:11**********************@q2g2000cwa.googlegro ups.com...
On Feb 6, 5:06 pm, "Lebesgue" <lebes...@gmail.comwrote:
>try to specify the field as readonly instead of const - these can be set
once with a non-constant value

Apparently this only works on member variables and not variables local
to the method. For if I try:

readonly int openElementPosition =
strXmlLine.IndexOf("<RacingNumber");

I get the error:

Error 2 The modifier 'readonly' is not valid for this item

Feb 6 '07 #4
On Feb 6, 5:50 pm, "Lebesgue" <lebes...@gmail.comwrote:
You can not give any specifiers to local variables (in-method) in C#. It's
up to you to know what you are doing in your method.

"Duncan Smith" <DSmith1...@googlemail.comwrote in message

news:11**********************@q2g2000cwa.googlegro ups.com...
On Feb 6, 5:06 pm, "Lebesgue" <lebes...@gmail.comwrote:
try to specify the field as readonly instead of const - these can be set
once with a non-constant value
Apparently this only works on member variables and not variables local
to the method. For if I try:
readonly int openElementPosition =
strXmlLine.IndexOf("<RacingNumber");
I get the error:
Error 2 The modifier 'readonly' is not valid for this item
Groan! Remind me to check in again on C# in another three years...

Thanks anyway.

Duncan.

Feb 6 '07 #5
Duncan Smith wrote:
On Feb 6, 5:50 pm, "Lebesgue" <lebes...@gmail.comwrote:
You can not give any specifiers to local variables (in-method) in C#. It's
up to you to know what you are doing in your method.

Groan! Remind me to check in again on C# in another three years...
This feature won't be in C# in three years.

-- Barry

--
http://barrkel.blogspot.com/
Feb 7 '07 #6

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

Similar topics

4
1905
by: MrTang001 | last post by:
How I can fix this problem? I don't know why it alway prompted (first use this function). I have use newDollars, newCents... to access the base class member variable. And the member functions for...
3
11171
by: Senthilraja | last post by:
I am able to compile the following code and get "hello" as ouput everytime I execute it:- #include <stdio.h> int main (void) { char *str; str="hello"; puts(str); return 0;
1
3877
by: tangus via DotNetMonster.com | last post by:
Hello all, I'm really struggling with getting some Active Directory code to work in ASP.NET. Can you please provide assistance? I am executing the following code: Dim enTry As DirectoryEntry =...
7
6991
by: WALDO | last post by:
I wrote a console application that basically consumes arguments and starts other command line apps via System.Process. Let's call it XCompile for now. I wrote a Visual basic add-in that does pretty...
2
4427
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
18
3300
by: len.hartley | last post by:
Hi, I am trying to pop-up a window when the user clicks on an image. The problem is that when the user clicks on the image and the window pops up OK, but the window underneath also proceeds to...
9
2481
by: weidongtom | last post by:
Hi, I've written the code that follows, and I use the function add_word(), it seems to work fine *before* increase_arrays() is called that uses realloc() to allocate more memory to words. But...
1
2863
by: bambr | last post by:
I'm using following code: function cloneRow(tBodyId, sourceRowId) { var obj = document.getElementById(tBodyId); if(obj.tagName != 'TBODY') { for(var i = 0; i < obj.childNodes.length;...
1
3754
by: cakeathon | last post by:
I'm working my way through the accelerated C++ book, and exercise 10-5, & 10-6 have me stuck I have the follwing class in a header file: class String_list { public: String_list(); void...
0
7220
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
7105
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
7308
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
7371
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...
1
7023
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5617
agi2029
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,...
1
5037
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...
0
1534
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.