Global Functions accessible to all classes in C#

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have created control array classes for buttons and textboxes and affixed
them to a form class. In VB I can easily create a module with fuctions that
can be accessed from any class. How can I do the same in C#? When I use the
'Static' keyword I get an error.

For example when I try to change the BackColor of an instance of a TextBox
object on the form from a ButtonArray class event handler function also on
the form I get this error: "An object reference is required" . I think it
wants me to create another instance of the TextBox array which means I will
be working with different objects.

I'm looking to create a bucnch of fuctions that can pass values and change
other object properties.

I can't figure this one out. Its probably very simple but I've just been
made stupid from using vb for so long.

Poe
 
Can you paste some code of your control array classes and how do you affix
them to your form class (or use them)?
If you add the controls on the form in the control collection of the control
array class instance, you shouldn't get an error since the class will know
what instance of control to manipulate.
 
Types themsleves cannot be statis in C# yet. However, you can make your
methods and fields static and then use them from anywhere just like a module.
For me that would achieve that same purpose like a module in VB. And in fact
this is what happens at the IL level. When you make modules in VB thay are
just converted into classes with all the methods and fields in them set to
"static". For example, if I have a method Add in my module1 as:
Public Function add(ByVal x As Integer, ByVal y As Integer) As Integer
Return x + y

End Function

it actually get converted inside your compiled assembly as :

..method public static int32 'add'(int32 x,
int32 y) cil managed
{
// Code size 9 (0x9)
.maxstack 2
.locals init ([0] int32 'add')
IL_0000: nop
IL_0001: ldarg.0
IL_0002: ldarg.1
IL_0003: add.ovf
IL_0004: stloc.0
IL_0005: br.s IL_0007
IL_0007: ldloc.0
IL_0008: ret
} // end of method Module1::'add'

In VB "shared" is equivilent to "static" in C#.
About your control array problem, the controls arrays as were in VB6 are not
supported in C# as well as in VB. Although you can create an array of type
TextBox and put all your textboxes in it. I'm not sure how have you declared
the array, if you can write your code here it will be helpful.

Hope that helps.
Abubakar.
http://joehacker.blogspot.com
 
Back
Top