Jump to content
  • Advertisement
Sign in to follow this  
jwbowyer

Class, Methods, Assemblies, Oh my

This topic is 3765 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

Upon recommendation of this board and some personal research of mine I've started to learn C#. I downloaded the Visual C# 2008 Express Edition, and have been searching the web for beginner tutorials to start me off until I can get a hold of a book. I went to the stickied thread containing links to some tutorials, and found a few beginner tutorials, but nearly all contain a single problem. Its as if they expect you to fully understand what certain keywords mean. Things like class, Methods, Assemblies, instances, and inheritance. I know they're all used to break the problem (the program) down, but I'm not exactly sure how. My understanding of programming through basic languages was in this order: > initialize program > initalize important variables (things like graphics, sound, global variables) > start main function > initalize the overall program loop (usually exited by hitting the esc key) > end main function > start different functions below (movement, enemy AI, weapons, etc) Now I'm guessing using an object oriented programming language is different in some ways. For instance I have a Hello World program that a tutorial directed me to create.
using System;

public class HelloWorld
{
    public static void Main()
    {
        //Print Hello World
        Console.WriteLine("Hello World!");
        Console.ReadKey();
    }
}

In explanation of the "public class HelloWorld" he writes "The keyword class is used to define a class in C#. The keyword public before the word class is the access modifier for the class, indicating that any assembly can create an instance of the class. " What does he mean by any assembly can create an instance of the class HelloWorld? Thanks for anyone who decided to read all of this and try and help me? I'm just a little confused. :P

Share this post


Link to post
Share on other sites
Advertisement
Classes are logical units that represent either a discrete object or a collection of functionality. A few examples would be a File class, which would represent a file and would allow you to open, close, and delete it, and a Math class, which would contain a collection of math related functionality.

The method you outlined is typical for a C style program. In C#, you can think of classes as groups of both methods (another name for functions) and variables. Things get more complicated when you throw in inheritance, but you can keep it very simple for now.

The keyword public specifies the visibility of a code element. Other examples of visibility keywords are private, protected, and internal.

Every class you write can be thought of as a blueprint for a new object. This is different from actually having an object, however. When someone refers to an instance of a class, it means they have used the class blueprint to make a new object based upon the class. For example:


class MyClass
{
public void Hi()
{
Console.WriteLine("Hi");
}
}



On its own, that class does nothing. It's just a blueprint for objects you could create using it. Now, if you do this:


MyClass c = new MyClass();
c.Hi();



You can now use the new instance of the class, and any methods that are connected to it.

An assembly can be thought of as a program unit. In the case of a simple program, the .exe you are creating is an assembly. In .NET, DLL files are also assemblies. When you say a class has a public visibility, it means that if another program adds your program as a reference, it can access that class and use it in its own programs. This allows you to create libraries by creating assemblies that don't contain a Main method, but have a bunch of public classes that another program can reference and use.

Share this post


Link to post
Share on other sites
Well I can see how that would definitely be confusing if this is your first experience working with object-oriented programming! I'd recommend taking a bit of time learning the basics of OOP, which is really just working with classes and methods. Inheritance is a bit of an advanced topic so I wouldn't worry about that just yet...and an Assembly is a concept that is part of the .NET Framework and doesn't have much to do with OOP itself (for now you can think of your Assembly as being your entire program).

If you do some searching, you should be able to find some OOP tutorials and reference material. Some of them may actually be C++ or Java tutorials, but the basics still very much apply to C#. The Wikipedia page on OOP actually lays out the basic terms and explains what they mean in a non-language-specific way, so that's probably not a bad place to start out.

Share this post


Link to post
Share on other sites
Ok... I think things make a bit more sense now. Let me see if I get this right.

First of all I'm guessing the heirarchy is then:

Assemblies (Program Units)
Classes (Collections of methods)
Methods (Also known as functions where the meat of the work is done)

In your example you created a new instance of the myClass class. Is this new instance of the class designated by the "c"? So if you wanted to create multiple instances of the same class you could differentiate using the instance's name? Like c.Hi(), d.Hi(), e.Hi() all call an instance of that method and can do different things with it while keeping separate from the others?

And the wikipedia link was quite helpful. Thanks. =)

Share this post


Link to post
Share on other sites
I don't understand why the book would go into those topics early on. Personally I would put those things in the back of my mind until you encounter object oriented programming later in the book. The first things you need to learn are Variables, data Types, Evaluations, Loops, and Methods. If your book doesn't start with these then I would get a new book. After you get these down the book should start devling into object oriented programming.

Share this post


Link to post
Share on other sites
Quote:
Original post by Chris27
I don't understand why the book would go into those topics early on. Personally I would put those things in the back of my mind until you encounter object oriented programming later in the book. The first things you need to learn are Variables, data Types, Evaluations, Loops, and Methods. If your book doesn't start with these then I would get a new book. After you get these down the book should start devling into object oriented programming.


I agree, and I don't have a book yet. I'm doing online tutorials at the moment (specifically this Master C Sharp Tutorial.) I'd much rather learn how variables are called, the input/output commands, how to use the loops, calling functions (or methods), and creating arrays... Seems much more logical to learn the basic layout of commands in the language before moving on to the more abstract stuff.

Share this post


Link to post
Share on other sites
The requirement of OOP syntax right up front makes C# a bit harder than some of the higher level languages. I’m betting it’s like a hitting a brick wall: you see the words, but you can’t figure out the importance or large scale relevance of a single thing.

OOP is really a design concept, so no amount of reading about the syntax will help you learn OOP. And like other design concepts, you gain an understand of them by writing programs over and over and over, with increasingly complex design requirements. That’s why I disagree with the advice to focus heavily on understanding OOP upfront. Syntax explanations are useless: you can’t see the relevance of the syntax in the first place. And there is no way to imbue design knowledge. So my advice is to actually not worry about the OOP syntax, to the extent you can get away with it. As you start writing programs, you’ll be forced to make up designs, and about sometime you should start seeing how OOP would be very useful.

Just to throw this out there, early on when trying to understand OOP, I looked at it from the idea of a black box. A class gives you a black box, with some hidden mechanisms inside. All you see is the box, and any interface it implements to be used.

EDIT:
Quote:
Is this new instance of the class designated by the "c"
No, the name is irrelevant. Usually, one uses a more descriptive name, so by that I mean it has more than one letter. Or two. Or three. Four letter words are not usually used unless you are very angry.

Share this post


Link to post
Share on other sites
I spent a couple minutes looking at those tutorials, and.... ugh. That guy should not be teaching people. He does not have the skill of explaining a concept to someone who does not already know it. Do yourself a favor: pick up a good book on C#. There's plenty of them out there, with copious Amazon reviews to help steer you to a good one, and unlike that page they were written by the sort of people who get paid to write books on C#.

Share this post


Link to post
Share on other sites
I agree with that. All the basics in C# can be learned in the default class that is created. Just write all your code in main() and you should be fine.

If you are using a Windows Form you should try and learn how event based programming works. Basically event based means something happens on the form which causes code on the form to execute. For instance if you click on a button the code under that botton executes and something happens. This is probably the most imporant thing to understand for a new programmer working with Forms applications.

Share this post


Link to post
Share on other sites
Quote:
Original post by oler1s
The requirement of OOP syntax right up front makes C# a bit harder than some of the higher level languages. I’m betting it’s like a hitting a brick wall: you see the words, but you can’t figure out the importance or large scale relevance of a single thing.


Yes! Exactly! I'm trying to figure out exactly how it can be used in later applications and can't yet grasp it.

Quote:
Original post by oler1s
EDIT:
Quote:
Is this new instance of the class designated by the "c"
No, the name is irrelevant. Usually, one uses a more descriptive name, so by that I mean it has more than one letter. Or two. Or three. Four letter words are not usually used unless you are very angry.

Yeah I guessed the instance was like a variable =P, it could be called anything and whatever name is used is the designator of that instance. In that example that name being "c."

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!