473,473 Members | 1,837 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

static functions

sangeetha jagannathan
23 New Member
Hello friends,
I have a doubt abt the static functions. Static function can access only static variables. Is this true? if so, then every program in c# contains public static void Main(), which access all kind of variables, objects and methods. How does this work? plz explain


Thanks in advance
Sangeetha
Mar 11 '08 #1
1 974
enggwaqas
19 New Member
Hello friends,
I have a doubt abt the static functions. Static function can access only static variables. Is this true? if so, then every program in c# contains public static void Main(), which access all kind of variables, objects and methods. How does this work? plz explain


Thanks in advance
Sangeetha
Static function can access static variables and variables that are local to that function but static function can not access "member" variables. The reason is Static method can be called without instatiating a class object. e.g.

Expand|Select|Wrap|Line Numbers
  1. class Program
  2.     {
  3.         private int _iNumber;
  4.         static int _iStaticNumber;
  5.  
  6.         static void Func()
  7.         {
  8.             _iStaticNumber = 0; //can access this bcoz it's static
  9.             _iNumber = 0; //ERROR: this variable isn't accesible in static method
  10.  
  11.             int iLocalNumber;
  12.             iLocalNumber = 3; //can access this
  13.         }
  14.     }
In the same Static Main method works. Which ever class Main method belongs to since it is static it is called without instatiating an object and it creates a local variable of your class. e.g.

Expand|Select|Wrap|Line Numbers
  1. static void Main()
  2.         {
  3.             Application.EnableVisualStyles();
  4.             Application.SetCompatibleTextRenderingDefault(false);
  5.             Application.Run(new frmMain()); //create local object of frmMain class
  6.         }
For more information: http://www.google.com.pk/search?hl=en&q=static+c%23&btnG=Search&meta=
Mar 11 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member...
6
by: Dumitru Sipos | last post by:
Hello everybody! is there possible to have a function that is both static and virtual? Dumi.
1
by: Bryan Parkoff | last post by:
I know how to write "Pointer to Function" inside struct or class without using static, but I have decided to add static to all functions inside struct or class because I want member functions to be...
15
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and...
9
by: Bryan Parkoff | last post by:
I have noticed that C programmers put static keyword beside global variable and global functions in C source codes. I believe that it is not necessary and it is not the practice in C++. Static...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
9
by: Pohihihi | last post by:
What could be the possible reasons (technical/non technical) of not using lots of static functions or variables in a program keeping in mind that Framework by itself has tons of static functions and...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
2
by: Nagrik | last post by:
Dear Group, The book of Bjarne Stroustrup in chapter 5.4.4 says the following "The word static is one of the most overused words in C and C++. For static data members it has both of the...
28
by: Why Tea | last post by:
I seem to remember that in ANSI C, all static functions should have their function prototypes listed at the beginning of the file for better consistency (or something like that). I googled and...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.