Create instance of unnamed class that implements interface 
Author Message
 Create instance of unnamed class that implements interface

Hello everyone

Is there possibility to create instance of unnamed class, that
implements some interface? In Java it worked as

ISomeInterface obj = new ISomeInterface ()
                         {
                            public void SomeMethod()
                            {
                                // do something;
                            }
                           . . .
                         };

Is there similar mechanism in C# ?

Any advice will be appreciated

Roman



Sat, 18 Jun 2005 04:21:25 GMT  
 Create instance of unnamed class that implements interface
Roman,

    As of now, no, there is not a way to do this (and I don't know that you
could do this in java exactly as you have it, as you can't instantiate
interfaces), but there will be something similar in the next release when
defining event handlers.

    Hope this helps.

--
          - Nicholas Paldino [.NET/C# MVP]


Quote:
> Hello everyone

> Is there possibility to create instance of unnamed class, that
> implements some interface? In Java it worked as

> ISomeInterface obj = new ISomeInterface ()
>                          {
>                             public void SomeMethod()
>                             {
>                                 // do something;
>                             }
>                            . . .
>                          };

> Is there similar mechanism in C# ?

> Any advice will be appreciated

> Roman



Sat, 18 Jun 2005 04:38:07 GMT  
 Create instance of unnamed class that implements interface
These are called Anonymous Inner Classes in Java and they're allowed.

C# doesn't have anonymous inner classes from what I know.

In Java, they were necessary to simulate events and delegates
in C# (anonymous methods work like this too).

So, the question is, Roman, why do you need anonymous inner classes?

Perhaps what you're doing could better be achieved with delegates or
events?

-c



Quote:
> Roman,

>     As of now, no, there is not a way to do this (and I don't know
that you
> could do this in java exactly as you have it, as you can't instantiate
> interfaces), but there will be something similar in the next release
when
> defining event handlers.

>     Hope this helps.

> --
>           - Nicholas Paldino [.NET/C# MVP]



> > Hello everyone

> > Is there possibility to create instance of unnamed class, that
> > implements some interface? In Java it worked as

> > ISomeInterface obj = new ISomeInterface ()
> >                          {
> >                             public void SomeMethod()
> >                             {
> >                                 // do something;
> >                             }
> >                            . . .
> >                          };

> > Is there similar mechanism in C# ?

> > Any advice will be appreciated

> > Roman



Sat, 18 Jun 2005 05:33:32 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Finding Class Instance from Interface Instance

2. Internal Compiler Error while deriving C# class from a MC++ class implementing a C# interface

3. creating an instance of a derived class from a base class

4. handing out interface pointers *only* to an instance - class factory not wanted

5. How to Find Class Instance from Interface?

6. Implementing an interface on a inherited class

7. Calling explicitly implemented interface method from derived class

8. How to implement several interface by one C++ class in VS7 ATL project

9. Implementing multiple interfaces from one class...

10. Derived Interface implemented in derived class not found

11. Implementing Events in your C++ classes/interfaces

12. Implementing automation interface on application class

 

 
Powered by phpBB® Forum Software