-1

I have a C# console app with a static main method.

I want to be able to call a non-static method in main but cannot.

I believe I need to keep main static but I also want to run non-static methods within the program?

The statement below says an object reference is required.

c1 = new Class1()

Where / how do I instantiate non-static objects in this program?

Thanks

namespace VER
{
    class Example
    {
        private Dictionary<string, long> dStrLong;
        Class1 c1;

        static void Main(string[] args)
        {
            c1 = new Class1();

        }
    }
}
eocron
  • 6,885
  • 1
  • 21
  • 50
  • 2
    I think you are mixing things up. I'd say get your hands on a basic c# tutorial and then come back later. – Peter Bons Jan 18 '18 at 18:10
  • `c1 = new Class1();` is instantiating an object. Use this access your non-static stuff. – Sweeper Jan 18 '18 at 18:11
  • objects aren't static, variables are. anyway maybe you're looking to do `Class1 c1 = new Class1();` – Ousmane D. Jan 18 '18 at 18:11
  • `c1` *itself* is non-static, so you can't access it from a `static` method. Why does `c1` need to be a class-level member? Why can't `main()` just declare a `c1` variable internally? What's the eventual goal here? – David Jan 18 '18 at 18:12
  • 1
    You're missing the basics of OOP. I recommend to google around and understand the difference between static and instance variables / methods. – GermanC Jan 18 '18 at 18:14
  • I want to have the instance of Class1 outside the scope of main() – Stephen McElduff Jan 18 '18 at 18:14
  • @StephenMcElduff: For what purpose? Why does this class need a member of type `Class1`? Can that member be `static`? Should `main()` perhaps be internally creating an instance of `Example` instead? What are you actually building here? Do you even know what `static` means? Have you looked that up? Are you just adding/removing "static" keywords until it compiles and hoping for the best? – David Jan 18 '18 at 18:16

2 Answers2

0

If you make Class1 c1 static, you will be able to assign new object to it and call its non-static method.

Alternatively, remove Class1 c1 field from the class and write Class1 c1 = new Class1();.

0

I think you need to understand how this works behind the scenes.

When you use a static method, a silent instance is created for you and that is the instance that serves the calls to static methods.

Anything that is not static, belongs to each and every instance you create. That is why the compiler is saying an object reference is required (c1 is declared as an instance member).

In order for you to consume c1 within main, you can either make it static as well or make it a public member and create an instance so you can use it:

namespace VER
{
    class Example
    {
        private Dictionary<string, long> dStrLong;
        //You can make it static
        static Class1 c1 = new Class1();

        static void Main(string[] args)
        {
            c1.DoSomething();

        }
    }
}

OR

namespace VER
{
    class Example
    {
        private Dictionary<string, long> dStrLong;
        //You can make it public
        public Class1 c1 = new Class1();

        static void Main(string[] args)
        {
            var c1 = new Example();
            c1.DoSomething();
        }
    }
}

As your code currently exists, you are trying to access a variable that belongs to an instance from a static method.

Either way, make sure you instantiate an object for the variable.

JuanR
  • 7,405
  • 1
  • 19
  • 30