Newbie question - Class, Objects, methods
-
Freitag, 2. März 2012 12:50
Okay I a finding it hard to understand the following:
What is the difference between a Class, Object and Method
And what is their relationship as well?
I have tried to read from various sources but I can not understand it :(
- Verschoben CoolDadTxMVP, Moderator Montag, 5. März 2012 14:54 Language related (From:Visual C# General)
Alle Antworten
-
Freitag, 2. März 2012 13:24
Hi Den-i,
Well first of all a method is where you add code to tell for example a button to do something for example:
private void btnCallMessageBox_Click(object sender, RoutedEventArgs e) { MessageBox.Show("This button works"); }so basically, any method is where you tell you application what to do and when to do it (for example you create a sepperate method and call it in your button's click event. Then a class holds various methods that will/can be used in different forms. so all you need to do is call that method in a form where it will be used... for example
using System.Forms; namespace Test { class testing { public void SendMessageBox() { MessageBox.Show("Message"); } } } private void btnCallMessageBox_Click(object sender, EventArgs e) { testing TestMsg = new testing; TestMsg.SendMessageBox(); }This is using a method from a class...An object can be a lot of things, read up on these urls...
http://msdn.microsoft.com/en-us/library/ms173110.aspx
http://msdn.microsoft.com/en-us/library/9kkx3h3c(v=vs.71).aspx
http://msdn.microsoft.com/en-us/library/527aztek(v=vs.71).aspx
Hope it helps :)
Rising Storm Technologies
- Als Antwort markiert Jason Dot WangMicrosoft Contingent Staff, Moderator Freitag, 23. März 2012 06:32
-
Freitag, 2. März 2012 13:24
Hi,
I will try to build up a comparison with the real world. There are of course multiple points that does not 100% fit but I hope that it is helpfull for you to understand everything.
A class is a plan how something could be build.
An object is something that was build using a specific plan.
A method is something that I either an object or a plan can do.
So in the real world, you could have a look at a car. A car is an object. This object was build using a plan. So the plan that describes how to build a car would be the class.
A method is now some kind of action you could do e.g. StartEngine, StopEngine, Accelerate, ...
You didn't ask for it, but there are also fields. Fields could be compared with items inside the car. So your car has a motor, wheels, .... You could keep them in so called fields.
Properties are more or less like fields. But see it as some kind of protection. You do not want others to play around with your wheels so you could have a property instead of a field. What is happening now is: Instead of direct access to the wheels, "code" is executed. So when you try to loosen a wheel there is something that checks if the car is standing still (for example).Important is now: You could put stuff on a plan, too. So you could have things there like a counter that tells you how many cars you build so far. Together with an Action called "IncreaseCounter" or so. And the plan also describes: "When you build the car, you have to Increase the counter!". So this would be a static field / property and a static method.
I hope this helped a little bit.
With kind regards,
Konrad
- Als Antwort vorgeschlagen BioGeneZ Dienstag, 6. März 2012 05:23
- Als Antwort markiert Jason Dot WangMicrosoft Contingent Staff, Moderator Freitag, 23. März 2012 06:32
-
Freitag, 2. März 2012 13:27
1st of all, lets clear what OBJECT is:
The object type is an alias for Object in the .NET Framework. In the unified type system of C#, all types, predefined and user-defined, reference types and value types, inherit directly or indirectly from Object. You can assign values of any type to variables of type object. When a variable of a value type is converted to object, it is said to be boxed. When a variable of type object is converted to a value type, it is said to be unboxed. For more information, see Boxing and Unboxing.
class MyClass { void MyMethod() { int a1 = 1; string b1 = "text"; object a2 = (int)a1; object b2 = (string)b2; } }
As you can see every memeber of a class (in upper example I only showed fields) can be an object.
CLASS:
Classes are declared using the keyword class, as shown in the following example:
class TestClass { // Methods, properties, fields, events, delegates // and nested classes go here. }
A class can contain declarations of the following members: Constructors, Destructors, Constants, Fields, Methods, Properties, Indexers, Operators, Events, Delegates, Classes, Interfaces, Structs
METHOD:
A method is a code block containing a series of statements. In C#, every executed instruction is done so in the context of a method. Methods are declared within a class or struct by specifying the access level, the return value, the name of the method, and any method parameters. Method parameters are surrounded by parentheses, and separated by commas. Empty parentheses indicate that the method requires no parameters. This class contains three methods:
class Motorcycle { public void StartEngine() { } public void AddGas(int gallons) { } public int Drive(int miles, int speed) { return 0; } } //Calling a method on an object is similar to accessing a field. After the object name, add a period, the name of the method,
//and parentheses. Arguments are listed within the parentheses, and separated by commas. The methods of the Motorcycle
//class can therefore be called like this: Motorcycle moto = new Motorcycle(); moto.StartEngine(); moto.AddGas(15); moto.Drive(5, 20);
Mitja
- Als Antwort vorgeschlagen BioGeneZ Dienstag, 6. März 2012 05:23
- Als Antwort markiert Jason Dot WangMicrosoft Contingent Staff, Moderator Freitag, 23. März 2012 06:32
-
Montag, 5. März 2012 05:04
Hi Den-i,
I have a long-winded article that talks about the topic you mentioned.Furthermore,it has explained "what does Object Orientation mean?".
Classes, Objects, and Methods in Objective-C
http://www.informit.com/articles/article.aspx?p=102257
I hope it will solve your problem.
Sincerely,
Jason Wang
orichisonic http://blog.csdn.net/orichisonic If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
- Bearbeitet orichisonic Montag, 5. März 2012 05:31
- Als Antwort markiert Jason Dot WangMicrosoft Contingent Staff, Moderator Freitag, 23. März 2012 06:32
-
Montag, 5. März 2012 05:42
On 3/2/2012 7:50 AM, Den-i wrote:> Okay I a finding it hard to understand the following:>> What is the difference between a Class, Object and Method>> And what is their relationship as well?>> I have tried to read from various sources but I can not understand it :(>A class and an object are the same thing. You instantiate an object anda object is based off its class definition as to what the object does.The class/object can have behavior/methods within the class/object thatcan act upon the object itself or behavior/methods within the object canact upon other public objects.Design patterns are recurring solutions to software design problems youfind again and again in real-world application development. Patterns areabout design and interaction of objects, as well as providing acommunication platform concerning elegant, reusable solutions tocommonly encountered programming challenges.You should be able to find free e-books on the above books.- Als Antwort markiert Jason Dot WangMicrosoft Contingent Staff, Moderator Freitag, 23. März 2012 06:34
-
Montag, 5. März 2012 06:09
hi there...
it seems to me that you are trying to learn programming.
ill give you a swift reply that is brief and ill provide you with some links for you to go through and start off your journey in the world of OOB (object oriented programming).
my description will not be technical just to give you a logical push in order for you to grasp it.
a class is where you put all your functions and where you declare all your variables ...
An object is an instance of a class ( meaning that when you create a class and you define functions, you will be able to access these functions from another class using the class object )
A function is a piece of code where you can define an input and a certain output ...
the hierarchy of the previous three is as follows.
Class Aww
{
public void funcA (string a,int b)
{
do this using a, do that using b.... // here you can define a certain action that you function will do.
}
}Class B
{
Aww obj = new Aww(); //here obj is the instance of the class Aww in which you can call the funcA method...
obj.funcA("test",10);
}
Please check this link for full detailed help!
http://www.blackwasp.co.uk/CSharpObjectOriented.aspx
Peter Koueik
- Als Antwort markiert Jason Dot WangMicrosoft Contingent Staff, Moderator Freitag, 23. März 2012 06:34
-
Montag, 5. März 2012 11:26
I suggest you look at Rob Miles books on the web.
C# Yellow Book http://www.csharpcourse.com/
Solutions to problems and creating Applications can use Objects Methods or Functions.
This quick "Hello World" is just pseudo code Math MySum Foo Bob Ted Alice Carol etc.
Math MySum = ( Number A + Numbers B ). You need to first define and declare the building blocks.
The Class Constructor contains integerA IntegerB you then can have an Object .
Creating objects the new Instance MySumA ( IntegerA + IntegerB ).
You can create objects the relationships the types variables Public Private used as you define.
SumNumber MyFunction Methods MySum IntegerA + IntegerB this depends on what you have created :D
Applications might contain Objects or Classes Methods that Handle Events like Mouse( Click ).
This could be a single application or a library Public or Private protected for the values.
I have read Rob's series I get the latest updated book when not busy :)
Martin
Martin Rasch
- Als Antwort markiert Jason Dot WangMicrosoft Contingent Staff, Moderator Freitag, 23. März 2012 06:34
-
Montag, 5. März 2012 12:42
Thanks everyone:
Martin Rasch, Darnold924, Jason Wang, Mitja, Linkit ( from Rising Storm Technologies), Konrad, Peter Koueik (Hopefully I have not missed anyone)
for all your explanations and helpful links. Thank you they were all great and very useful. I think I have a better understanding of it now.
Best regards
Denis aka the newb :)
-
Dienstag, 6. März 2012 05:19
dear Denis,
if your question was answered please don't forget to mark the thread or threads that answered you as answer
so that the thread will close...thank you.
Peter Koueik

