Click here to Skip to main content
15,892,059 members
Home / Discussions / C#
   

C#

 
AnswerRe: Annotations on screen Pin
OriginalGriff16-Aug-09 0:45
mveOriginalGriff16-Aug-09 0:45 
QuestionMaskedTextBox help Pin
nhqlbaislwfiikqraqnm15-Aug-09 23:18
nhqlbaislwfiikqraqnm15-Aug-09 23:18 
AnswerRe: MaskedTextBox help Pin
Hristo-Bojilov15-Aug-09 23:31
Hristo-Bojilov15-Aug-09 23:31 
GeneralRe: MaskedTextBox help Pin
nhqlbaislwfiikqraqnm15-Aug-09 23:57
nhqlbaislwfiikqraqnm15-Aug-09 23:57 
GeneralRe: MaskedTextBox help Pin
Hristo-Bojilov16-Aug-09 2:00
Hristo-Bojilov16-Aug-09 2:00 
QuestionAnnotations on screen Pin
Vijay Mudunuri15-Aug-09 22:54
Vijay Mudunuri15-Aug-09 22:54 
Questionstatic data members and methods Pin
Black Gen15-Aug-09 21:28
Black Gen15-Aug-09 21:28 
AnswerRe: static data members and methods Pin
OriginalGriff15-Aug-09 22:03
mveOriginalGriff15-Aug-09 22:03 
{
private static int count;
public StaticDataMembersAndMethods()
   {
   InitializeComponent();
   count++;
   }

public void showcount()
   {
   MessageBox.Show("Value of count is :" + count);
   }

private void button1_Click(object sender, EventArgs e)
   {
   StaticDataMembersAndMethods objstatic = new StaticDataMembersAndMethods();
   objstatic.showcount();
   }


There are a couple of things here, that you need to understand.

Firstly, it is bad practise to declare a variable, and use it before it is initiialised. You do this with "count" - you declare it "private static int count;" and then increment it in teh class constructor "count++". You get away with this because it is an int, and the framework initialized it for you, to zero. If you try this with a string, you will get an exception when you try to append to it. Declare you int as "private static int count = 0;"

Secondly, the count does allready start at zero, not two. What is happening is that you do not understand what a static variable is. When you declare a variable (or a method) as static, you are saying that there is only one instance of this variable - it is shared in common between all instances of the class.

So, you have a class called "StaticDataMembersAndMethods" which is derived from a Form (since the constructor contains a "InitializeComponent" call), which contains a static variable "count", and a button "Button1". When the form is constucted, count is created and initialized by the framework to zero. The class constructor is called, and increments the count to one. The button press then creates a second instance of the class, which shares the existing instance of the static count. The constructor for the class is called, which increments the count to two. The MessageBox then shows that.

Oh, please use <pre></pre> tags to surround you code fragments; it displays it as shown above which is a lot easier to read - particularly when the fragment gets bigger.

No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.

This message is made of fully recyclable Zeros and Ones

AnswerRe: static data members and methods Pin
Saksida Bojan15-Aug-09 22:09
Saksida Bojan15-Aug-09 22:09 
Questionadding PNG icons with tranparency to listview Pin
Muhammad Ahmed15-Aug-09 16:22
Muhammad Ahmed15-Aug-09 16:22 
AnswerRe: adding PNG icons with tranparency to listview Pin
Henry Minute16-Aug-09 3:19
Henry Minute16-Aug-09 3:19 
QuestionFileStream runtime issues (Updated) [modified] Pin
Mikey_H15-Aug-09 14:21
Mikey_H15-Aug-09 14:21 
AnswerRe: FileStream runtime issues Pin
Luc Pattyn15-Aug-09 14:38
sitebuilderLuc Pattyn15-Aug-09 14:38 
GeneralRe: FileStream runtime issues [modified] Pin
Mikey_H15-Aug-09 14:59
Mikey_H15-Aug-09 14:59 
AnswerRe: FileStream runtime issues Pin
riced15-Aug-09 23:09
riced15-Aug-09 23:09 
GeneralRe: FileStream runtime issues Pin
Mikey_H15-Aug-09 23:22
Mikey_H15-Aug-09 23:22 
AnswerRe: FileStream runtime issues (Updated) Pin
riced16-Aug-09 0:18
riced16-Aug-09 0:18 
GeneralRe: FileStream runtime issues (Updated) Pin
Mikey_H16-Aug-09 1:44
Mikey_H16-Aug-09 1:44 
GeneralRe: FileStream runtime issues (Updated) Pin
riced16-Aug-09 2:32
riced16-Aug-09 2:32 
GeneralRe: FileStream runtime issues (Updated) Pin
Mikey_H16-Aug-09 12:48
Mikey_H16-Aug-09 12:48 
QuestionReading letters from an Image in C# Pin
Sumit_Ghosh15-Aug-09 12:41
Sumit_Ghosh15-Aug-09 12:41 
AnswerRe: Reading letters from an Image in C# Pin
Henry Minute15-Aug-09 13:24
Henry Minute15-Aug-09 13:24 
QuestionWeb Browser(C# + other .NET technologies) Pin
abhifanatic15-Aug-09 10:12
abhifanatic15-Aug-09 10:12 
AnswerRe: Web Browser(C# + other .NET technologies) Pin
Saksida Bojan15-Aug-09 10:29
Saksida Bojan15-Aug-09 10:29 
QuestionC# IP Scanner Pin
nhqlbaislwfiikqraqnm15-Aug-09 10:06
nhqlbaislwfiikqraqnm15-Aug-09 10:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.