I am trying to pass custom struct values to a public static method in C# but it is giving
Error 1 Inconsistent Accessibility
My main method code :
Console.WriteLine("Distance 1");
Console.Write("Enter feet : ");
int feet = int.Parse(Console.ReadLine());
Console.Write("Enter Inches : ");
float inches = float.Parse(Console.ReadLine());
P4_Distance distance1 = new P4_Distance(feet, inches);
Console.WriteLine("Distance 2");
Console.Write("Enter feet : ");
feet = int.Parse(Console.ReadLine());
Console.Write("Enter Inches : ");
inches = float.Parse(Console.ReadLine());
P4_Distance distance2 = new P4_Distance(feet, inches);
P4_Compare_distances(distance1, distance2);
and my struct is simply :
struct P4_Distance
{
public int feet { get; set; }
public float inches { get; set; }
public float totalInches { get; set; }
public P4_Distance(int Feet,float Inches)
{
feet = Feet;
inches = Inches;
totalInches = inches + (feet * 12);
}
}
the method that is giving error is :
public static void P4_Compare_distances(P4_Distance distance1, P4_Distance distance2)
{
if (distance1.totalInches > distance2.totalInches)
{
Console.WriteLine("Distance 1 > Distance 2");
}
else
{
Console.WriteLine("Distance 2 > Distance 1");
}
}
public struct P4_Distance, or removepublicfromP4_Compare_distances. The error is because your method is usable from areas of the program that can't necessarily access the types of the parameters specified.structinstead of aclass? Also if you really do need to use a struct you really really should make it Immutable (Read-Only) by making thesetin toprivate seton the 3 properties. It is very, very, easy to introduce bugs in to your code by having mutable structs.