Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: Can someone explain c# classes, please?  (Read 2658 times)

0 Members and 1 Guest are viewing this topic.
Can someone explain c# classes, please?
« on: October 18, 2010, 02:09:17 am »
  • *
  • Reputation: +12/-2
  • Offline Offline
  • Gender: Male
  • Posts: 4849
I have two books on c#, and a few more based on XNA which include information on c# classes.

I can't grasp it for some reason.  Like abstract, public, void, private, generics, etc.
I am trying as hard as I can to get away from Game Maker and move into more real programming with c#.
Trying to figure out how exactly the c# OOP with classes and such works.

Here's what I have been doing on anything I make in C# with XNA:
link.cs
Code: [Select]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsGame1
{
    class link
    {
        public int x ;
        public int y ;

        public bool moveable = true; // checking to see if link is moveable
        public bool attacking = false; // checking to see if link is attacking
        public bool canattack = true; //checking to see if Link can attack, used so the player cannot just hold down Z to attack over and over

        public bool canmoveright = true; // use this variable for movement so the player cannot move in diagonals
        public bool canmoveleft = true;
        public bool canmoveup = true;
        public bool canmovedown = true;


        public int globalLink; // link's actions

       
    }
}

For some reason I keep thinking there has to be better ways of declaring the class and even what I am trying to accomplish.
I understand everything I did here.

HUD.cs
Code: [Select]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsGame1
{
    class hud
    {
        public int x = 0;
        public int y = 0;


        //variables for displaying the number of the following
        public byte bombs;
        public byte keys;
        public short rupees;

        //public bool dungeoncompass = false;

        //overworld compass
        public int compass_x;
        public int compass_y;


        //variables for displaying the X and Z items in ues
        public byte invSlotX;
        public byte invSlotZ;

    }
}
Same with the HUD. I understand what i am doing but it seems like the class should be different for some reason.

Then in the main Game1.cs(the main class in a XNA project)
I understand the link objPlayer = new link();//all variables for Link will be in the link class creates a instance or object of the link.cs and gives it a name objPlayer with all the bools and ints i used in the link.cs


So far it seems like i have a basic understanding of classes.  but then you start doing this just as an example:
public link ()
{
   something here
}

You lose me. 
I keep reading the books and just don't seem to understand public and private other than the fact of something to deal with changing variables in the actual class itself or something: public you can change the variables, private you cannot....or something.

Anyone have any good insight?
Logged
  • Super Fan Gamers!

Mamoruanime

@Mamoruanime
Re: Can someone explain c# classes, please?
« Reply #1 on: October 18, 2010, 02:13:18 am »
  • ^Not actually me.
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 9786
Look up Static Methods and Functions in your book- it should be able to help on the issue better than I can :P
Logged
Re: Can someone explain c# classes, please?
« Reply #2 on: October 18, 2010, 03:18:30 am »
  • Doesn't afraid of anything
  • *
  • Reputation: +42/-0
  • Offline Offline
  • Gender: Male
  • Posts: 7002
There are three levels of what you would refer to as encapsulation.  Public, Private and Protected.

Public means that the member can be accessed by anything that is in the same scope as the object.  I.e., you can access it with the dot operator from any method.
Private means that only members of the OBJECT'S class can access the member.  I.e only functions that are within the class definition.
Protected is the same as private, but it has a little addition to it that allows inherited classes to access the member.

For example...

public:
Code: [Select]
class x
{
     public int a;
     private int b;
     protected int c;

     public int someFfunction()
     {
            return b;
     }
};

class y
{
     public void otherFunction(){}
     private int n;
};

class z : public x
{
    public void thisGuy()
    {
          return c;
    }
};

a object1;
b object2;
c object3;

object1.b +=  object1.a;  //this is ok
object2.n += object1.b //this however, is not.  The + operator for object2.n is trying to access the private variable, object1.b
object2.n += object1.someFunction(); //this is ok.  The b variable is being accessed through someFunction(), which is a member of object1, and therefore has access to b.

And this applies to ALL object-oriented languages, not just C#.
« Last Edit: October 18, 2010, 03:20:30 am by MG-Zero »
Logged



i love big weenies and i cannot lie
Re: Can someone explain c# classes, please?(New ...
« Reply #3 on: October 18, 2010, 03:25:17 am »
  • *
  • Reputation: +12/-2
  • Offline Offline
  • Gender: Male
  • Posts: 4849
Okay.  I understand it a little better.
Thanks.  I'll have to keep reading to further grasp this.  Don't know why I am having such a hard time.
I blame RADs >:(  :P

So far, I am thinking of classes as new variables.  i don't know if that is a good way or not.

Like example
Code: [Select]
class playersprite
{
    public int x;
    public int y;
    public int currentframe;
}


Since one of the ways of creating an object that uses the class is as such:
playersprite  objPlayer;

It seems to me just as you declare a varible as:
var player;
or
int x;
..., that a class is similar to a variable.  Then:
objPlayer = new playersprite;

Tells the system to create an object of playersprite into the variable objPlayer.

Am I on track so far?
« Last Edit: October 18, 2010, 03:46:17 am by Theforeshadower »
Logged
  • Super Fan Gamers!
Re: Can someone explain c# classes, please?
« Reply #4 on: October 18, 2010, 05:24:44 am »
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3725
So far it seems like i have a basic understanding of classes.  but then you start doing this just as an example:
public link ()
{
   something here
}
That is a constructor (and without parameters it becomes the default constructor).  This is called when you create an instance of a class that is defined and declared in a .cs file. For example:

Code: [Select]
public FooClass fooInstance;

....

fooInstance = new FooClass(param1, param2);

The "something here" is code that is executed when an object is created with that particular constructor and only when it is created.


Okay.  I understand it a little better.
Thanks.  I'll have to keep reading to further grasp this.  Don't know why I am having such a hard time.
I blame RADs >:(  :P
Maybe because you started out learning XNA, without having a good grasp of C# and object oriented languages. My advice is to just read a book about C# first.

So far, I am thinking of classes as new variables.  i don't know if that is a good way or not.
Not completely true. Classes are the definitions of instances. I often find a simple metaphore makes it easy to understand. Variables are boxes with content. And types are the checklist for the content in a box of the type. A class is a user defined type, thus a checklist made by a programmer.

I don't think that made it easier.

Like example
Code: [Select]
class playersprite
{
    public int x;
    public int y;
    public int currentframe;
}


Since one of the ways of creating an object that uses the class is as such:
playersprite  objPlayer;

It seems to me just as you declare a varible as:
..., that a class is similar to a variable.  Then:
objPlayer = new playersprite;

Tells the system to create an object of playersprite into the variable objPlayer.
Yes, as long as objPlayer is of the type playersprite. Then objPlayer = new playersprite; will assign an instance of playersprite to objPlayer.

My advice is go read a book about C# and OOP first before you go into XNA.
« Last Edit: October 18, 2010, 06:35:14 am by Niek »
Logged
Re: Can someone explain c# classes, please?
« Reply #5 on: October 18, 2010, 12:39:33 pm »
  • *
  • Reputation: +2/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1767
When I get home from school today I can upload the lectures from my C++ class two years ago to help explain some of it with some visuals.

So far, I am thinking of classes as new variables.  i don't know if that is a good way or not.

Eh... not the best way to see it. I think of a class like an object in Game Maker. Then when you call and instantiate your class by creating an object by doing "myClass myObject = new myClass(*parameters*);" would be putting that object into a room. You can't really think of a class as another variable because it can hold variables and functions/methods within it.
Logged
  • https://colbydude.com
Re: Can someone explain c# classes, please?
« Reply #6 on: October 18, 2010, 04:59:50 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 2245
I sort of see classes as programs themselves, designed to handle a specific task. For example if you look at link, you can quite easily break it up into several different tasks, for me I'd probably have a sprite class, that will store sprite information and also be responsible for loading the sprite, then I would have another class that would take that as a parameter and would be responsible for keeping track of frame cycle.

abstract classes are those where the implementation hasn't been fully defined, say for example you have 5 different objects, each of those having the same things in common, but lets say each one of those draws differently, so each class would inherit from that abstract class, which would contain a draw method that hadn't been implemented, and each class that inherits from it would then have to implement their own draw method.  Then lets say you put those into a list that would only contain instances of that abstract class, then as you go through the list you would call the draw method of that abstract class, which would then call each draw method as implemented above.

If you want to relate it to game maker, you can think of it as how each object has a step event that it defines itself, all game maker knows is that each object has a step event, and to call it when going through the object loop, but it has no idea what that step event does, hence the term abstract.

If you want to relate constructors to game maker, think how you use instance_create, when you create an instance, you are required to pass x and y coordinates. that is no different then creating a constructor that would look like:
Code: [Select]
class Link
{
Link(int x, int y)
{
// assign my x and y coordinates
}
}
and would then be instantiated like
Code: [Select]
variablename = new Link(234, 34); // my x and y coordinates

you can also define as many as these as you like as long as the parameters are different
Code: [Select]
class Link
{
Link()
{
// assign my x and y coordinates to zero as the default
}

Link(int x, int y)
{
// assign my x and y coordinates
}
}

constructors can also inherit from each other

Code: [Select]
class Link
{
Link() : this(0, 0) // call the Link(int x, int y) constructor first
{
// do whatever now
}

Link(int x, int y)
{
// assign my x and y coordinates
}
}

Last point i'm going to make, you should not use int x and int y to store the coordinates, XNA already has a class that you should use called Vector3 that takes x, y, z, as to why you should use Vector3 instead of Vector2 which just has x and y, while you are simulating working with only a 2d plane, you are still really working on a 3d plane, and all your transforms will be required to reflect that, and then it saves you the trouble of having to create a Vector3 every time you need to do a transform.
Logged
Re: Can someone explain c# classes, please?
« Reply #7 on: October 18, 2010, 08:14:43 pm »
  • *
  • Reputation: +12/-2
  • Offline Offline
  • Gender: Male
  • Posts: 4849
Thanks, Windy.

It's helping me alot.  Just got into the methods and overloading(etc) with classes in my book.
After watching your examples and reading my book, I think I am understanding better.


Niek:  Using XNA with C# is no different to me than just C# because I already jumped into XNA all the way back to Refresh 1.0 so I know alot of the technical stuff with XNA now I just need to get a grasp on C# to implement it properly and not have one huge code file.  I need to learn to break it down into methods and classes instead of one big procedural setup.

That and I have no patience for making mailing labels and making a hex dump of a file directory.  I know they help with beginners but i take what I read from those examples and implement them into XNA to learn.
Logged
  • Super Fan Gamers!
Re: Can someone explain c# classes, please?
« Reply #8 on: October 19, 2010, 05:44:32 am »
  • *
  • Reputation: +12/-2
  • Offline Offline
  • Gender: Male
  • Posts: 4849
Okay.

So, I got into methods a bit.

Static methods affect the CLASS therefor, it affects all instances of the class(or objects that use the class).
Nonstatics affect only the instance of the class, not the class itself.

Do I have that right?
Logged
  • Super Fan Gamers!
Re: Can someone explain c# classes, please?
« Reply #9 on: October 19, 2010, 07:08:48 am »
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3725
No static methods are methods that perform calculations independent of the state of any instance of a class. They are also called without instantiating an instance of that class. Common uses of static methods are conversions, independent calculations (like math class methods), instance factories and in a Singleton Pattern. Static methods cannot access dynamic data of an instance without that the instance is provided as a parameter or as a return value of another static method.

Static variables however is shared data overall the instances of a class. Thus when one instance changes the value of a static variable it is changed for all the instances. An example of a good use for something like that is a counter that counts the number of instances created of a class.
Logged
Re: Can someone explain c# classes, please?
« Reply #10 on: October 19, 2010, 07:27:44 am »
  • *
  • Reputation: +12/-2
  • Offline Offline
  • Gender: Male
  • Posts: 4849
hmm...Well, don't know if I understand how you put it.  Sorry, like I said, classes are confusing me.


I just wrote something up real quick into a class.
Code: [Select]
public class player
    {
       public int x;
        public int y;

       //Methods called to update x and y that will be called upon key presses
       public void increaseX()
       {
            x = x + 1;
       }

       public void increaseY()
       {
           y = y + 1;
       }

       public void decreaseX()
       {
           x = x - 1;
       }

       public void decreaseY()
       {
           y = y - 1;
       }
    }


I know that is an unnecessary way of movement but I am trying to grasp classes and methods.

If I call each method respectively to the arrow keys or WASD, would it work?

Such as:
Code: [Select]
           if (Keyboard.GetState().IsKeyDown(Keys.Right))
            {
                objPlayer.increaseX();
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Left))
            {
                objPlayer.decreaseX();
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Up))
            {
                objPlayer.decreaseY();
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Down))
            {
                objPlayer.increaseY();
            }

Thus far I get no errors in the compiler.  
« Last Edit: October 19, 2010, 07:35:49 am by Theforeshadower »
Logged
  • Super Fan Gamers!
Re: Can someone explain c# classes, please?
« Reply #11 on: October 19, 2010, 07:36:56 am »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 2245
Technically, it isn't changed for all instances of a class, because a static variable is not a part of an instance.
If you want to relate it to game maker, think about how global variables work, only instead of being completely global, they are limited to an object of that particular type.
Logged

Mamoruanime

@Mamoruanime
Re: Can someone explain c# classes, please?
« Reply #12 on: October 19, 2010, 07:37:59 am »
  • ^Not actually me.
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 9786
Unfortunately confusion with classes comes from using tools like MMF and GM :P I have to call a "BAD BABY BAD BABY" on Niek for suggesting GM as a good starter for programming :(

Any program that claims to have oop functionality that doesn't allow the user to create (or even understand) classes is probably a horrible starter for the game industry.

It took me a while to wrap my head around a lot of it and I still have issues with a lot of things :P
Logged
Re: Can someone explain c# classes, please?
« Reply #13 on: October 19, 2010, 07:44:24 am »
  • *
  • Reputation: +12/-2
  • Offline Offline
  • Gender: Male
  • Posts: 4849
Well, my implementation seemed to work when i put it altogether and threw an image into the mix to see it in action.
So far, I understand how to use it.  Just need to learn where to use and more advanced functions.

I think I will just expand on my current implementation and include changing the sprite.
Logged
  • Super Fan Gamers!
Re: Can someone explain c# classes, please?
« Reply #14 on: October 19, 2010, 07:59:30 am »
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3725
Unfortunately confusion with classes comes from using tools like MMF and GM :P I have to call a "BAD BABY BAD BABY" on Niek for suggesting GM as a good starter for programming :(
It is kinda a thing of perspective. I don't think it is a bad place to start, because it has a lot of things already done for you and I don't find the jump from one to another that big. But than again I have a skewed perspective. I started programming with Java.


And classes aren't that confusing as long as you let go of the idea that a class is something tangible in your code something to work with. A class is nothing more than an abstract definition. Just like being human is a abstract definition. You can see a person and look at his/her features and you see that they fall in the classification of human. You can say that person is an example of human. But you can't say that person is the object human, because there is no object human.

And then don't try to understand the complete complexity and just let it be. Let your self be confused but don't get bothered by being confused. The most of it is just making mistakes and getting things right. Don't ask why. It just is. And then the confusion will say POOF!.
Logged
Re: Can someone explain c# classes, please?
« Reply #15 on: October 19, 2010, 05:42:34 pm »
  • (y)(;>.<;)(y)
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3293
I'd recommend not learning with XNA. If you absolutely must have pretty graphics, go for a simpler framework that you can build in rather than having to build from (I've heard good things about SFML).

Personally I'd suggest you try and create a text-based adventure game. I mean a proper one, where you have the cool

You are in a room. There is a fridge, a bed and a book.
> open fridge
The fridge contains one dry potato inside, no lie. Not even bread, jam.
> sleep
You can't get no sleep.

Coding these is actually rather interesting, believe it or not, and it'll introduce you to important concepts without dropping you in the deeper end of things (so-to-speak) =)
« Last Edit: October 19, 2010, 07:07:32 pm by TheDarkJay »
Logged
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.554 seconds with 69 queries.