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

static bool firstTime = true

Hello,
I am trying to do this in C# within a private void method but the value
keeps changing in each call. I tried to use const but i get compiler
errors.

const bool firstTime = true;
if( firstTime == true)
{
firstTime = false; // compiler does not like this
}

I get left hand side of an assignment must be a variable.
Thanks, Please tell me how to reply...
BRAHM

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
4 5409

"Brahm" <br***@ficticous.org> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Hello,
I am trying to do this in C# within a private void method but the value
keeps changing in each call. I tried to use const but i get compiler
errors.

const bool firstTime = true;
if( firstTime == true)
{
firstTime = false; // compiler does not like this
}


C# does not support local static variables (a la C and VB).

Use a private member variable instead.

David
Nov 16 '05 #2
Hi,

It's not clear what you want to do as you do not provide code.
A few hints only
A static variable cannot be declared inside a method

I assume you are coming from C++ ( or C to be more exact ) , what you do in
this case is declare a member variable, it will exist between calls to the
method which is your intended objective.

a const cannot be changed, it cannot appear in the left side of an
assignation. that's why you are getting that error.

You should not use static UNLESS you want that the value of it to persist
during the run of the application, and across all the instances of that
type.
Please post back if you still have doubt about this.
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Brahm" <br***@ficticous.org> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Hello,
I am trying to do this in C# within a private void method but the value
keeps changing in each call. I tried to use const but i get compiler
errors.

const bool firstTime = true;
if( firstTime == true)
{
firstTime = false; // compiler does not like this
}

I get left hand side of an assignment must be a variable.
Thanks, Please tell me how to reply...
BRAHM

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #3
David,
Thanks for your reply. So, i need to have a private variable in my
class initialized to "true". So, the method needs to check it like
this._variablename and if it is true, it does the one thing and sets it
to false so the next time the function is called, an alternative logic
is executed. ok, this should work.
Then const means constant and it is not supposed to change and readonly
means it is not supposed to change except the creator of the object...

Thanks, again.
BRAHM

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #4
>Then const means constant and it is not supposed to change

Yes, as the name "constant" implies......

Also, when checking for a boolean variable to be true, you don't need
to compare it to "true" - just check the variable itself, it's ALREADY
a boolean! (which is only true or false anyway):

if(firstTime)
{
firstTime = false;
}

Marc
================================================== ==============
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
Nov 16 '05 #5

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

Similar topics

1
by: Icosahedron | last post by:
I know that virtual static methods have been addressed, and I'm not really looking for virtual methods, because I don't really need the run time dispatching. What I would like is to have compile...
1
by: Jean-Francois Brault | last post by:
I wrote a crappy class for radian angle management. The class consists of an array of radian values. I put all these things in a class in which all methods are static, so I can access it anywhere...
9
by: Ioannis Vranos | last post by:
Today I came across this code. As far as I know this is undefined behaviour, but I wanted to see what others think: class SomeClass { static bool isDisconnected; public: ~SomeClass()
16
by: Ed Sutton | last post by:
I use a mutex to disallow starting a second application instance. This did not work in a release build until I made it static member of my MainForm class. In a debug build, first instance got...
1
by: Ray Ackley | last post by:
I'm experiencing a threading problem that's really perplexing me. I have a multithreaded application that reads car ECU information that's sent over the serial port by the ECU and received by the...
15
by: Sharon | last post by:
I’m trying to build a generic Publisher-Subscriber that will work over the net, so I’m using the Remoting. I wish that the subscriber user will be notify about the messages sent by the...
2
by: superseed | last post by:
Hi, I'm pretty new to C#, and I'm quite stuck on the following problem. I would like to add to my application a Windows.Form (singleton) on which I could display a message of one of the...
7
by: Brybot | last post by:
Apparently it is not possible for a static class to extend an abstract class? I was wondering how else I might be able to go about my problem here? I have a base class Parent which has a static...
8
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
BarryA
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...
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
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...

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.