473,508 Members | 2,195 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is it possible to use global variable/function in VC.NET?

I found it's not possible.

But if I have a function which is called by every forms,
do I have to copy the function to every form class?
It looks like non-sense.

Is there any other options?
Dec 14 '05 #1
6 1493
Ok, I got it.
Inheritance is a good choice here.
Thanks.

Eddie wrote:
I found it's not possible.

But if I have a function which is called by every forms,
do I have to copy the function to every form class?
It looks like non-sense.

Is there any other options?

Dec 14 '05 #2
>>
Is there any other options?

Also consider a static member function.

Brian
Dec 14 '05 #3

Eddie wrote:
Ok, I got it.
Inheritance is a good choice here.

No, I don' think so. Better put your utility function as a static
member function in an "Utils" (name it the way you want) class.

Arnaud
MVP - VC

Dec 14 '05 #4
Actually, I added static member function for BaseForm which is inherited
from System::Windows::Forms::Form.
And all other forms are inherited from the BaseForm.
Then I can type following code when I need the function.

utility_function(...);

But if I create a specific utility class like

public ref class Utils {
....
public: int utility_function();
....
};

Whenever I try to use the funtion, I have to write the following code.

Utils utils; (or Utils^ utils = gcnew Utils();)
utils->utility_function();

I think this is not cute code. ;-P

Sorry.
I'm a quite newbie in VC.NET so I think I can be missing some other way.
Please give me one more comment on this.
I have a lot of forms for my project, so I think it's also not cute that
I have to
change all base forms.
ad******@club-internet.fr wrote:
Eddie wrote:
Ok, I got it.
Inheritance is a good choice here.


No, I don' think so. Better put your utility function as a static
member function in an "Utils" (name it the way you want) class.

Arnaud
MVP - VC

Dec 14 '05 #5
Eddie wrote:
public ref class Utils {
...
public: int utility_function();
...
};
As Arnaud recommended, make your methods static:

public:
static int utility_function();
Whenever I try to use the funtion, I have to write the following code.

Utils utils; (or Utils^ utils = gcnew Utils();)
utils->utility_function();

I think this is not cute code. ;-P


Once you make the method static, you don't have to create an instance,
you can simply type

Utils::utility_function();

In this case Utils looks like a namespace and utility_function appears
to be a non-member function in that namespace. That's how you handle it
in .NET, because you don't have a choice to write non-member functions.

Your next question may be how to handle non-member (global) constants in
..NET. In native C++ you just write

const int MyConst = 10;

In .NET you make it a static property. Here's an example:

public ref class Utils
{
public: static property int MyConst
{
int get() { return 10; }
}
};

And use it like this: Utils::MyConst.

Tom
Dec 15 '05 #6
Thanks Mr. Demjen.
This is very useful info for me.

Tamas Demjen wrote:
Eddie wrote:
public ref class Utils {
...
public: int utility_function();
...
};

As Arnaud recommended, make your methods static:

public:
static int utility_function();
Whenever I try to use the funtion, I have to write the following code.

Utils utils; (or Utils^ utils = gcnew Utils();)
utils->utility_function();

I think this is not cute code. ;-P

Once you make the method static, you don't have to create an instance,
you can simply type

Utils::utility_function();

In this case Utils looks like a namespace and utility_function appears
to be a non-member function in that namespace. That's how you handle it
in .NET, because you don't have a choice to write non-member functions.

Your next question may be how to handle non-member (global) constants in
.NET. In native C++ you just write

const int MyConst = 10;

In .NET you make it a static property. Here's an example:

public ref class Utils
{
public: static property int MyConst
{
int get() { return 10; }
}
};

And use it like this: Utils::MyConst.

Tom

Dec 22 '05 #7

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

Similar topics

8
102743
by: David Hitillambeau | last post by:
Hi guys, As I am new to Python, i was wondering how to declare and use global variables. Suppose i have the following structure in the same module (same file): def foo: <instructions>...
4
24159
by: Andrew V. Romero | last post by:
I have been working on a function which makes it easier for me to pull variables from the URL. So far I have: <script language="JavaScript"> var variablesInUrl; var vArray = new Array(); ...
2
8803
by: Thomas Matthews | last post by:
Hi, I'm getting linking errors when I declare a variable in the global scope, but not inside a function. The declarations are the same (only the names have been changed...). class Book {...
8
2509
by: lawrence | last post by:
I'm learning Javascript. I downloaded a script for study. Please tell me how the variable "loop" can have scope in the first function when it is altered in the second function? It is not defined...
13
2516
by: Alison Givens | last post by:
....... that nobody knows the answer. I can't imagine that I am the only one that uses parameters in CR. So, my question again: I have the following problem. (VB.NET 2003 with CR) I have a...
16
3196
by: Roman Ziak | last post by:
Hello, there were times when I used to be looking for a way to access JavaScript Global object similar to those found in VBScript or PHP ($GLOBALS). At present this has only academic value for...
10
2631
by: Charles O'Flynn | last post by:
As a complete newcomer (2-3 days) to PHP, although not to programming in general, I have 'dived in' to start a small project to read and parse an XML data stream. I have already worked out most of...
3
2754
by: User1014 | last post by:
A global variable is really just a property of the "Global Object", so what does that make a function defined in the global context? A method of the Global Object? ...
112
5350
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
20
12692
by: teddysnips | last post by:
Weird. I have taken over responsibility for a legacy application, Access 2k3, split FE/BE. The client has reported a problem and I'm investigating. I didn't write the application. The...
0
7227
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
7331
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,...
1
7054
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7501
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5633
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5056
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4713
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1564
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
424
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.