RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Looking for Good Tut

AntKneeM

Wanderer
Looking for Good Tut

Iam not sure if this is allowed or not, but ive searched and ive yet to come accross and C# Tutorial to keep my attention long enough to explain to me what a class is ( Example ) iam looking for a decent C# Tutorial that is extensive BUT breaks things down and explains things to me and can help me understand better the C# language ?? so if anyone knows of a good one for beginners please let me know!~
 

Jeff

Lord
AntKneeM;719520 said:
Iam not sure if this is allowed or not, but ive searched and ive yet to come accross and C# Tutorial to keep my attention long enough to explain to me what a class is ( Example ) iam looking for a decent C# Tutorial that is extensive BUT breaks things down and explains things to me and can help me understand better the C# language ?? so if anyone knows of a good one for beginners please let me know!~

Goto your local book store, or online such as amazon and buy the bool "Learning C#" by O'Reilly Publishing. This book is my #1 recommendation to people looking to really learn C# but can't quite grasp the C# tutorials on the web (same problem I had ). Anyway this book is awsome cause it descibes things with metaphors(which I love).

But to answer your question, "What is a class". The simply answer would be to descibe it like school. You goto school and you have 'classes' each 'class' contains information reguarding a subject. Generally, when you goto school you start with the basics and move on up the ladder to more advanced things, but they are all relative to the base class. In programming you have the same principals. You start making objects with a base class and move on up with new classes with the same base class. Lets look at an example.

Lets take my metaphor and pretend we are writing code for Math 101.

So what do we know about Math 101. We know that it is going to be able Math, and we know we will learn some basic math functions. So we would start out with

Code:
public class Math101
{
    public Math()
    {

    }
}

Now, in Math 101 we learn things like Add, Subtract, Multiply and Divide. So our base Math class needs to have these functions(also called methods).

Code:
public class Math101
{
    public Math()
    {

    }

    public int Add(int a, int b)
    {
        return a + b;
    }


    public int Subtract(int a, int b)
    {
        return a - b;
    }


    public int Multiply(int a, int b)
    {
        return a * b;
    }


    public int Divide(int a, int b)
    {
        return a / b;
    }
}

So there, now we have our basic math class we have called Math101

If we wanted to use our Math101 class in some code we could now do so like this

Code:
Math101 mathClass = new Math101();
int result = Math.Add(2, 2);

In this example result would be 4 since our add function returns the result of 2 + 2. Now, like I was saying earlier, in school we have a base class and we move up to more advanced classes. However something to keep in mind is that the base functions always stay with us as we progress through the more advanced classes

In programming we call this 'Inheritance'. Basically this means our new class will contain the same functions and functionality of its base class, just as Math 102 would contain the same math functions in Math 101.

So for example
Code:
public class Math102 : Math101
{
    public Math102() : base()
    {

    }
}

Now since this is a more advanced class it needs new, more advanced functions. So lets assume in Math 102 we learn how to average numbers. We would need to Add all the number supplied to our function together then divide them by the count of numbers passed to be averaged. Well since we already learned how to Add and Divide in our Math 101 class, we can use these same functions to help us with our work.

Lets take a look:
Code:
public class Math102 : Math101
{
    public Math102() : base()
    {

    }

    public int Average(int[] values)
    {
        int added = 0;
        for(int i = 0; i < values.Length; i++)
        {
            added = Add(added, values[i]);
        }

        return Divide(added, values.Length);
    }
}

See how I am able to use the functions from the base class(Math 101). This is basically how classes work, and how they should be used.

If you have questions regarding this please feel free to ask.
 

AntKneeM

Wanderer
Thx Jeff, Appreciate it... ill deff check that out that is what i was looking for for something to break things down for me, and that was and example i know what a class is but thx :)
 

Jeff

Lord
AntKneeM;719562 said:
Thx Jeff, Appreciate it... ill deff check that out that is what i was looking for for something to break things down for me, and that was and example i know what a class is but thx :)

np you can always ask questions here to :)
 
Jeff;719541 said:
Code:
Math101 mathClass = new Math101();
int result = Math.Add(2, 2);
I could be wrong, but my thinking was that this should read:
Code:
Math101 mathClass = new Math101();
int result = mathClass.Math.Add(2, 2);
I was just thinking that mathClass would have to be called before you could call Math() because that function belongs to Math101, which is being referenced as mathClass.

Pardon my poor terminology; and definitely feel free to correct me on any and all aspects of the above. I too am learning. :D
 

daat99

Moderator
Staff member
Storm33229;719615 said:
I could be wrong, but my thinking was that this should read:
Code:
Math101 mathClass = new Math101();
int result = mathClass.Math.Add(2, 2);
I was just thinking that mathClass would have to be called before you could call Math() because that function belongs to Math101, which is being referenced as mathClass.

Pardon my poor terminology; and definitely feel free to correct me on any and all aspects of the above. I too am learning. :D

You are correct in your remarks but to Jeff's defense, he was just posting pseudo code for an example and not a real working code.
 
daat99;719640 said:
You are correct in your remarks but to Jeff's defense, he was just posting pseudo code for an example and not a real working code.
Oh, wasn't quite aware of that. ^_^ sorry.
 

Jeff

Lord
Storm33229;719644 said:
Oh, wasn't quite aware of that. ^_^ sorry.

not to mention it would be

Code:
Math102 math102Class = new Math102();
int result = math102Class.Add(2, 2);
 
Jeff;719659 said:
not to mention it would be

Code:
Math102 math102Class = new Math102();
int result = math102Class.Add(2, 2);
:eek: Yeah, just noticed that Add and Math are in-fact both functions.:eek:
 

Jeff

Lord
Storm33229;719672 said:
:eek: Yeah, just noticed that Add and Math are in-fact both functions.:eek:

Math isnt a function its a constructor, its what you call to create the object.
 

Jeff

Lord
Storm33229;719756 said:
I thought anything with () after it was a function? =/

Technically i spose it is, but it's return type is its self. Thus its called a constructor, as in how you construct the object.
 
Top