Jump to content
  • Advertisement
Sign in to follow this  
Kasceh

Not-Working C# Inheritance Tutorial!

This topic is 2618 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm following this (http://www.freewebs.com/campelmxna/HTMLTutorials/Tutorial12HTML.htm) tutorial in to how inheritance works but I've come across a problem:

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:

rPfaG.png

iAaiP.png

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?

Share this post


Link to post
Share on other sites
Advertisement
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;
}

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.

Share this post


Link to post
Share on other sites
Please post your code as text, not images. We cannot copy and paste images into our IDEs. Also, include the exact error messages given by the compiler.

I've compiled the following code using Mono:

using System;

class Block
{
protected int x, y;
string identifier;

public Block()
{
identifier = "I'm the base class. I'm a block";
x = 200;
y = 300;

Console.WriteLine(identifier);
Console.WriteLine(x);
Console.WriteLine(y);
Console.WriteLine();
}
}

class Ball : Block
{
//new
string identifier;

public Ball()
{
identifier = "I'm the child class. I'm a Ball";
base.x = 744;
base.y = 6555;
Console.WriteLine(identifier);
Console.WriteLine(x);
Console.WriteLine(y);
Console.WriteLine();
}
}

class Test
{
public static void Main(string [] args)
{
new Block();
new Ball();
Console.WriteLine(" ~fin~ ");
}
}

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

Please post your code as text, not images. We cannot copy and paste images into our IDEs. Also, include the exact error messages given by the compiler.


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.


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:

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.


Thanks your reply, can you suggest any better tutorials, then?

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!