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

declaring a viarable

If you define a variable when you declare it, does that mean that you can
never change the variable?
int myVar = 5;

Where as maybe
int myVar;
myVar = 5;

and later on somewhere I could then say
myVar =6;
?

Does that make sense.
Nov 15 '05 #1
3 1237
Robert,

no, you are free to change the variable no matter how you declare it.

Only a variable declared as a const cannot be changed, e.g.

const int myVar = 5; // this cannot be changed

Also, have a look at the keyword 'static' in the documentation.

Steven.

---
Steven Wood

"Robert Blackwell" <robbieatwowcentraldotcom> wrote in message
news:u2**************@TK2MSFTNGP10.phx.gbl...
If you define a variable when you declare it, does that mean that you can
never change the variable?
int myVar = 5;

Where as maybe
int myVar;
myVar = 5;

and later on somewhere I could then say
myVar =6;
?

Does that make sense.

Nov 15 '05 #2
Thanks
Nov 15 '05 #3

"Steven Wood" <st*********@hotmail.com> wrote in message
news:%2*****************@TK2MSFTNGP11.phx.gbl...
Robert,

no, you are free to change the variable no matter how you declare it.

Only a variable declared as a const cannot be changed, e.g.

const int myVar = 5; // this cannot be changed

Also, have a look at the keyword 'static' in the documentation.


Steven, I'm sure you know what static means... but mentioning it here could
be confusing.

For Robert, static has nothing to do with being able to change or not change
a variable. It simply means that a variable (or method or whatever element
it modifies) belongs to a particular instance of a class or is shared
between all objects. Sometimes I think the word "shared" in VB.NET makes
more sense.

But since static is sometimes confused with const by beginners, it is a good
idea to look up both and make sure you understand these keywords as you're
learning the language. I'd suggest readonly too, while you are at it.

Here's the full list of keywords for C#:

http://msdn.microsoft.com/library/de...eywords_PG.asp
--
Mike Mayer
http://www.mag37.com/csharp/
mi**@mag37.com

Nov 15 '05 #4

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

Similar topics

11
by: arekkusu | last post by:
Hello, I have the following problem: when declaring hex data in C, you typically do something like: const char = {0xde, 0xad, 0xbe, 0xef, 0x01, 0x02, 0x03, 0x04 ... }; This is quite verbose...
1
by: Maarten Terlingen | last post by:
Sample: ClassA: ClassB ClassB: ClassC ClassA x = new Class C I want to know the declaring type of C. x.GetType() returns C x.GetType().BaseType returns B x.GetType().BaseType.BaseType...
3
by: mark | last post by:
When I declare an array as double(,) then try to use it I get an error: "Object reference not set to an instance of an object." I have found that I can redim the array and all is well. Is my...
1
by: John Dann | last post by:
Is there a way of declaring an array of a structure where the structure has a constructor? So if I had a structure say Friend MyStructure prop1 as integer prop2 as string sub New (p1 as...
5
by: param | last post by:
Declaring struct as static is creating problem with newer version of CC compiler 5.7 in solaris. e.g. static struct new_str { int a; int b; };
4
by: GnG | last post by:
Hello all, Someone posted a similar question a while ago but there was no response. Does anyone know the answer? I have a managed C++ DLL which is used by a C# project. In that DLL, I have...
6
by: Mark A. Sam | last post by:
Hello, I am using Visual Web Developer 2005 Express. I want to declare a varible, using Visual Basic as the language and can't get anywhere. For example Public Test1 as String I'll get en...
8
by: rendle | last post by:
I have a MSIL/performance question: Is there any difference between declaring a variable once and assigning to it multiple times, and declaring and assigning multiple times? For example: //...
8
by: SM | last post by:
I've always wonder if there is diference when declaring and initializing a varible inside/outside a loop. What's a better practice? Declaring and initializing variables inside a loop routine,...
8
by: nguillot | last post by:
Hello. If I have the following classes: class B {}; typedef B tB; if A is: class A
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.