-2

I have the following ISampleInterface interface

public interface ISampleInterface
{
    bool sampleMethod(string _txt);
}

So that interface I implemented like this,

public Class SampleInterface : BaseEntity<Sample>, ISampleInterface
{
    IContext _context;

    public SampleInterface (IContext context) 
     : base(context)
    {
      _context = context;
    }

    public bool sampleMethod(string _txt)
    {
        return true;
    }
}

so from my main static method how to call this sampleMethod, without invoking SampleInterface class

my Service class tight to an entity, which is "Sample", so I just want to know, without invoke Service class, can call this method(sampleMethod) via ISampleInterface

public static void main(String[] args) 
{ 
    
} 

Exact issue I’m facing, I have batchjob and web project, web project i can invoke this method via IOC addictive, also i want to invoke same method in batchjob, i cant it’s because of batchjob’s main method is static method

Kelum
  • 1,745
  • 5
  • 24
  • 45
  • 1
    So you wants to call an instance method for a class, without an instance of said class? – Justin Lessard Oct 14 '20 at 12:34
  • 2
    You cant call an instance method without an instance! – Jamiec Oct 14 '20 at 12:34
  • you need to _instantiate_ an instance, by using `var instance = new SampleInterface();` then call the method on the instance `instance.sampleMethod("foo")` – Pac0 Oct 14 '20 at 12:35
  • @Justin yes, is there any way to call, without instantiate – Kelum Oct 14 '20 at 12:36
  • 1
    @Gesso It makes no sense. What are you trying to accomplish here? – Justin Lessard Oct 14 '20 at 12:37
  • "without invoking SampleInterface class" = without creating an instance of SampleInterface? Impossible. Period! Why don't you want to create an instance? SampleInterface should be named Sample, just as ISampleInterface should be named ISample. – Youp Bernoulli Oct 14 '20 at 12:38
  • 1
    This is an XY problem. You want to do something, X, and you think that the solution is Y, calling a method on an interface without any instance of a class implementing that interface. You're now asking how to do Y, but that's simply impossible. An interface is a contract, not something you can call. You can call it on a class that implements said interface. Please explain your X, or why you think you need this. The static part here is just a red herring, it's irrelevant. – CodeCaster Oct 14 '20 at 12:58
  • @CodeCaster Actually my Service class tight to an entity, which is "Sample", so I just want to know, without invoke Service class, can call to this method via ISampleInterface – Kelum Oct 14 '20 at 13:02
  • 2
    @Gesso no, you really must provide more context, still too vague. Yes, your `SampleInterface` class inherits from `BaseEntity`, if you want a class that implements `ISampleInterface` but is not tied to `Sample`, you need to create a new class that implements that interface. But why? If you want to call the method that's inside `SampleInterface.sampleMethod()`, then you must instantiate `SampleInterface`: `new SampleInterface().sampleMethod()`. You **must call `ISampleInterface.sampleMethod()` on an instance**. Explain why you don't want that. – CodeCaster Oct 14 '20 at 13:10
  • I have reopened the question, because the "static" part is irrelevant. It's not a duplicate of https://stackoverflow.com/questions/1360183/how-do-i-call-a-non-static-method-from-a-static-method-in-c?noredirect=1&lq=1. – CodeCaster Oct 14 '20 at 13:10
  • 1
    @CodeCaster The question is asking literally exactly the same thing, and the answers here, and your comments saying what's wrong, are exactly the same as on the duplicate. – Servy Oct 14 '20 at 13:25
  • @Servy no, this has nothing to do with static. It's about **interfaces** and instances. Not static and instances. – CodeCaster Oct 14 '20 at 13:46
  • @code caster if i say real problem, i have batchjob and web project, web project i can invoke this method via IOC addictive, also i want to invoke same method in batchjob, i cant it’s because of batchjob’s main method is static method – Kelum Oct 14 '20 at 15:10
  • So your actual question is how to get dependency injection to work in a console app, so you can use the class the same way as you do in your web application? – CodeCaster Oct 14 '20 at 15:15
  • @codecaster i found something like this, will look into this, thanx https://andrewlock.net/using-dependency-injection-in-a-net-core-console-application/ – Kelum Oct 14 '20 at 15:27

2 Answers2

0

sampleMethod is an instance method, so you need an instance. There is no way to call it without one!

public static void main(String[] args) 
{ 
    var instance = new SampleInterface();
    instance.sampleMethod("some text");
}
Jamiec
  • 133,658
  • 13
  • 134
  • 193
0

Calling an instance method without any instance has no meaning in C#.

It's like asking to do numeric calculation without number.

Maybe related to your context: interfaces must be implemented at the instance level, there are no such thing as static interfaces. While not completely impossible to achieve, this is not available in C#, by design.

So, with instances, for completeness of answer:

To create an instance, use a constructor (since you didn't defined any constructor, a default public parameterless constructor is created implicitly)

Then, you can call the method on the instance.

var instance = new SampleInterface();
instance.someMethod("foo");
Pac0
  • 21,465
  • 8
  • 65
  • 74
  • Sorry I just added contractor – Kelum Oct 14 '20 at 12:40
  • What you added doesn't compile, what is this private field `_sample`? it's not defined in a class. – Pac0 Oct 14 '20 at 12:41
  • If you have a specific constructor, then you need to call the appropriate constructor with `new`. If you have some parameters to pass to the constructor, you need to pass them, like in a function call `new MyClassName(parameter1, parameter2)` – Pac0 Oct 14 '20 at 12:43
  • Actually my service class tight to an entity, which is "Sample", wonder if can call without instantiate – Kelum Oct 14 '20 at 12:58
  • @Gesso what are you trying to achieve? What is the problem you want to solve? – Pac0 Oct 14 '20 at 13:16
  • if i say real problem, i have batchjob and web project, web project i can invoke this method via IOC addictive, also i want to invoke same method in batchjob, i can’t it’s because of batchjob’s main method is static method, – Kelum Oct 14 '20 at 15:09
  • you can create an instance method that calls the static method (note: make sure that your static method is thread-safe). – Pac0 Oct 14 '20 at 15:33
  • Found this anser, seems will works https://stackoverflow.com/questions/48413055/correct-use-of-autofac-in-c-sharp-console-application/48413665#48413665 – Kelum Oct 14 '20 at 15:44