Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #11 (permalink)  
Old January 17th, 2005, 09:57 AM
Friend of Wrox
 
Join Date: Jul 2004
Location: Tehran, , Iran.
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

please see the below code,
Code:
public class ErrorMessage
{
  private string message1;  
  public string Message
   {
      get
      {
         return this.message1;
      }
   }
}
class TestApp
{
  public static void DisplayError(ref ErrorMessage errormsg)
   {
      AfxMessageBox(errormsg);
   }
}
I think above code is efficient like C++ and also the compiler doesnt let change the reference,However I wrote more codes but the C# designing is better and more desirable,IMO there is a good logic behind everything we had in C++ and now we dont have in C#.in most situations the lack of those abilities results in better designing.

_____________________________
Mehdi.
software engineering student.
Looking for a good job for summer 2005.
Reply With Quote
  #12 (permalink)  
Old January 17th, 2005, 12:17 PM
Authorized User
 
Join Date: Feb 2004
Location: , , .
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

In your example, you are using a 'ref' parameter - this will allow the method to change what the argument is pointing at - i.e., changing the parameter. The 'const' parameter in C++ certainly doesn't allow that.

As I said earlier, the closest thing in C# is *not* to use a 'ref' parameter. Even this is subtly different from the C++ 'const' parameter, but as close as you can get.




Reply With Quote
  #13 (permalink)  
Old January 17th, 2005, 02:03 PM
Registered User
 
Join Date: Dec 2004
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Constant references let the called function guarantee to callers that it is not going to modify their object. Callers don't have to read all of the code in the function to find this out.

Callers also don't have to go to the trouble of guaranteeing this themselves by privatizing class members, like in Mehdi's example. If you have a class with 100 members, you have to privatize 100 things. This is not good design, especially if you need the members public for some other reason. It would be much easier to just add one "const" keyword to a function's parameter declaration.

P.S. Doknjas, in C++, "void SomeMethod(const someclass* someobject)" is using a constant pointer and not a reference. A constant reference would be done like this: "void SomeMethod(const someclass &someobject)". Notice the "&", instead of the "*".

P.P.S. Also, Doknjas, your statement that "void SomeMethod(someclass someobject) i.e., any change to the reference 'someobject' will not be reflected back to the code calling the method." is not true.

In the following example, Tweak actually modifies the i member of the original scMain object in the calling code:
   class SomeClass
   {
      public int i=0;
   }
   void Tweak(SomeClass scPassed)
   {
      scPassed.i=2;
   }
   void MyMain()
   {
      SomeClass scMain=new SomeClass();
      scMain.i=1;
      Tweak(scMain);
      MessageBox.Show(scMain.i.ToString());
   }
The output is "2".
Reply With Quote
  #14 (permalink)  
Old January 17th, 2005, 02:27 PM
Authorized User
 
Join Date: Feb 2004
Location: , , .
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

When I said: "any change to the reference 'someobject'", I meant not the internals of the object pointed to, but the pointer itself - i.e., it will still point to the same object after calling the method, but of course the internals of the object may have been manipulated.

I admit that I'm getting reference and pointer mixed up - sorry for the confusion on that. I've been using C# for too long ;/



Reply With Quote
  #15 (permalink)  
Old January 17th, 2005, 03:21 PM
Registered User
 
Join Date: Dec 2004
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ah, now I see what you're saying on changing the reference. OK thanks.

This is kind of off-topic, but it's interesting how the difference between reassigning an object and reassigning its members, and the fact that a string has no modifiable members (like the class in Mehdi's example), is what makes so many people think that a passed string is a constant reference.

If I wanted to, say, use my own string class to emulate the greater allocation efficiency of MFC's CString, I can't guarantee to callers of my function that I won't modify it. They'll have to read my whole function to find out if they can trust it--a very inefficient use of programmer time.

I'd be willing to bet my Visual Studio .net that there is a war being fought over this on Microsoft's listservs. I'll go see... :^)
Reply With Quote
  #16 (permalink)  
Old January 18th, 2005, 08:12 AM
Friend of Wrox
 
Join Date: Jul 2004
Location: Tehran, , Iran.
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
quote:Originally posted by Kurtis
 ...I can't guarantee to callers of my function that I won't modify it.They'll have to read my whole function...
I have a question here,why did we have Const parameter in C++?well,I myself can take care of my codes and dont let them change the reference of my argument?why should compiler do this task for me?

_____________________________
Mehdi.
software engineering student.
Looking for a good job for summer 2005.
Reply With Quote
  #17 (permalink)  
Old January 18th, 2005, 11:06 AM
Registered User
 
Join Date: Dec 2004
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

May I attempt to answer your question with some questions of my own? Your answers to my questions should answer your question. :^)

With your last ErrorMessage-class example, what if you wanted to be able to change the .message1 member of an ErrorMessage object after its construction? What would you change in the ErrorMessage class to make this possible? Would you make .message1 public? Would you add a "set" method? Either way, how are you then going to keep functions to which you pass it from using that new functionality to change it?

If you can, wouldn't it have been much easier to just add one keyword (const) to the function declaration?
Reply With Quote
  #18 (permalink)  
Old January 18th, 2005, 11:38 AM
Friend of Wrox
 
Join Date: Jul 2004
Location: Tehran, , Iran.
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

I will write a set method for my class and then when I want to send my object to a function with const parameters like C++,I dont change my object within,
Code:
class TestApp
{
  public static void DisplayError(ErrorMessage errormsg)
   {
      //there is no line like,
      //errormsg.SetNewValue();
      //errormsg=New ErrorMessage();
      //or everthing changes my refrence object 
      //however I put a line like,
      errormsg.GetErrorMeessage();
   }
}
(I dont change my object within my function,no need for the compiler to check whether I've changed it or not).

_____________________________
Mehdi.
software engineering student.
Looking for a good job for summer 2005.
Reply With Quote
  #19 (permalink)  
Old January 18th, 2005, 12:26 PM
Registered User
 
Join Date: Dec 2004
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You demonstrated my point well.

What you're describing is fine when you're a college student and noone else will ever have to deal with your code in the future, but 99.9% of the time in the real world that's not the case.

When you're dealing with someone else's functions (like most of us are), you shouldn't have to read the entire body of a function to know whether your object is safe in its hands.

P.S. Ever seen that Star Trek episode where Leah Brahms visits Jordi's engineering room and discovers that her theoretical calculations done on paper needed tweaking for the real-world application? It's something like that. :^)
Reply With Quote
  #20 (permalink)  
Old December 13th, 2007, 08:28 AM
Registered User
 
Join Date: Dec 2007
Location: Bangalore, Karnataka, India.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to viveksrivastava
Default

Hi everyone....
What I thought for such requirement if at all is there is.....
Make an Interface derive your class from that interface
Update the Interface definition whenever you are going to add new function in your class.
Now in the Interface definition keep Readonly properties only.
Now whenever you need to pass the constant reference to the function as a parameter pass this reference and not the actual Class object.... so the user will only get readonly properties and will not be able to set the value to the object. So your parameter will treated as constant reference.

Vivek Srivastava
viveksrivastavas@gmail.com
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C# constant functions viveksrivastava C# 4 December 6th, 2007 10:44 PM
A constant value is expected dcct84 C# 2 October 1st, 2007 09:08 AM
Constant in VBScript savoym VBScript 2 May 18th, 2005 06:53 AM
Static vs constant stoneman Access 2 December 10th, 2003 09:12 PM
'Constant' Error MattLeek Access VBA 2 September 14th, 2003 08:56 AM



All times are GMT -4. The time now is 05:16 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.