I can only have access to the base. variables in the Ball class when I add public when creating the variables within the Block class see images:

As you can see there are errors in the above image where I'm trying to change base.x and y, and print them to console.

Is there something wrong the tutorial? It also mentions that if I DON'T add new to the string creation then I would get a warning, however when I first made it (with the 'new' thing) I got the warning the tutorial said would occur if I DIDN'T have it there!

Maybe it's an out of date tutorial?

0

Share this post

Link to post

Share on other sites

x and y are private field of the Base class (private is the default access modifier for class) and then cannot be access from any child class. If you want to access them in the Ball class you have to define them as protected, like this :

public class Base
{
protected int x, y;
}

1

Share this post

Link to post

Share on other sites

x and y is private so you can't access them from outside the Block class. If you want to give the derived classes access the variables but still have them private from other code you can make use of the protected keyword.

As you can see, I had to put "protected" before "int x, y;" to allow it to compile. I also got the following warning about the "new string identifier" line until I commented it out:
test.cs(23,16): warning CS0109: The member `Ball.identifier' does not hide an inherited member. The new keyword is not required

It appears the tutorial expected you to make the members public.

Just skimming the tutorial, it seems pretty terrible. Even the website is atrocious, with that weird background.

Share this post

Link to post

Share on other sites

I'm sorry, since it was a minor error I assumed you wouldn't need to copy and paste it since I already had some kind of work-around.
[/quote]
In this case, yes. In general, and especially in future, it is easier for everyone if you paste as text. Use tags to make pretty boxes next time.

Thanks your reply, can you suggest any better tutorials, then?
[/quote]
Unfortunately I cannot, I haven't read any C# tutorials.