473,406 Members | 2,956 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,406 software developers and data experts.

how to declare a const field with object data type

I need to create some constant fields those are of object type (other than
value types)

public class TestData10

{

public TestData10(string name)

{}

public string Airport;

}

public const TestData10 c_s1=new TestData10("Sea");

I am getting error on last line that right hand side is not const
expression, any idea hint.

Thanks,

Sajjad
Nov 16 '05 #1
9 3929

A const is something that exists as some sort of value at compile time.
Therefore "new myObject(...)" would never work.
Instead of const, I'd recommend using "readonly" in this case. This type of
variable can be assigned only once, either when it's declared, or in the
object constructor.

Hope that Helps.

Daniel.

"Sajjad Akhter" <sa@nospam.nospam> wrote in message
news:ez**************@TK2MSFTNGP10.phx.gbl...
I need to create some constant fields those are of object type (other than
value types)

public class TestData10

{

public TestData10(string name)

{}

public string Airport;

}

public const TestData10 c_s1=new TestData10("Sea");

I am getting error on last line that right hand side is not const
expression, any idea hint.

Thanks,

Sajjad

Nov 16 '05 #2
<"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk>> wrote:
A const is something that exists as some sort of value at compile time.
Therefore "new myObject(...)" would never work.
Instead of const, I'd recommend using "readonly" in this case. This type of
variable can be assigned only once, either when it's declared, or in the
object constructor.


Note, however, that that only makes the value of the variable (i.e. a
reference) readonly. It doesn't stop you from changing values inside
the object. For instance, you could have a readonly StringBuilder
variable, but still call Append on it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
Declaring readonly doesnt solve my problem as its not const
Is ther any way to use may be value types

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk>> wrote:
A const is something that exists as some sort of value at compile time.
Therefore "new myObject(...)" would never work.
Instead of const, I'd recommend using "readonly" in this case. This type
of
variable can be assigned only once, either when it's declared, or in the
object constructor.


Note, however, that that only makes the value of the variable (i.e. a
reference) readonly. It doesn't stop you from changing values inside
the object. For instance, you could have a readonly StringBuilder
variable, but still call Append on it.

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

Nov 16 '05 #4
Sajjad Akhter <sa@nospam.nospam> wrote:
Declaring readonly doesnt solve my problem as its not const
Is ther any way to use may be value types


Either make your type immutable, or make it a value type.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
Really? Hmm, didn't know that.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk>> wrote:
A const is something that exists as some sort of value at compile time.
Therefore "new myObject(...)" would never work.
Instead of const, I'd recommend using "readonly" in this case. This type
of
variable can be assigned only once, either when it's declared, or in the
object constructor.


Note, however, that that only makes the value of the variable (i.e. a
reference) readonly. It doesn't stop you from changing values inside
the object. For instance, you could have a readonly StringBuilder
variable, but still call Append on it.

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

Nov 16 '05 #6
Something like this you mean?

public class TestData10
{

public TestData10(string name)
{}

private string _airport;
public string Airport
{
get
{
return _airport;
}
// no setter
}

}

public readonly TestData10 c_s1=new TestData10("Sea");
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Sajjad Akhter <sa@nospam.nospam> wrote:
Declaring readonly doesnt solve my problem as its not const
Is ther any way to use may be value types


Either make your type immutable, or make it a value type.

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

Nov 16 '05 #7
<"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk>> wrote:
Something like this you mean?

public class TestData10
{

public TestData10(string name)
{}

private string _airport;
public string Airport
{
get
{
return _airport;
}
// no setter
}

}

public readonly TestData10 c_s1=new TestData10("Sea");


Exactly. If the type is immutable (like string is) then making the
variable itself readonly is enough.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8
I'm understanding this, slooowly.
So what is it a general rule of thumb to make classes immutable or not?

I.E. StringBuilder versus string

Exactly. If the type is immutable (like string is) then making the
variable itself readonly is enough.

--
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
<"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk>> wrote:
I'm understanding this, slooowly.
So what is it a general rule of thumb to make classes immutable or not?

I.E. StringBuilder versus string


Well, if you *can* make them immutable, it's often a good idea - all
kinds of things become easier when they're immutable. Basically, do you
want the object to be dynamic, or do you want it to stand for a
coherent set of values which don't change?

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

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

Similar topics

19
by: Thomas Matthews | last post by:
Hi, Given a structure of pointers: struct Example_Struct { unsigned char * ptr_buffer; unsigned int * ptr_numbers; }; And a function that will accept the structure:
4
by: PawelF | last post by:
I need to replace string with Guid ready to use: public const string GUID_1 = "CF0003D61F6E4D6AA6B17B18E5057FCD"; like public const Guid ... Is there any chance to do this.
23
by: mark.moore | last post by:
I know this has been asked before, but I just can't find the answer in the sea of hits... How do you forward declare a class that is *not* paramaterized, but is based on a template class? ...
8
by: redefined.horizons | last post by:
I would like to have an array declaration where the size of the array is dependent on a variable. Something like this: /* Store the desired size of the array in a variable named "array_size". */...
10
by: d3x0xr | last post by:
---- Section 1 ---- ------ x.c int main( void ) { char **a; char const *const *b; b = a; // line(9)
0
by: d3x0xr | last post by:
Heh, spelled out in black and white even :) Const is useles... do NOT follow the path of considering any data consatant, because in time, you will have references to it that C does not handle,...
4
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader =...
0
by: roamnet | last post by:
hi i created database file with .mdf extention ,sql server as a source and use grid view to display data there're no problem in data retrieve and display,but i want to edit it or insert new...
9
by: raylopez99 | last post by:
I'm posting this fragment from another thread to frame the issue clearer. How to pass an object to a function/method call in C# that will guarantee not to change the object?* In C++, as seen...
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
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
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
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,...
0
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...

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.