Static in C#

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
EdEown
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 30
Joined: Sat Dec 19, 2009 5:00 pm
Favorite Gaming Platforms: PS3
Programming Language of Choice: C#
Location: Bosnia and Herzegovina

Static in C#

Post by EdEown »

Can any one plzz explain me little bit about static.

I came to tutorial which teaches you about static and it is kind of good but still he didn't explain some things and I got soooo pissed off !!!!xD

I just cant understand what "return" means and why is he keep telling something about instances !

Code: Select all

namespace Tutorial1
{
    class Program
    {
        static int AddNumbers(int number1, int number2)
        {//Note that we declare with the variable in our method and only use the name assigned
            int result;//this is to make it more clear what we're returning
            result = number1 + number2;
            return result;
        }
        static int GetMedian(int number)
        {
            return (number/2);//Here we're returning number back in half.
            /*we could also have done it like this 
              int finishedproduct;
              finishedproduct = number/2;
              return finishedproduct;
             */
        }
        static int GetNumbers()
        {//This method accepts no arguments, it's just set to get the numbers and pass them back
            string entry;
            int entrytranslated;
            Console.WriteLine("Please enter a number");
            entry = Console.ReadLine();
            Console.WriteLine();
            entrytranslated = int.Parse(entry);
            return entrytranslated;
        }
        static void Main(string[] args)//This is our entry point even if it's below everything else
        {
            int product;
            int numberA, numberB;
            numberA = GetNumbers();//Here you see that we're using the method to assign a variable to numberA
            numberB = GetNumbers();//Again we're using GetNumber to assign a variable
            product = AddNumbers(numberA,numberB);//Now we're using add number, feeding it it's required variables and
            //it's assigning a number to product
            Console.WriteLine("The sum of the numbers is " + product);//Here we're showing the product
            product = GetMedian(product);//now product will be assigned the median using GetMedian
            Console.WriteLine("The median of the two numbers is " + product);
            Console.ReadLine();
        }
    }
}
Here is code so you could see what I am learning :P
"There is nothing either good or bad, but our thinking makes it so"
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Static in C#

Post by avansc »

Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
Lord Pingas
Chaos Rift Regular
Chaos Rift Regular
Posts: 178
Joined: Thu Dec 31, 2009 9:33 am
Favorite Gaming Platforms: NES, SNES, Nintendo 64, Dreamcast, Wii
Programming Language of Choice: C++
Location: Hiding In My Mum's Basement With My Pokemon Cards

Re: Static in C#

Post by Lord Pingas »

I'm pretty sure the return statement is just the same in C++... It's just returning your arguments, which is something I should revise on. I think I skipped a large part of the Functions chapter on my book... I'll get right on it :)

Anyway, this should answer your question:
http://www.google.co.uk/search?hl=en&rl ... =&aq=f&oq=

I'm sure you will find something ;)
User avatar
RyanPridgeon
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 447
Joined: Sun Sep 21, 2008 1:34 pm
Current Project: "Triangle"
Favorite Gaming Platforms: PC
Programming Language of Choice: C/C++
Location: UK
Contact:

Re: Static in C#

Post by RyanPridgeon »

EdEown wrote:Can any one plzz explain me little bit about static.

I came to tutorial which teaches you about static and it is kind of good but still he didn't explain some things and I got soooo pissed off !!!!xD

I just cant understand what "return" means and why is he keep telling something about instances !

Code: Select all

namespace Tutorial1
{
    class Program
    {
        static int AddNumbers(int number1, int number2)
        {//Note that we declare with the variable in our method and only use the name assigned
            int result;//this is to make it more clear what we're returning
            result = number1 + number2;
            return result;
        }
        static int GetMedian(int number)
        {
            return (number/2);//Here we're returning number back in half.
            /*we could also have done it like this 
              int finishedproduct;
              finishedproduct = number/2;
              return finishedproduct;
             */
        }
        static int GetNumbers()
        {//This method accepts no arguments, it's just set to get the numbers and pass them back
            string entry;
            int entrytranslated;
            Console.WriteLine("Please enter a number");
            entry = Console.ReadLine();
            Console.WriteLine();
            entrytranslated = int.Parse(entry);
            return entrytranslated;
        }
        static void Main(string[] args)//This is our entry point even if it's below everything else
        {
            int product;
            int numberA, numberB;
            numberA = GetNumbers();//Here you see that we're using the method to assign a variable to numberA
            numberB = GetNumbers();//Again we're using GetNumber to assign a variable
            product = AddNumbers(numberA,numberB);//Now we're using add number, feeding it it's required variables and
            //it's assigning a number to product
            Console.WriteLine("The sum of the numbers is " + product);//Here we're showing the product
            product = GetMedian(product);//now product will be assigned the median using GetMedian
            Console.WriteLine("The median of the two numbers is " + product);
            Console.ReadLine();
        }
    }
}
Here is code so you could see what I am learning :P
An instance is what you make using a class.

For example,

Code: Select all

class Thing{
}

...

Thing foobar;
foobar is an 'instance' of the class Thing
Ryan Pridgeon
C, C++, C#, Java, ActionScript 3, HaXe, PHP, VB.Net, Pascal
Music | Blog
EdEown
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 30
Joined: Sat Dec 19, 2009 5:00 pm
Favorite Gaming Platforms: PS3
Programming Language of Choice: C#
Location: Bosnia and Herzegovina

Re: Static in C#

Post by EdEown »

RyanPridgeon wrote:
EdEown wrote:Can any one plzz explain me little bit about static.

I came to tutorial which teaches you about static and it is kind of good but still he didn't explain some things and I got soooo pissed off !!!!xD

I just cant understand what "return" means and why is he keep telling something about instances !

Code: Select all

namespace Tutorial1
{
    class Program
    {
        static int AddNumbers(int number1, int number2)
        {//Note that we declare with the variable in our method and only use the name assigned
            int result;//this is to make it more clear what we're returning
            result = number1 + number2;
            return result;
        }
        static int GetMedian(int number)
        {
            return (number/2);//Here we're returning number back in half.
            /*we could also have done it like this 
              int finishedproduct;
              finishedproduct = number/2;
              return finishedproduct;
             */
        }
        static int GetNumbers()
        {//This method accepts no arguments, it's just set to get the numbers and pass them back
            string entry;
            int entrytranslated;
            Console.WriteLine("Please enter a number");
            entry = Console.ReadLine();
            Console.WriteLine();
            entrytranslated = int.Parse(entry);
            return entrytranslated;
        }
        static void Main(string[] args)//This is our entry point even if it's below everything else
        {
            int product;
            int numberA, numberB;
            numberA = GetNumbers();//Here you see that we're using the method to assign a variable to numberA
            numberB = GetNumbers();//Again we're using GetNumber to assign a variable
            product = AddNumbers(numberA,numberB);//Now we're using add number, feeding it it's required variables and
            //it's assigning a number to product
            Console.WriteLine("The sum of the numbers is " + product);//Here we're showing the product
            product = GetMedian(product);//now product will be assigned the median using GetMedian
            Console.WriteLine("The median of the two numbers is " + product);
            Console.ReadLine();
        }
    }
}
Here is code so you could see what I am learning :P
An instance is what you make using a class.

For example,

Code: Select all

class Thing{
}

...

Thing foobar;
foobar is an 'instance' of the class Thing
Ahh thanks that is what I needed to know :D :bow:
"There is nothing either good or bad, but our thinking makes it so"
EdEown
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 30
Joined: Sat Dec 19, 2009 5:00 pm
Favorite Gaming Platforms: PS3
Programming Language of Choice: C#
Location: Bosnia and Herzegovina

Re: Static in C#

Post by EdEown »

I just can not learn from those link....They explain it IN MUCH harder way...

:(
"There is nothing either good or bad, but our thinking makes it so"
EdEown
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 30
Joined: Sat Dec 19, 2009 5:00 pm
Favorite Gaming Platforms: PS3
Programming Language of Choice: C#
Location: Bosnia and Herzegovina

Re: Static in C#

Post by EdEown »

avansc wrote:dude you need to use google.
http://www.google.com/search?hl=en&q=c% ... qi=g10&oq=

I just CANT get it!!!

Are they important for now ?
"There is nothing either good or bad, but our thinking makes it so"
User avatar
hurstshifter
ES Beta Backer
ES Beta Backer
Posts: 713
Joined: Mon Jun 08, 2009 8:33 pm
Favorite Gaming Platforms: SNES
Programming Language of Choice: C/++
Location: Boston, MA
Contact:

Re: Static in C#

Post by hurstshifter »

EdEown wrote:
I just cant understand what "return" means
When a function 'returns' something it is basically outputting it to the calling function. I don't know C# so I can't help you specifically with that but in C/++ you could have a function such as

Code: Select all

int addtwonumbers(int numA, int numB)
{
   numSum = numA + numB;
   return numSum;
}
Using this function would return the value of numSum to wherever you called it from. Such as

Code: Select all

int main(void)
{
   int A = 5;
   int B = 6;
   int number = 0; 

   number = addtwonumbers(A, B);

   printf("%d", number);
   return 0;
}
So number would be equal to 11 because the addtwonumbers() function returned the sum of A and B. Hope that helps a little.
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
User avatar
Lord Pingas
Chaos Rift Regular
Chaos Rift Regular
Posts: 178
Joined: Thu Dec 31, 2009 9:33 am
Favorite Gaming Platforms: NES, SNES, Nintendo 64, Dreamcast, Wii
Programming Language of Choice: C++
Location: Hiding In My Mum's Basement With My Pokemon Cards

Re: Static in C#

Post by Lord Pingas »

W00t!
Finally finished the functions chapter of my book! As well as the pointers, references, classes and inheritance chapters in about 3 days. All I got left is polymorphism, macros and GUI programming.

I would explain to you what the return key word meant now means but it seems hurstshifter has already covered it.

Anyway sorry about the smart ass reply I gave earlier. I don't really respond like that, honest. ;)
User avatar
MrDeathNote
ES Beta Backer
ES Beta Backer
Posts: 594
Joined: Sun Oct 11, 2009 9:57 am
Current Project: cocos2d-x project
Favorite Gaming Platforms: SNES, Sega Megadrive, XBox 360
Programming Language of Choice: C/++
Location: Belfast, Ireland
Contact:

Re: Static in C#

Post by MrDeathNote »

EdEown wrote:
avansc wrote:dude you need to use google.
http://www.google.com/search?hl=en&q=c% ... qi=g10&oq=

I just CANT get it!!!

Are they important for now ?
You can program in c# without using static although it can be very useful. For example if i want to make an object or variable global i would declare it as public static. But if your only starting i wouldn't worry about it too much, you'll understand it when you need to use it.
http://www.youtube.com/user/MrDeathNote1988

Image
Image

"C makes it easy to shoot yourself in the foot. C++ makes it
harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup
X Abstract X
Chaos Rift Regular
Chaos Rift Regular
Posts: 173
Joined: Thu Feb 11, 2010 9:46 pm

Re: Static in C#

Post by X Abstract X »

Say you have a class called Apple. Then, you have a method eat().

if eat() is static, you call it like this:

Code: Select all

Apple.eat();
if eat() is non-static, you first create an Instance of Apple, then call eat() on the instance:

Code: Select all

Apple myApple = new Apple();
myApple.eat();
User avatar
MrDeathNote
ES Beta Backer
ES Beta Backer
Posts: 594
Joined: Sun Oct 11, 2009 9:57 am
Current Project: cocos2d-x project
Favorite Gaming Platforms: SNES, Sega Megadrive, XBox 360
Programming Language of Choice: C/++
Location: Belfast, Ireland
Contact:

Re: Static in C#

Post by MrDeathNote »

X Abstract X wrote:Say you have a class called Apple. Then, you have a method eat().

if eat() is static, you call it like this:

Code: Select all

Apple.eat();
if eat() is non-static, you first create an Instance of Apple, then call eat() on the instance:

Code: Select all

Apple myApple = new Apple();
myApple.eat();
Exactly, that's a good example of the use of static! But like i said it's not a huge deal your get it eventually and you'll prob be fine till then.
http://www.youtube.com/user/MrDeathNote1988

Image
Image

"C makes it easy to shoot yourself in the foot. C++ makes it
harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup
Post Reply