I created a quick Windows App to test static data members:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplicationStaticSe
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Cont
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.F
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
TestStatic ts = new TestStatic();
MessageBox.Show(ts.longVar
}
}
public class TestStatic
{
public static long longVarStatic;
public long longVar;
public TestStatic()
{
MessageBox.Show("hi from test static");
longVar = 5;
}
}
}
==========================
The output is:
hi from test static
5
==========================
The variable that I declared static did NOT show up as accessible by the ts object!!!!! I am not sure why this is.
Main Topics
Browse All Topics





by: knowltonPosted on 2004-10-20 at 17:30:09ID: 12364994
I am new to C#. I may not be able to help...but sometimes talking through a problem helps lead to an answer.
Can you post your class code....and explain what you mean by static data and serializing public values?
My understanding is that static data are values that are tracked among several instances of the same class. This allows you to count how many instances you have created, etc. Is that correct?
Also...it is just your data members, not methods that are static?
I have a book on C# which states: "Static methods do not require an instance of an object and, therefore, cannot access the data members of the current class."