Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
| 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 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 September 15th, 2004, 08:36 PM
Authorized User
 
Join Date: Jul 2004
Location: , , .
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default how to achieve static method in an interface?

Hello, here is a question for c# gurus...

I am trying to conceptually do this

public interface IContentWebControl
{
    static string GetLink();
    static string bool IsReachable(int UserId);
}

public MyLoginControl : MyCompany.MyStandardUserControl ,IContentWebControl
{
    static string GetLink() {}
    static IsReachable(int UserId) {}
    // ...
}

public MyCheckOutControl : MyCompany.MyStandardUserControl ,IContentWebControl
{
    static string GetLink() {}
    static IsReachable(int UserId) {}
    // ...
}

This does not work because the methods inside the interface cannot be static. The compiler complains.

Is there a way to achieve the same result by other means?

I also tried to make virtual method in the MyCompany.MyStandardUserControl base class but static methods
are not allowed ;) good try though...

Right now, I am using reflection to scan types that derive from MyCompany.MyStandardUserControl
on application start up and look for a GetLink() method with the right prototype.

This is a start up check that catches the classes that did not implement a GetLink()
method. It works fine and it makes you love System.Reflection but it would be better
at compile time.


Ideas?

CF


 
Old September 21st, 2004, 12:10 PM
Friend of Wrox
 
Join Date: Jul 2004
Location: Tehran, , Iran.
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hello,
you cant have any Static Object (like Methods or <s>Fields</s>) in an interface because there is no implementation in an interface so we cant have instances of an interface and so there is no meaning that we could have a static method in an interface ....

--------------------------------------------
Mehdi.:)
 
Old September 23rd, 2004, 07:32 PM
Authorized User
 
Join Date: Jul 2004
Location: , , .
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That's right, we can't have any static in an interface which was the whole point of my post ;)

I have posted on other list and my approach using reflection seems to be one of the viable approaches.

It works great BTW

 
Old September 28th, 2004, 08:59 AM
Friend of Wrox
 
Join Date: Jul 2004
Location: Tehran, , Iran.
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hmm,yes ...
I had a mistake in my previous post,
we cant use Fields in an interface.
cfouquet,I think you could achieve fields too!!! :D

--------------------------------------------
Mehdi.:)




Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT php:function: call objects non static method polarbear Pro PHP 1 June 6th, 2008 05:39 PM
Web User Control to act on interface shared method rodmcleay General .NET 0 June 4th, 2008 11:51 PM
non-static reports to static html files miamikk ASP.NET 2.0 Basics 0 June 4th, 2007 01:48 PM
Method calls within same class w/Interface gmontanaro C# 0 April 6th, 2006 05:44 PM





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