473,387 Members | 1,590 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.

constant vs. read only property

Emm, being a newbie to C#, can someone explain to me what is the difference
between a constant (as a Class member) and a read only property??

Also I simply cannot grasp the concept of static methods, can someone please
explain it to me in human language??
Nov 16 '05 #1
8 19172
Davmp ^srisson wrote:
Emm, being a newbie to C#, can someone explain to me what is the
difference between a constant (as a Class member) and a read only
property??

Also I simply cannot grasp the concept of static methods, can someone
please explain it to me in human language??


const means it's not going to change, read-only it might chane but
_you_ cannot change it.

static method is a method which doesn't require/care for the actual
object. It's pretty much like a global function - you might think of it
as the global function but in your class namespace.
Nov 16 '05 #2
"Jack Hanebach" <ja*****@spam.hanebach.net> wrote in message
news:ex**************@TK2MSFTNGP14.phx.gbl...
const means it's not going to change, read-only it might chane but
_you_ cannot change it.


You being the person using the class, if you are the person creating the
class then you can change it. :-)

Michael Culley
Nov 16 '05 #3
Michael Culley wrote:
"Jack Hanebach" <ja*****@spam.hanebach.net> wrote in message
news:ex**************@TK2MSFTNGP14.phx.gbl...
const means it's not going to change, read-only it might chane but
_you_ cannot change it.

You being the person using the class, if you are the person creating the
class then you can change it. :-)

An object can change the value of its own readonly properties but
anything using that object cannot.
JB
Nov 16 '05 #4
'readonly' variables can only be assigned a value in the declaration
or in the variable's class constructor.

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
Nov 16 '05 #5
Emmm... First there is a difference between const and readonly and it
has to do with versioning or whether the value is updated at runtime or
only if you recompile the _caller_.
http://www.geocities.com/jeff_louie/OOP/oop5.htm

6) Const is Hard Coded Into the Caller and not Version-able
I find this one a bit weird. A const field value is hard coded into the
caller for optimization so that the value is not updated until you
recompile the caller. If you want to version-able read only field
declare it readonly. Finally, you can provide a get only Property like
this:

public string ModelName
{
get
{
return modelName; // danger, return ModelName --> stack
overflow!
}
}

As for static.
http://www.geocities.com/jeff_louie/OOP/oop4.htm

What Is a Static Field or Method?
Let's change the question. When is a field or method not part of an
object? Answer: when it is part of the class! Remember, an object is an
instance of a class and each object exists in a separate space in
memory. It is possible to access class fields and class methods without
creating an instance of a class using the "static" key word. Declaring a
field or method with the static key word, tells the compiler that the
field or method is associated with the class itself, not with instances
of the class. In a sense, static or "class" fields and methods are
global variables and methods that you can touch using the class name. If
you think of a class as a blueprint used to create objects, then you can
think of static fields and methods are being part of the blueprint
itself. There is only one copy of the static fields and methods in
memory, shared by all instances of the class.

Hope that helps,
Jeff
Emm, being a newbie to C#, can someone explain to me what is the

difference between a constant (as a Class member) and a read only
property??<

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #6
Hi,
Actually readonly values can be set only in the constructor of the type
decalring the readonly field. Once the constuctor exits no one can change
the readonly field. Constants on the other hand has no field behind them.
Their value is stored in the assembly metadata and that data is used by the
compiler when generating the code. The values of the constant are hardcoded
once the program is built. The latter might introduce versioning problems
that's why constants should be use for something that doesn't change e.g.
WM_* constants.
The other difference that I feel like needs to be mentioned is that as long
as readonly values can be of any type constants are limited only to the
primitive .NET types.
--
HTH
Stoitcho Goutsev (100) [C# MVP]
"The Last Gunslinger" <jb******@yahoo.com> wrote in message
news:lT*******************@news-server.bigpond.net.au...
Michael Culley wrote:
"Jack Hanebach" <ja*****@spam.hanebach.net> wrote in message
news:ex**************@TK2MSFTNGP14.phx.gbl...
const means it's not going to change, read-only it might chane but
_you_ cannot change it.

You being the person using the class, if you are the person creating the
class then you can change it. :-)

An object can change the value of its own readonly properties but anything
using that object cannot.
JB

Nov 16 '05 #7
Stoitcho Goutsev (100) [C# MVP] wrote:
Hi,
Actually readonly values can be set only in the constructor of the type
decalring the readonly field. Once the constuctor exits no one can change
the readonly field. Constants on the other hand has no field behind them.
Their value is stored in the assembly metadata and that data is used by the
compiler when generating the code. The values of the constant are hardcoded
once the program is built. The latter might introduce versioning problems
that's why constants should be use for something that doesn't change e.g.
WM_* constants.
The other difference that I feel like needs to be mentioned is that as long
as readonly values can be of any type constants are limited only to the
primitive .NET types.

Yeah, sorry bout that.
I interpreted readonly property as property without a setter since I am
currently (hating it) using vb.net which uses the readonly keyword.

Disregard my post.

JB
Nov 16 '05 #8
The Last Gunslinger <jb******@yahoo.com> wrote:
Stoitcho Goutsev (100) [C# MVP] wrote:
Hi,
Actually readonly values can be set only in the constructor of the type
decalring the readonly field. Once the constuctor exits no one can change
the readonly field. Constants on the other hand has no field behind them.
Their value is stored in the assembly metadata and that data is used by the
compiler when generating the code. The values of the constant are hardcoded
once the program is built. The latter might introduce versioning problems
that's why constants should be use for something that doesn't change e.g.
WM_* constants.
The other difference that I feel like needs to be mentioned is that as long
as readonly values can be of any type constants are limited only to the
primitive .NET types.

Yeah, sorry bout that.
I interpreted readonly property as property without a setter since I am
currently (hating it) using vb.net which uses the readonly keyword.


No, your post was exactly right. Stoitcho's post was describing
readonly *fields*, which may be what the OP meant, but it isn't what he
wrote :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #9

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

Similar topics

6
by: Prasad | last post by:
Where are the const variables actually stored in memory. I mean If they are stored in data segment(external & static variables)or stack segment(local) how the compiler knows that it is read only...
2
by: JJ Feminella | last post by:
This statement is legal C#: public const string day = "Sunday"; but this statement is not: public const string days = new string { "Sun", "Mon", "Tue" }; The C# Programmer's Reference...
5
by: Paul E Collins | last post by:
I have a class hierarchy representing data importers, each of which reads lines from a particular type of comma-separated data file and creates corresponding entries in a database. There is an...
2
by: Marty | last post by:
Hi, In VB.NET, what happen to constants when compiling? Are they copied at every place they are needed? Or the application has to refer to constants address each time it need it at run time? ...
8
by: Eugene | last post by:
Hi, how can I declare in the method parameter and pass an object using ByVal and ensure that the called method cannot change the value of the object. I wouldn't want the called method to change the...
3
by: Miro | last post by:
Instead of having Public Variables all over my code, I have decided to make a class called "CONSTANT" and move them all into it. Public Class Constants Private Const strMiro = "MIRO" ' This...
13
by: bobg.hahc | last post by:
running access 2k; And before anything else is said - "Yes, Virginia, I know you can NOT use a variable to set a constant (that's why it's constant)". BUT - my problem is - I want a constant,...
3
by: Rob Blackmore | last post by:
I am trying to create an attribute of type Icon and have the following code: <AttributeUsage(AttributeTargets.Class)_ Public Class PIMSGridViewIconAttribute Inherits System.Attribute '///...
7
by: Hendrik Schober | last post by:
Hi, this #include <string> class test { typedef std::string::size_type size_type; static const size_type x = std::string::npos; }; doesn't compile using either VC9 ("expected constant...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
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...
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
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.