use of "static" keyword - can it be used indiscrimnantly?

  • Thread starter Thread starter Rich
  • Start date Start date
R

Rich

I was just experimenting writing some delegate functions which required me to
add the "static" keyword to procedures I was adding to a delegate array. One
of the procedures calls another procedure which takes an argument (a dataset
argument). VS said I needed to add "static" to this ancillary proc and I
also needed to add static to a form level variable I was passing to the proc.


static Dataset ds;

My question is if "static" can be over used? Is there a scenario where you
would not want to use static - where it would interefere? Or can I add
static to everythign by default?
 
I was just experimenting writing some delegate functions which required
me to
add the "static" keyword to procedures I was adding to a delegate
array. One
of the procedures calls another procedure which takes an argument (a
dataset
argument). VS said I needed to add "static" to this ancillary proc and I
also needed to add static to a form level variable I was passing to the
proc.

static Dataset ds;

My question is if "static" can be over used? Is there a scenario where
you
would not want to use static - where it would interefere? Or can I add
static to everythign by default?

If you put "static" on everything, then you no longer have any objects.
So, no...you can't use the "static" keyword "indiscriminately". You
definitely need to discriminate between various uses.

Without a code example, it's difficult to provide specific advice. But I
would guess that you tried to initialize a delegate using an instance
method without providing an instance. Using "static" may have
side-stepped that particular issue, but it has likely introduced new
problems in your code, in that wherever you use "static", you no longer
have an object instance you're using.

Pete
 
Here is a sample of the scenario I am in using static all over the place:

public partial class Form1 : Form
{
public delegate void MyDelegate();
MyDelegate[] dlg = new MyDelegate[] { proc1, proc2, proc3, proc4 };

static Dataset ds; ...

private void Form1_Load(object sender, EventArgs e)
{
listbox1.Items.Add("proc1()");
listbox1.Items.Add("proc2()");
listbox1.Items.Add("proc3()");
listbox1.Items.Add("proc4()");

dataAdapter1.Fill(ds, "tbl1")

}

private void listbox1_Click(object sender, EventArgs e)
{
dlg[listbox1.SelectedIndex]();
}

public static void proc1()
{
...
}

public static void proc2()
{
...
}

public static void proc3()
{
...
}

public static void proc4()
{
//call proc5()
proc5(ds)
...
}

public static void proc5()
{
...
}
...
}
 
Here is a sample of the scenario I am in using static all over the
place: [...]

If your methods don't need access to an instance-specific member, "static"
is fine. But in your case, you appear to have a member "Dataset ds" that
probably ought to be an instance member. If you want that member to be
static, then you should add code to your form to enforce there being only
one instance of it at a time; otherwise, it's theoretically possible for
you to have two or more instances of your Form1, all trying to use the
same "ds" member.

From the code you posted, it appears to me that what really happened is
you tried to initialize an instance member "MyDelegate[] dlg" using other
instance members. But you can't use instance members in the initializer
syntax, so the compiler complained. The correct solution, rather than
making things "static", is to perform the initialization in the
constructor instead. There, you'll have an instance reference available
for the methods (which you can use implicitly...there's no need to
actually specify "this" if you don't want to).

Pete
 
Thank you for your response. This is kind of what I was thinking - but still
a little junior level in C#. I made the following changes and was able to
successfully remove "static" from all over the place:

public partial class Form1 : Form
{
SqlConnection conn1; SqlDataAdapter da; DataSet ds;
MyDelegate[] dlg;

public delegate void MyDelegate();

public Form1()
{
InitializeComponent();
dlg = new MyDelegate[] { proc1, proc2, proc3, proc4 };
}
...




Peter Duniho said:
Here is a sample of the scenario I am in using static all over the
place: [...]

If your methods don't need access to an instance-specific member, "static"
is fine. But in your case, you appear to have a member "Dataset ds" that
probably ought to be an instance member. If you want that member to be
static, then you should add code to your form to enforce there being only
one instance of it at a time; otherwise, it's theoretically possible for
you to have two or more instances of your Form1, all trying to use the
same "ds" member.

From the code you posted, it appears to me that what really happened is
you tried to initialize an instance member "MyDelegate[] dlg" using other
instance members. But you can't use instance members in the initializer
syntax, so the compiler complained. The correct solution, rather than
making things "static", is to perform the initialization in the
constructor instead. There, you'll have an instance reference available
for the methods (which you can use implicitly...there's no need to
actually specify "this" if you don't want to).

Pete
 
Thank you for your response. This is kind of what I was thinking - but
still
a little junior level in C#. I made the following changes and was able
to
successfully remove "static" from all over the place:

public partial class Form1 : Form
{
...
public Form1()
{
InitializeComponent();
dlg = new MyDelegate[] { proc1, proc2, proc3, proc4 };
}
...

Looks good.
 
Back
Top