how to declare a const field with object data type

S

Sajjad Akhter

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
 
D

Dan Bass

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.
 
J

Jon Skeet [C# MVP]

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.
 
S

Sajjad Akhter

Declaring readonly doesnt solve my problem as its not const
Is ther any way to use may be value types
 
J

Jon Skeet [C# MVP]

Sajjad Akhter said:
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.
 
D

Dan Bass

Really? Hmm, didn't know that.

Jon Skeet said:
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.
 
D

Dan Bass

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");
 
J

Jon Skeet [C# MVP]

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.
 
D

Dan Bass

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
 
J

Jon Skeet [C# MVP]

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top