Page 1 of 1

Static in C#

Posted: Tue Jan 05, 2010 12:59 pm
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

Re: Static in C#

Posted: Tue Jan 05, 2010 1:18 pm
by avansc

Re: Static in C#

Posted: Tue Jan 05, 2010 2:21 pm
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 ;)

Re: Static in C#

Posted: Tue Jan 05, 2010 2:55 pm
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

Re: Static in C#

Posted: Wed Jan 06, 2010 2:38 am
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:

Re: Static in C#

Posted: Wed Jan 06, 2010 2:40 am
by EdEown
I just can not learn from those link....They explain it IN MUCH harder way...

:(

Re: Static in C#

Posted: Wed Jan 06, 2010 4:57 am
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 ?

Re: Static in C#

Posted: Wed Jan 06, 2010 8:43 am
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.

Re: Static in C#

Posted: Wed Jan 13, 2010 8:24 pm
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. ;)

Re: Static in C#

Posted: Sat Feb 13, 2010 5:06 am
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.

Re: Static in C#

Posted: Sat Feb 13, 2010 3:16 pm
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();

Re: Static in C#

Posted: Sun Feb 14, 2010 3:38 am
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.