Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 5th, 2007, 02:02 AM
Registered User
 
Join Date: Dec 2007
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
 
Old December 5th, 2007, 05:04 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
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 -/
 
Old December 5th, 2007, 08:02 PM
Authorized User
 
Join Date: Jul 2003
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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


 
Old December 6th, 2007, 05:00 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

OK, my mistake.

/- Sam Judson : Wrox Technical Editor -/
 
Old December 6th, 2007, 10:44 PM
Registered User
 
Join Date: Dec 2007
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





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





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