In case you missed
The present invent
WASHINGTON — The T
---
abstract: 'We
Carcinoma arising
Cotton fabrics are
Q:
Avoiding SQL I
Cochlear developme
Vision from the he
"He was the one whQ:
How to fix "A field initializer cannot reference the nonstatic field, method, or property"
i'm a newbie in C# and i'm really stuck in a point,
i've tried some ways but nothing works...
Here is my code:
public class Calculator
{
public static void Main()
{
float a = 0;
float b = 0;
int c = 0;
int x = 0;
float boh = 0;
float bohoh = 0;
Console.WriteLine("Enter the first Number");
a = Convert.ToSingle(Console.ReadLine());
Console.WriteLine("Enter the second Number");
b = Convert.ToSingle(Console.ReadLine());
boh = Math.Sqrt(a);
bohoh = Math.Pow(b, b);
Console.WriteLine("the result = " + bohoh);
}
}
Error 1 'a' is a field but is used like a method
Error 2 'b' is a field but is used like a method
Error 3 The name 'c' does not exist in the current context
Error 4 'x' does not exist in the current context
Error 5 'boh' does not exist in the current context
Error 6 'bohoh' does not exist in the current context
A:
In the main method, variables a, b, c, x, boh and bohoh are not visible. You have to initialize them in the method and make them static:
public class Calculator
{
private static float a, b, c, x, boh, bohoh;
public static void Main()
{
Console.WriteLine("Enter the first Number");
a = Convert.ToSingle(Console.ReadLine());
Console.WriteLine("Enter the second Number");
b = Convert.ToSingle(Console.ReadLine());
boh = Math.Sqrt(a);
bohoh = Math.Pow(b, b);
Console.WriteLine("the result = " + bohoh);
}
}
The fields are only accessible inside the class.
A:
Change all the fields to class variables and initialize them inside your Main method as you have seen in your error messages.
//fields declaration
float a = 0;
float b = 0;
int c = 0;
int x = 0;
float boh = 0;
float bohoh = 0;
public static void Main()
{
Console.WriteLine("Enter the first Number");
a = Convert.ToSingle(Console.ReadLine());
Console.WriteLine("Enter the second Number");
b = Convert.ToSingle(Console.ReadLine());
boh = Math.Sqrt(a);
bohoh = Math.Pow(b, b);
Console.WriteLine("the result = " + bohoh);
}
This will fix your compilation errors.
A:
Use static variables if you want to use them within the class.
private static float a, b, c, x, boh, bohoh;
public static void Main()
{
Console.WriteLine("Enter the first Number");
a = Convert.ToSingle(Console.ReadLine());
Console.WriteLine("Enter the second Number");
b = Convert.ToSingle(Console.ReadLine());
boh = Math.Sqrt(a);
bohoh = Math.Pow(b, b);
Console.WriteLine("the result = " + bohoh);
}
OR use
public static void Main()
{
static float a, b, c, x, boh, bohoh;
Console.WriteLine("Enter the first Number");
a = Convert.ToSingle(Console.ReadLine());
Console.WriteLine("Enter the second Number");
b = Convert.ToSingle(Console.ReadLine());
boh = Math.Sqrt(a);
bohoh = Math.Pow(b, b);
Console.WriteLine("the result = " + bohoh);
}
OR you can create a constructor.
public class Calculator
{
public float a;
public float b;
public int c;
public int x;
public float boh;
public float bohoh;
public Calculator()
{
Console.WriteLine("Enter the first Number");
a = Convert.ToSingle(Console.ReadLine());
Console.WriteLine("Enter the second Number");
b = Convert.ToSingle(Console.ReadLine());
boh = Math.Sqrt(a);
bohoh = Math.Pow(b, b);
Console.WriteLine("the result = " + bohoh);
}
static void Main()
{
Calculator calc = new Calculator();
}
}
OR use only this class fields instead of a constructor.
public class Calculator
{
public static float a;
public static float b;
public static int c;
public static int x;
public static float boh;
public static float bohoh;
static void Main()
{
Console.WriteLine("Enter the first Number");
a = Convert.ToSingle(Console.ReadLine());
Console.WriteLine("Enter the second Number");
b = Convert.ToSingle(Console.ReadLine());
boh = Math.Sqrt(a);
bohoh = Math.Pow(b, b);
Console.WriteLine("the result = " + bohoh);
}
}