0

I've created a dll, lets call it ExampleHelper.dll.

The structure of the Visual Studio Class Library which I've compiled to dll is the following:

namespace ExampleHelper
{
    public class Example
    {
        public string GetExamples(string input)
        {
            // stuff
        }
    }
}

So, I reference it in my other project in which I want to use these ExampleHelper classes, by adding a using line at the top of the file in question:

using ExampleHelper;

Now, I can see that I can access the class from ExampleHelper, which is called Example. But, I can't access the methods in that class, which means I can't write Example.GetExamples("hello"), as it says GetExamples doesn't exist.

I noticed that I can do this:

Example e = new Example();
e.GetExamples("hello");

which I of course can use, but it doesn't feel quite right to instantiate a new object each time I want to use a helper method.

Have I done something completely wrong? My guess is yes, but I can't find where I'm going wrong. Any help appreciated!

etempo11
  • 23
  • 2
  • 7
  • If you don't want to create an instance, make the method (and perhaps the class) static... – CodeCaster Oct 04 '17 at 14:14
  • also see [What's a “static method” in C#?](https://stackoverflow.com/questions/4124102/whats-a-static-method-in-c) – Liam Oct 04 '17 at 14:16

3 Answers3

1

You need to have an instance of Example object to call this method. To call a method without an instace of a object, method must be static.

public static string GetExamples(string input)

should be the method's declaration.

1

Make GetExamples(string input) a static method

public static string GetExamples(string input)

Static methods do not require an instance of the class.

adjan
  • 13,371
  • 2
  • 31
  • 48
0

Please put your class and methods as STATIC, you'll be able to use it everywhere.

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/static

Bestter
  • 877
  • 1
  • 12
  • 29