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 December 5th, 2007, 02:02 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 C# constant functions

Hi,
Can anybody let me know how, constant functions in C++ has been replicated in C# language.
Or it has been removed completely?

Vivek Srivastava
viveksrivastavas@gmail.com
Reply With Quote
  #2 (permalink)  
Old December 5th, 2007, 05:04 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

I think that would be 'static' or 'Shared' in VB.Net.

public class Math
{
public static int Add(int x, int y)
{
  return x + y;
}
}

And using it:

int a = Math.Add(2, 4);



/- Sam Judson : Wrox Technical Editor -/
Reply With Quote
  #3 (permalink)  
Old December 5th, 2007, 08:02 PM
Authorized User
 
Join Date: Jul 2003
Location: , , .
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You cannot defined const functions in C#.
Static methods are an unrelated concept.


Reply With Quote
  #4 (permalink)  
Old December 6th, 2007, 05:00 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

OK, my mistake.

/- Sam Judson : Wrox Technical Editor -/
Reply With Quote
  #5 (permalink)  
Old December 6th, 2007, 10:44 PM
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

Agree that we cannot define a constant function in C#, I am concerned if there is any way we can achieve this in C#.
As far I know constants should be resolved at compile time itself. But in C# all object creations happens at run time. And there is no global function concept as well in C#.
Lets take C++ for a while.... here we can always define a global function as constant or even a member function as constant. Now here also we have the flexibility to create the object at runtime. Now consider we are creating object at run time only then also we can make it constant such as in the following example....

class aC
{
public:
    int i;
    aC():i(9){}
    void print() const
    {
        cout<<" In Const "<<i<<endl;
    }
    void print()
    {
        cout<<" In Non Const "<<i<<endl;
    }


};

main()
{

    aC aObj;
    aC const aObj2;
    aC const *aNewObj = new aC();

    aObj.print();
    aObj2.print();
    aNewObj->print(); // will call constant version of print()

    return 0;
}

why in c# we just cannon write.....

aC const aObj = new aC(); // if aC is a class in C#

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
constant reference in C#? texasraven C# 19 December 13th, 2007 08:28 AM
Page 361 Constant Member Functions Nick Y BOOK: Ivor Horton's Beginning Visual C++ 2005 0 June 9th, 2006 08:01 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 08:55 PM.


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