0

I am new to C# and am having trouble calling methods of a class object that is inside of another class object. I get an error saying that 'MainWindow.Mascot.MyNameIs()' is inaccessible due to its protection level. However, all of the classes are public. How can I call a class method of an object from inside another object?

Thanks!

Here is part of the code:

public partial class MainWindow : Window { public class Mascot { string name; string MyNameIs() { return name; } } public class School { public Mascot myMascot; } public MainWindow() { InitializeComponent(); School Houston = new School(); Houston.myMascot = new Mascot(); Houston.myMascot.MyNameIs(); } } 

2 Answers 2

1

Your modifier of method MyNameIs() is private because u dont define it. Just add acess modifier public like:

public string MyNameIs() { return name; } 

If the class is not nested within another class, then its default access modifier is internal.If the class is nested within another class default access specifier is private.

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

Comments

0
  1. public class does not mean the class's member is public; it is about to another scope. You have to attach public front of the function MyNameIs.

Try this.

public partial class MainWindow : Window { public class Mascot { string name; public string MyNameIs() // changed { return name; } } public class School { public Mascot myMascot; } public MainWindow() { InitializeComponent(); School Houston = new School(); Houston.myMascot = new Mascot(); Houston.myMascot.MyNameIs(); } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.