473,387 Members | 1,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Global Functions accessible to all classes in C#

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

Nov 16 '05 #1
2 6645
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.

"Poewood" <Po*****@discussions.microsoft.com> wrote in message
news:F4**********************************@microsof t.com...
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

Nov 16 '05 #2
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
"Poewood" wrote:
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

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

88
by: Tim Tyler | last post by:
PHP puts most of its functions into a big flat global namespace. That leads to short function names - but creates a namespace minefield for programmers. Lots of the functions are legacies from...
0
by: Joe Blow via DotNetMonster.com | last post by:
Hello, I have a design problem involving class instances as global variables. To give you my background, I've programmed lots in Java, and most of it has been large class structures. I'm...
2
by: Patrick | last post by:
Hi I have a problem in my C++ Funktion. I needed an 2 dimensional Array. For this i took a template script in order to create the array in the funktion. The Size of the Array I have to calculate...
7
by: Michael | last post by:
Hi newsgroup, as the subject indicates I am looking for an advice using global variables. I am not if this problem is more about style then C. If its wrong in thi group, sorry. So I have a...
8
by: Marty | last post by:
Hi, I'm new to C#, I used to code in VB.NET. Where is the best place to declare all my constants and global objects in my C# project to have them accessible globally? I have an event logger...
4
by: Simon Harris | last post by:
Hi All, I'm new to ASP.Net, so be gentle! (Plenty of 'classic' ASP experience), just one question... - Am I correct in thinking that global functions are stored in ASCX files? Thanks! ...
2
by: PRTC | last post by:
I'm trying to use the global.asax in my new web aplication proyect using the Application start to store my connection string GLOBAL.ASAX.vb Sub Application_Start(ByVal sender As Object,...
2
by: Nathan Sokalski | last post by:
I would like to access variables and functions that I declare in the Global.asax.vb file. However, I am having trouble doing that. What does the declaration have to look like in the Global.asax.vb...
22
by: UJ | last post by:
How do I create an instance of a class that is accessible to all classes/forms in my project? I already have a startup module and I was thinking putting stuff in there and then accessing it through...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.