Okay, so I've used this without even knowing 100% about what it is. I know what it does so I've always just called it the "type of object" but I want to know what this is really called. For example, I create a class and I want it to be a type of another class. I do this:

public class Class1 : Class2 { /* Stuff */ }

But I've never really understood it. I picked it up while building custom controls and figured out that I could use it to create modified copies of certain classes. The reason I now want to know, is that one I'm having to use it more often; and I've recently seen one that blew my mind:

public class Class1 : Class2, Class3 { /* Stuff */ }

So an explanation on these would be greatly appreciated. I would like to know what the ':' character is actually called in this particular case. Kind of like || is the OR operator.

Thanks,
Jamie

P.S. - If there is such a thing, what would '::' be and what would it be used for? Because I know that '||' is used in conditional and '|' is not, so if there is a '::' please explain that one as well. :)

It is called inheritance. Your class is being declared as a subclass of the inherited class. It follows a 'is a' relationship e.g. a car could inherit from the vehicle class with

public class Car : Vehicle

The main advantage of this is that the Car class gains access to methods of the Vehicles class (as you will know from building your custom controls). If the vehicle class had a method called moveForward(), it could be accessed via the Car object.

Anyway, this MSDN post sums it up pretty well:
http://msdn.microsoft.com/en-us/library/ms173149.aspx

Thank you for clearing that up; I thought it was something along those lines but couldn't find the exact name for it.

Okay so this brings another question to my mind, since I'm currently working on a game that could use this to my advantage.

Say I have a base class called Weapon and I want sub-classes of each other weapon available; how would I access the basic information via the Weapon class?

For example:

public class Weapon
{
// reload and fire methods are common to all weapons in the game, so this would be useful I wouldn't have to re-type it a million times.
public void Reload(/*args*/)
{
// do reload stuff here
}
}

public class Gun : Weapon
{
public int capacity = 30;
public int contents = 30;
}

public class game
{
Gun myGun = new Gun();

public void update(GameTime gameTime)
{
Gun.Reload(); // ?
}
}

Not understanding 100% on how this part works; I've just mainly used it for creating menus in my game. So now I'm beginning to develop the essentials for gameplay but I want to make it as simple as possible considering there will be about 25 different weapons that the player can use at anytime. I grasp concepts quickly so a brief explanation on how inheritance works in this case would be appreciated. I guess what I'm asking is how would I change the contents of the gun's clip via external class, would I have to pass an argument of how much the gun holds then store it into a variable made public to the gun classes?

Thanks,
Jamie

Because a Gun is also a Weapon(you made it inherit from Weapon), you are able to Reload a Gun.

So I should put my variables for clip capacity and contents into the weapons class since that's common between them all as well?

Just read up on some keywords on MSDN and found the answer on how to change the value of the gun's ammo via the Weapon class. I will be using an out parameter.

public class Weapon
{
public void Reload(int ClipCapacity, out int ClipContents)
{
ClipContents = ClipCapacity;
}
}

public class Gun : Weapon
{
public int clipcontents = 30;
public int clipcapacity = 30;
}

public class game
{
Gun myGun = new Gun();
bool UserForceReload = false;

public void Update()
{
if (myGun.clipcontents <= 0 || UserForceReload)
myGun.Reload(myGun.clipcapacity, out myGun.ClipContents);
}
}

Just a simple example typed up in the reply box. Thanks for all the information guys.

Jamie

I would avoid making members and methods public unless they need to be (ie, required for functionality in development).

C# has a few different access specifiers:

public - the method/property/member is visible to all other classes under any context

protected - the method/property/member is visible to derived classes only

private - the method/property/member is not accessible outside the class' scope

internal - the method/property/member is only accessible inside of the current assembly

static - the method/property/member does not require the class to be instantiated and can be accessed via ClassName.StaticMethodName (not really an access specifier but worth noting - static members can have specifiers as well ie protected static void myMethod())


Your Weapon class will not have access to ClipContents, since it is a member of a derived class. In fact, Reload should be a member of Gun, not weapon, since not all 'weapons' will need to be reloaded (assuming you are using weapons other than guns - if not then a base class of type Gun would be a better solution)

I already had the information you posted, but thank you for your reply. I've tested it and it worked for me in XNA Framework. I have no idea about Windows Forms Apps, but it should apply the same. True, not all weapons have to reload in a general sense but I'm not building a general sense war game. My game is more of the Horror genre of game and will not be using modern technology. So I will figure out which class I want to use as a base when my attention is drawn back to the subject. I develop different sections at different times just because I'm working alone and my ideas don't stick around very long unless I do something with them right away (ADHD - Shiny Object Alert) and so I've developed 3 chapters of my game without even implementing all weapons. I've only implemented a standard gun (based off of an AK47) that I'm using for debugging at the present time. However, I've done some reading on the MSDN site and found a few keywords and their uses that I didn't know existed and even those I knew about but didn't fully understand.

// finally
string[] x = new string[5];

try
{
MessageBox.Show(x[x.Length + 1]);
}

catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

finally
{
foreach (string s in x)
MessageBox.Show(s);
}

// continue

for (int i = 0; i < 100; i++)
{
if (i < 50)
continue;

MessageBox.Show(i.ToString());
}

Those are just two examples of things I didn't even know existed. From what I understand the Finally block allows the developer to handle un-handled code from the try block, in case the try block fails, do something else. The continue keyword is used in the same manner as the break keyword. This keyword will skip the following lines of code and restart/exit a loop/method. In this case, if 'i' is less than 50 we won't display a message box with the value of i.

I do thank you all for your replies, I've learned a great deal about inheritance today and learned a few things I didn't even know. This thread has been answered officially so I will mark it as solved.

Thanks,
Jamie

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.