views:

189

answers:

2

I have a number of classes that represents business transaction calls: executing appropriate stored procedures.

Now the looks like this:

public static class Request
{
    public static void Approve(..) {
        using(connection) {
            command.Text = "EXEC [Approve] ,,"]
            command.ExecuteNonQuery();
        }
    }
}

And I want to make them more thread-safe:

public class Request {
    public static void Approve(..) {
        new Request().Approve(..);
    }

    internal void Approve(..) {
        using(connection) {
            command.Text = "EXEC [Approve] ,,"]
            command.ExecuteNonQuery();
        }
    }
}

But getting next error message:

The call is ambiguous between the following methods or properties: 'MyNamespace.Request.Approve(..)' and 'MyNamespace.Request.Approve(..)'

How can I force, mark that I'm calling non-static, instance method from static?

Or I cannot do that without renaming one of the methods? Or moving static method to another class, etc

+3  A: 

If you're making a call from an instance (e.g. requestVar.Approve()), then no, you have to rename it. The static can be called by using Request.Approve() however.

nasufara
Yes, because you're calling `Approve()` from an instance of the `Request` class. To call the static method unambiguously, you would do `Request.Approve()`. And anyways, doing a static method and an instance method with the same signature would generate a compiler error.
nasufara
You're right, sorry, didn't read your answer carefully
abatishchev
It's no problem :). But as per LBushkin's answer, you should probably just rename the internal method to `InternalApprove()` or something similar, or perhaps rethink your class structure.
nasufara
I just cannot name a static class :) What postfix sounds good for it? RequestManager? RequestMapper? RequestFactory?
abatishchev
It depends on what you're trying to do. Since both methods return void, `RequestFactory` wouldn't sound right. IMHO, I would choose `RequestManager`, but you know your own code :).
nasufara
I decided to name instance methods host class RequestInternal and put in into static Request. Thanks!
abatishchev
+5  A: 

C# allows does not allow static methods to be called through instance references. As such - the methods must either be named differently or use argument overloading to differentiate static methods from instance methods.

In your example, since the Approve() method is internal, renaming it is probably your easiest option.

As to marking that a method is static ... I (personally) think the name is a perfectly good means to differentiate the two - why invent something more complicated.

LBushkin
Your first sentence is erroneous. C# certainly does NOT allow static methods to be called through instance references.
Eric Lippert
Indeed. How embarrassing.
LBushkin