Having Problem with small Function
-
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);
}
}
} -
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);
}
}
}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 -
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 KreskowiakI am using the a book and I am following the example. I don't know why I am getting the error.
-
I am using the a book and I am following the example. I don't know why I am getting the error.
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
-
I am using the a book and I am following the example. I don't know why I am getting the error.
-
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);
}
}
}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 bestatic
. From astatic
method, you can reach otherstatic
methods. Or you can create an object. When you create a e.g. new Windows Forms application in Visual Studio, it adds a line toMain
:Application.Run(new Form1());
. This creates a new object of typeForm1
. 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); -
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);
}
}
}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 theTest
methodstatic
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)