Sign in to follow this  
GraySnakeGenocide

Creating a new instance of a class to call a methood, C#

Recommended Posts

I'm not understanding why this is being done. Basically, there is one class, Program (the default one). These tutorials/this code from a book, create a new instance of the class to call the method or something. When, as far as I understand, the same thing can be done WITHOUT creating a new instance of the only class in the program. So the question is, Why in the heck are they creating a new instance if you don't need to, to call a method. Does is have something to do with the static keyword? I'm just not getting it.

[code]using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class MyClass
{
public void SomeMethod(int firstParam, float secondParam)
{
Console.WriteLine("Here are the parameters received: {0}, {1}",
firstParam, secondParam);
}
}
public class Tester
{
static void Main()
{
int howManyPeople = 5;
float pi = 3.14f;
MyClass mc = new MyClass();
mc.SomeMethod(howManyPeople, pi);
}
}[/code]

[url="http://www.youtube.com/watch?v=B8RD1xihQJo&feature=related"]http://www.youtube.com/watch?v=B8RD1xihQJo[/url]
:24 is the issue
[url="http://www.youtube.com/watch?v=rMFOWhao2RQ&feature=related"]http://www.youtube.com/watch?v=rMFOWhao2RQ[/url]
:44 is the issue

Share this post


Link to post
Share on other sites
Actually there are two classes here MyClass and Tester, so to access SomeMethod from Tester you have to instantiate the object "[color=#1C2837][font=CourierNew, monospace][size=2][color=#660066]MyClass[/color][color=#000000] mc [/color][color=#666600]=[/color][color=#000000] [/color][color=#000088]new[/color][color=#000000] [/color][color=#660066]MyClass[/color][color=#666600]();"[/color][/size][/font][/color] so it's methods can be used from Tester.

So in this case you do need to create an instance of MyClass to use SomeMethod.

Share this post


Link to post
Share on other sites
You can create static classes which will be initialized by the application start.

When the methods are declared static too you can call them simple with

MyClass.Method();

You can still create instances of this class, if you only want to have one instance then you should take a look at the singleton pattern (google, wikipedia).

Share this post


Link to post
Share on other sites
[quote name='Dr1fter' timestamp='1305460676' post='4811040']
Actually there are two classes here MyClass and Tester, so to access SomeMethod from Tester you have to instantiate the object "[color="#1C2837"][font="CourierNew, monospace"][size="2"][color="#660066"]MyClass[/color][color="#000000"] mc [/color][color="#666600"]=[/color] [color="#000088"]new[/color] [color="#660066"]MyClass[/color][color="#666600"]();"[/color][/size][/font][/color] so it's methods can be used from Tester.

So in this case you do need to create an instance of MyClass to use SomeMethod.
[/quote]

oh, I must've overlooked Tester because I thought it was a regular method, didn't see the Class part.

Share this post


Link to post
Share on other sites
You should remember it like this:

Static fields and methods can be accessed without using an instance of a class, and will as a result apply to all instances of a class
eg. Say you have a class with a static integer field, you can set that integer value without needing a class instance, and that integer field will hold the same value in each instance of that class

Non-static fields and methods [b]always[/b] need an instance of a class to be manipulated/called, and non-static values will only apply to that single instance

Share this post


Link to post
Share on other sites
[quote name='Radikalizm' timestamp='1305463808' post='4811057']
You should remember it like this:

Static fields and methods can be accessed without using an instance of a class, and will as a result apply to all instances of a class
eg. Say you have a class with a static integer field, you can set that integer value without needing a class instance, and that integer field will hold the same value in each instance of that class

[b]Non-static fields and methods always need an instance of a class to be manipulated/called, and non-static values will only apply to that single instance[/b]
[/quote]

why would you need to do that if there's only ONE class?

I'm still a beginner at this so a lot is still new/unknown to me. I've been bouncing between a million books.

Share this post


Link to post
Share on other sites
[quote name='GraySnakeGenocide' timestamp='1305464788' post='4811063']

why would you need to do that if there's only ONE class?

I'm still a beginner at this so a lot is still new/unknown to me. I've been bouncing between a million books.
[/quote]


The thing is that most programs do not contain just one class, this example you've seen did have just one class but that's a very exceptional case
Using static methods and fields everywhere is a very bad coding practice (there have been countless discussions about how the static keyword clashes with a good object-oriented design), so the example you saw probably just wanted to show how to properly make new instances of a class and how to manipulate their data in a correct manner

You'll see the big picture eventually, just keep on reading and practicing

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this