Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Having Problem with small Function

Having Problem with small Function

Scheduled Pinned Locked Moved C#
csharphelplinqtestingbeta-testing
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    computerpublic
    wrote on last edited by
    #1

    Hello, I am trying to learn functions in C#. I wrote the following test functions and I am getting the error: Error 1 An object reference is required for the non-static field, method, or property 'Testing_Functions.Program.Test(double, double, double)' C:\Users\computerpublic\AppData\Local\Temporary Projects\Testing Functions\Program.cs 18 29 Testing Functions.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace Testing_Functions
    {
    class Program
    {
    public Double Test(Double Val1, Double Val2, Double Val3)
    {
    Double Test_Val = Val1 + Val2 + Val3;
    return Test_Val;
    }
    static void Main(string[] args)
    {
    Double A = 1, B = 2, C = 3;
    Double Result = Test(A,B,C);
    }
    }
    }

    D B D 3 Replies Last reply
    0
    • C computerpublic

      Hello, I am trying to learn functions in C#. I wrote the following test functions and I am getting the error: Error 1 An object reference is required for the non-static field, method, or property 'Testing_Functions.Program.Test(double, double, double)' C:\Users\computerpublic\AppData\Local\Temporary Projects\Testing Functions\Program.cs 18 29 Testing Functions.

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;

      namespace Testing_Functions
      {
      class Program
      {
      public Double Test(Double Val1, Double Val2, Double Val3)
      {
      Double Test_Val = Val1 + Val2 + Val3;
      return Test_Val;
      }
      static void Main(string[] args)
      {
      Double A = 1, B = 2, C = 3;
      Double Result = Test(A,B,C);
      }
      }
      }

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      You have to declare the Test method as "static", just like your Main method. Either that, you have to new up an instance of the Program class and use the Test method through that. Serisouly, this is C# 101 stuff. You really need to pick up a beginners book on C# and work through it.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak

      C 1 Reply Last reply
      0
      • D Dave Kreskowiak

        You have to declare the Test method as "static", just like your Main method. Either that, you have to new up an instance of the Program class and use the Test method through that. Serisouly, this is C# 101 stuff. You really need to pick up a beginners book on C# and work through it.

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak

        C Offline
        C Offline
        computerpublic
        wrote on last edited by
        #3

        I am using the a book and I am following the example. I don't know why I am getting the error.

        L 2 Replies Last reply
        0
        • C computerpublic

          I am using the a book and I am following the example. I don't know why I am getting the error.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Look up the keyword "static", as that is why you are getting that particular error.

          Why is common sense not common? Never argue with an idiot. They will drag you down to their level where they are an expert. Sometimes it takes a lot of work to be lazy Please stand in front of my pistol, smile and wait for the flash - JSOP 2012

          1 Reply Last reply
          0
          • C computerpublic

            I am using the a book and I am following the example. I don't know why I am getting the error.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            You're trying to call an instance method on an implicit this, but Main is static so there is no this.

            1 Reply Last reply
            0
            • C computerpublic

              Hello, I am trying to learn functions in C#. I wrote the following test functions and I am getting the error: Error 1 An object reference is required for the non-static field, method, or property 'Testing_Functions.Program.Test(double, double, double)' C:\Users\computerpublic\AppData\Local\Temporary Projects\Testing Functions\Program.cs 18 29 Testing Functions.

              using System;
              using System.Collections.Generic;
              using System.Linq;
              using System.Text;

              namespace Testing_Functions
              {
              class Program
              {
              public Double Test(Double Val1, Double Val2, Double Val3)
              {
              Double Test_Val = Val1 + Val2 + Val3;
              return Test_Val;
              }
              static void Main(string[] args)
              {
              Double A = 1, B = 2, C = 3;
              Double Result = Test(A,B,C);
              }
              }
              }

              B Offline
              B Offline
              Bernhard Hiller
              wrote on last edited by
              #6

              The function "Main" is the start point of the program. When "Main" is called, no instance of any object could yet be created, hence it must be static. From a static method, you can reach other static methods. Or you can create an object. When you create a e.g. new Windows Forms application in Visual Studio, it adds a line to Main: Application.Run(new Form1());. This creates a new object of type Form1. Similarly, you could write another class, create the corresponding object, and call its function, e.g.

              namespace Testing_Functions
              {
              class Addition
              {
              public Double Test(Double Val1, Double Val2, Double Val3)
              {
              Double Test_Val = Val1 + Val2 + Val3;
              return Test_Val;
              }
              }
              }

              and then call it from your Main method:

              Addition a = new Addition();
              Double Result = a.Test(A,B,C);

              1 Reply Last reply
              0
              • C computerpublic

                Hello, I am trying to learn functions in C#. I wrote the following test functions and I am getting the error: Error 1 An object reference is required for the non-static field, method, or property 'Testing_Functions.Program.Test(double, double, double)' C:\Users\computerpublic\AppData\Local\Temporary Projects\Testing Functions\Program.cs 18 29 Testing Functions.

                using System;
                using System.Collections.Generic;
                using System.Linq;
                using System.Text;

                namespace Testing_Functions
                {
                class Program
                {
                public Double Test(Double Val1, Double Val2, Double Val3)
                {
                Double Test_Val = Val1 + Val2 + Val3;
                return Test_Val;
                }
                static void Main(string[] args)
                {
                Double A = 1, B = 2, C = 3;
                Double Result = Test(A,B,C);
                }
                }
                }

                D Offline
                D Offline
                DaveyM69
                wrote on last edited by
                #7

                I think it's been explained pretty well already, but just in case... static methods are available without an instance of the class. Non static methods are only available through an instance of the class. This gives you two options in your case. Either 1. Make the Test method static

                public static Double Test(Double Val1, Double Val2, Double Val3)

                or 2. Create a new instance of the class that holds the method (Program in this case).

                Double Result = new Program().Test(A,B,C);

                alternative

                Program program = new Program();
                Double Result = program.Test(A,B,C);

                Dave
                Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
                BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups