Class reference types

  • Thread starter Thread starter Sujith Manuel
  • Start date Start date
S

Sujith Manuel

Hi all,

Anybody know how to define class reference type in visual C#. My requirement
is as follows:

I have one class named CMyClass, I need to define one custom type named
"CNewClass" of type CMyClass so that I can create variables of type
CNewClass.

The variables of type CNewClass can hold a reference to CMyClass and it's
derived classes.

Any help in this regard is really appreciated.

Thanks in advance,
Sujith Manuel
 
-
Anybody know how to define class reference type in visual C#. My requirement
is as follows:

I have one class named CMyClass, I need to define one custom type named
"CNewClass" of type CMyClass so that I can create variables of type
CNewClass.

The variables of type CNewClass can hold a reference to CMyClass and it's
derived classes.


You can use the 'using-alias' directive to do what you want.

Here is a sample code for what you want to do:
namespace ConsoleApplication1
{

using CNewClass = CMyClass;
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//

CNewClass a = new CMyClass();
CMyClass b = new CMyClass();
a=b;




}
}


class CMyClass
{
}


}


In the above code, we create a type reference to CMyClass with the 'using'
directive. This basically creates an alias for CMyClass and you can then
use the two type names interchangeably.

Hope this helps.


--
Adrian Mascarenhas, Developer Division

This posting is provided "AS IS" with no warranties, and confers no rights.

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.
 
Back
Top