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
  #1 (permalink)  
Old March 24th, 2004, 07:14 PM
Authorized User
 
Join Date: Aug 2003
Location: , , .
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Default constant reference in C#?

How do you pass a constant reference in C#?

It was a real handy feature in C++, where you could return a constant reference to an object from a function, and give maximum visibility without possibility of side effect. Can you do that in C#? Would appreciate an example of function and property

Thanks
Reply With Quote
  #2 (permalink)  
Old April 2nd, 2004, 12:42 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I'm not really sure what you mean by passing a constant reference? Do you want to pass and/or return a constant? I think it's just a matter of having a function that takes and/or returns a data type that matches whatever the constant is you might want to accept/return.
Reply With Quote
  #3 (permalink)  
Old April 2nd, 2004, 04:08 PM
Authorized User
 
Join Date: Aug 2003
Location: , , .
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Let's say I have a class

public class myClass
{
//implementation
}

and a function in some class:

public class utility
{
 public static int myfunction(ref myClass obj)
 {
   //some logic using obj
   return someInt
 }
}

and another class

public class anotherClass
{
  private myClass obj = new myClass(...);
  int x = utility.myFunction(obj);
}

How can I be sure passing obj to utility.myfunction as reference that it is not going to alter obj. If I could pass a const reference to obj as could in C++, I would give utility.myfunction full visibility to obj, and yet be sure that it couldn't change it (no side effect of calling utility.myfunction!!)

Thanks

Reply With Quote
  #4 (permalink)  
Old April 3rd, 2004, 11:25 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I don't think this is possible. Any function argument of an object type will be a reference.

Instead of a class, you could create a structure and pass that by value to the method. This would ensure that the passed instance is not modified within the function, or at least the outer instance is not affected. The method's inner instance could certainly change but it would have not outside affect.

Peter
------------------------------------------------------
Work smarter, not harder.
Reply With Quote
  #5 (permalink)  
Old December 21st, 2004, 06:44 PM
Registered User
 
Join Date: Dec 2004
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I was wondering the same thing, but, Planoie, you're missing the point for why one would want to pass a constant reference. Let me give another, perhaps simpler, example in C++:

   void DisplayError(CString s2)
   {
      AfxMessageBox(s2);
   }

   void F()
   {
      CString s1="Houston, we have a problem!";
      DisplayError(s1);
      LogError(s1);
   }

The problem in my code here is an inefficiency. A new copy of s1's contents is created in memory when OutputError is called. If this string were, say, 1 meg in size, it would be ridiculous to call that function. So, to fix this inefficiency, I redefine my OutputError function as shown below:

   void DisplayError(CString &s2)
   {
      AfxMessageBox(s2);
   }

Now, instead of making a copy of the string, I just use the original one that was allocated in memory back in the F function. However, in a team-coding environment, if I saw the header definition of OutputError, I would think that it was going to modify my string, because it takes it as a reference. But I don't want it to modify my string, because I'm using it for something else myself after I call OutputError.

So, to guarantee to my function's callers that I'm not going to change their string but to eliminate the inefficiency of an unnecessary string copy, I would redefine my OutputError function as shown below:

   void DisplayError(const CString &s2)
   {
      AfxMessageBox(s2);
   }

This passes the string by reference, thereby avoiding an unnecessary string copy, but doesn't allow OutputError to modify it, thereby giving a warm fuzzy to all coders who are thinking of calling my OutputError function.

Again, the question is, "Is this possible in C#?" I want to avoid a string copy, but I want coders to know that my function can not modify the string that they pass me.
Reply With Quote
  #6 (permalink)  
Old January 15th, 2005, 11:46 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Yes, I guess I am missing the point. I have not written enough in C++ to ever have encountered this possibility. But also, I would never build a function that returns nothing and takes a input argument by reference. If I wrote a function that planned on doing something to an input argument, I'd have it return the result instead of modifying a referenced argument.

I don't know the answer to this, but I imagine the .NET documentation will have the answer.

-Peter
Reply With Quote
  #7 (permalink)  
Old January 15th, 2005, 02:15 PM
Friend of Wrox
 
Join Date: Jul 2004
Location: Tehran, , Iran.
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

you can make a class and declare your constant fields as private(also you can have a readonly property for getting them),
Code:
public class ErrorMessage
{
  private string message1;  
  public string Message
   {
      get
      {
         return this.message;
      }
   }
}
class TestApp
{
  public static void DisplayError(ErrorMessage errormsg)
   {
      AfxMessageBox(errormsg);
   }
}
_____________________________
Mehdi.
software engineering student.
Looking for a good job for summer 2005.
Reply With Quote
  #8 (permalink)  
Old January 16th, 2005, 01:47 PM
Authorized User
 
Join Date: Feb 2004
Location: , , .
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

My C++ is a little rusty, but a function which takes a const reference to an object in C++:
void SomeMethod(const someclass* someobject)

is fairly close to a method in C# such as:
void SomeMethod(someclass someobject)

i.e., any change to the reference "someobject" will not be reflected back to the code calling the method.

There are two differences however:

1. C++ compilers will generate an error if you try to modify the reference within the method, while C# will allow you to modify the reference, but will not reflect it back to the calling code.

2. The C++ const reference is more efficient since it isn't a copy as in C#. The actual reference is passed, but the compiler prevents you from attempting to change the reference within the method (via a compile error). This (very slight) speed difference is not likely to be the sort of thing a modern programmer should care about though.



Reply With Quote
  #9 (permalink)  
Old January 16th, 2005, 04:02 PM
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 Doknjas
 ...The C++ const reference is more efficient since it isn't a copy as in C#...
I think classes are refrence kinds like C++,it's a refrence and not a copy(against a struct).

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

Yes, but the argument passed to the function is a copy of the address in C# when the parameter is not a 'ref' parameter.

In C++, the actual address is passed to a 'const' parameter - the compiler then checks to ensure you don't change it during the method.


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 06:22 PM.


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