0

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");
            }

        }
4
  • 3
    Change your struct declaration to public struct P4_Distance, or remove public from P4_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. Commented Jan 25, 2016 at 23:10
  • It worked ! thanks for the help Commented Jan 25, 2016 at 23:14
  • Is there a specific reason you are using a struct instead of a class? Also if you really do need to use a struct you really really should make it Immutable (Read-Only) by making the set in to private set on the 3 properties. It is very, very, easy to introduce bugs in to your code by having mutable structs. Commented Jan 25, 2016 at 23:39
  • Yes thank you @ScottChamberlain for your advice I will work on it , and for struct no reason , I am new to C# and and was just solving a struct chapter problem which said so. Commented Jan 31, 2016 at 13:57

1 Answer 1

2

declare the struct as public:

public struct P4_Distance
{
 ...
}

Without the public the structure has protected accessibility. You cannot expose a protected structure as a public element somewhere - that would defeat the purpose of the protected accessibility.

By making the the struct public you make the accessibility consistent.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.