Must value be determined at compile-time?

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2009
Posts: 3
Reputation: srubys is an unknown quantity at this point 
Solved Threads: 0
srubys srubys is offline Offline
Newbie Poster

Must value be determined at compile-time?

 
0
  #1
Jan 5th, 2009
hi

Just a few days ago I've started learning C#.

BTW - I hope I'm not breaking any forum rules by putting three questions inside a single thread, but I don't want to "spam" this forum by making too many threads.


1)
From the book:

A field initializer is part of the field declaration, and consists of an equals sign followed by an expression that evaluates to a value. The initialization value must be determinable at compile time.
But if initialization value must indeed be determined at compile time, then the following code would produce an error, due to the fact that here the initialization value can’t be known at compile time, since memory on the heap is only allocated at run-time:

  1. class A
  2. {
  3. B b = new B(); // here value of b can't be known until run time
  4. }


2)
The following quote talks about about the usefulness of auto-implemented properties:

Besides being convenient, auto-implemented properties allow you to easily insert a property where you might be tempted to declare a public field.
You might, however, be tempted to release a version of the code with a public field, and then in a later release change the field to a property. However, the semantics of a compiled variable and a compiled property are different. If, in a later release, you were to switch from a field to a property, any assemblies accessing that field in the first release would have to be recompiled to use the property. If you use a property in the first place, the client doesn’t have to be recompiled.
Say I created class X, the member of which is also a public instance field named A, and put this class into assembly. Now some third party writes a code that access this field A. Later I decide to replace public field A with public property called A ( of same type as field was ), so I rewrite and recompile my class.
Now client would still access this property using the same code as it used for accessing field A -->
  1.  
  2. void Main
  3. {
  4. ...
  5. X x = new X();
  6. int i = x.A;
  7. ...
  8. }

And since the client code remains unchanged, I don't see the need for recompiling it?




3) Why must the value of constant be determined at compile time rather than at run time, while read only field can be determined at run time?
Last edited by srubys; Jan 5th, 2009 at 1:46 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: Must value be determined at compile-time?

 
0
  #2
Jan 5th, 2009
1)
  1. B b = new B();

Does have a value - as in effect it is a pointer to a B shaped peice of memory. Ergo, it has value, it is B shaped.

2) In your code if you have
  1. Class X
  2. {
  3. public int A;
  4. }

And you change it to
  1. class X
  2. {
  3. private int a;
  4. public int A
  5. {
  6. get { return a; }
  7. set { a=value; }
  8. }

You've changed where in memory its pointing to.. so it would need recompliation.

3) because, constants are hard coded into the exe, eg if you put in A= 50; rather than it taking A and placing 50 in it it, all instances of A are pointing to a reference of 50 within the exe (ish) - give or take a bit.
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,978
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 288
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Must value be determined at compile-time?

 
0
  #3
Jan 5th, 2009
B b = new B();
Under the hood new B() might even change so many variables that it is possible the world is totally changed after executing this statement.
Last edited by ddanbe; Jan 5th, 2009 at 2:51 pm.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 3
Reputation: srubys is an unknown quantity at this point 
Solved Threads: 0
srubys srubys is offline Offline
Newbie Poster

Re: Must value be determined at compile-time?

 
0
  #4
Jan 5th, 2009
hi


1)
Originally Posted by LizR View Post
  1. B b = new B();

Does have a value - as in effect it is a pointer to a B shaped peice of memory. Ergo, it has value, it is B shaped.
So b field has a value even though it doesn’t actually point to a specific location ( at compile time )? But couldn't then same argument also applied to, say, integer field:

  1. public int someValue = new A().someMethod();

Here someValue will be initialized at run time, when instance method someMethod() is called.
But we could still argue that at compile time someValue is a holder of a integer shaped piece of memory and thus someValue has a value, it is int shaped?!


2)
Originally Posted by LizR View Post
2) In your code if you have
  1. Class X
  2. {
  3. public int A;
  4. }

And you change it to
  1. class X
  2. {
  3. private int a;
  4. public int A
  5. {
  6. get { return a; }
  7. set { a=value; }
  8. }

You've changed where in memory its pointing to.. so it would need recompliation.
I realize that in the example you provided client code also needs to change ( since basically the interface changed ). But I don’t understand why client code should also change if it wants to access member A of the following class:

  1. class X
  2. {
  3. public int A
  4. }

and we change class X to

  1.  
  2. class X
  3. {
  4. public int A
  5. {
  6. get; set;
  7. }
  8. }

Here client code should remain the same, since the interface didn't change!




3)
Originally Posted by LizR View Post
3) because, constants are hard coded into the exe, eg if you put in A= 50; rather than it taking A and placing 50 in it it, all instances of A are pointing to a reference of 50 within the exe (ish) - give or take a bit.
If I understood you correctly, then compiler doesn't place a constant A into an assembly, but rather just its value?
I don't quite understand what is gained by implementing constants in such way?
Also, why isn't static readonly field treated in the same manner?


BTW – is class ( not an instance of class ) created at compile time, or at runtime?




thank you
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,978
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 288
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Must value be determined at compile-time?

 
0
  #5
Jan 5th, 2009
BTW – is class ( not an instance of class ) created at compile time, or at runtime?
A class is a discription of an object which is created(instantiated) at runtime.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 3
Reputation: srubys is an unknown quantity at this point 
Solved Threads: 0
srubys srubys is offline Offline
Newbie Poster

Re: Must value be determined at compile-time?

 
0
  #6
Jan 5th, 2009
Originally Posted by ddanbe View Post
A class is a discription of an object which is created(instantiated) at runtime.
Sorry about that. I know class is just a blueprint, what I meant was if static members and static methods are created at compile time or at run time?
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: Must value be determined at compile-time?

 
0
  #7
Jan 5th, 2009
Originally Posted by srubys View Post
1)


So b field has a value even though it doesn’t actually point to a specific location ( at compile time )? But couldn't then same argument also applied to, say, integer field:
Sort of, but, very basic elemental types are slightly different..

  1. public int someValue = new A().someMethod();

Here someValue will be initialized at run time, when instance method someMethod() is called.
But we could still argue that at compile time someValue is a holder of a integer shaped piece of memory and thus someValue has a value, it is int shaped?!
Ah but who are you to amke the assumption that .somemethod() only returns an int, it could do so much more. hence it needs the change.

2)


I realize that in the example you provided client code also needs to change ( since basically the interface changed ). But I don’t understand why client code should also change if it wants to access member A of the following class:

  1. class X
  2. {
  3. public int A
  4. }

and we change class X to

  1.  
  2. class X
  3. {
  4. public int A
  5. {
  6. get; set;
  7. }
  8. }

Here client code should remain the same, since the interface didn't change!
Ah, but, it needs to generate the missing private variable etc.


3)
If I understood you correctly, then compiler doesn't place a constant A into an assembly, but rather just its value?
I don't quite understand what is gained by implementing constants in such way?
Readability, and the fact you set it in one place in your code eg "FPS = 30" and then you can type FPS everywhere rather than 30, and more easily maintainable..

What if then you had MAX_CONNECTIONS =30.. and you see "30" somewhere.. which is it?

Also, why isn't static readonly field treated in the same manner?
Read only properties are still changable by the private variable.. just not outside the class.

BTW – is class ( not an instance of class ) created at compile time, or at runtime?
thank you
The class template is done at compile time, the instance, runtime.
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC