473,549 Members | 2,597 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 19194
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.h anebach.net> wrote in message
news:ex******** ******@TK2MSFTN GP14.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.h anebach.net> wrote in message
news:ex******** ******@TK2MSFTN GP14.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******** ***********@new s-server.bigpond. net.au...
Michael Culley wrote:
"Jack Hanebach" <ja*****@spam.h anebach.net> wrote in message
news:ex******** ******@TK2MSFTN GP14.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.co m>
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
12347
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 memory.
2
1810
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 specifies that 'the only valid values of a constant declarator constant expressions'. A constant expression is defined as 'an expression that can be...
5
3924
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 abstract base class, Importer, which contains the file-reading logic, and a series of inherited classes - CarrierImporter, RouteImporter, etc. - that...
2
1782
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? Thanks Marty
8
2529
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 data member of the class or change the properties when windows form is passed in. Eugene
3
1409
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 example Works Private Const boolInDebugMode As Boolean = System.Diagnostics.Debugger.IsAttached() 'Gets error The error I get is that it...
13
1846
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, that I can set from a variable (one time)!!! When my application starts, the user is prompted to make a selection from a list. I want that...
3
2598
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 '/// Private variables Private oPiGridViewIcon As System.Drawing.Icon = Nothing
7
4253
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 expression") or Comeau Online ("constant value is not known"). If I replace
0
7459
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7726
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7967
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6052
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5377
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5097
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3488
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1064
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
772
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.