473,320 Members | 1,884 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,320 software developers and data experts.

Global string or modify string in function

Hello all. Newbie question here...

I have a program that needs to do 1 of 2 things
1. declare a global System::String (which I think can't be done, because
VS 2005 complains)
2. modify the System::String in one function

An example may be easier to understand... The question is how would I get
the second option to work???

--- First option
// All of my defines go here
#define fnOfFile "test.xml"
#define ErrorFNF 1
#define OK 0

// Global Vars
System::String GlobalString;

namespace ReportGenerator {

public ref class Form1 : public System::Windows::Forms::Form
{

private: int CheckFiles()
{
// If file is in current directory
GlobalString = String::Concat(".\\" + fnOfFile);
// else If file is in parent directory
GlobalString = String::Concat("..\\" + fnOfFile);
// else
return ErrorFNF;

return OK;
}

private: System::Void btnGenerate_Click(System::Object^ sender,
System::EventArgs^ e)
{
// some code here
status = CheckFiles();
}
}
}
--- Second option
// All of my defines go here
#define fnOfFile "test.xml"
#define ErrorFNF 1
#define OK 0

namespace ReportGenerator {

public ref class Form1 : public System::Windows::Forms::Form
{

private: int CheckFiles(System::String^ aString)
{
// If file is in current directory
aString = String::Concat(".\\" + fnOfFile);
// else If file is in parent directory
aString = String::Concat("..\\" + fnOfFile);
// else
return ErrorFNF;

return OK;
}

private: System::Void btnGenerate_Click(System::Object^ sender,
System::EventArgs^ e)
{
System::String FileName = "";
// some code here
status = CheckFiles(FileName);
// Now FileName has the correct location (if the file was found)
}
}
}
Sep 5 '06 #1
8 1688
Hello all. Newbie question here...
>
I have a program that needs to do 1 of 2 things
1. declare a global System::String (which I think can't be done,
because VS 2005 complains)
2. modify the System::String in one function
Something like this would work, if you need a global:
ref class myGlobal
{
public:
static String ^globalString;
static myGlobal()
{
globalString = nullptr;
}

};

you cannot create globals, but it is perfectly valid to create a class,
solely for the purpose of containing public static (i.e. global) variables.
myGlobal::globalString = "Hello";
The static constructor ensures that the global has a know value at startup.

Alternatively, if you want to change a string that you pass to a function,
you have to pass it by reference (%).
If you didn't use %, the caller would still point to the same string.
You do that like this:

reg class Test
{
// ..
static void ChangeString(String ^ %str)
{
str = "Test";
}
};

--

Kind regards,
Bruno van Dooren
br**********************@hotmail.com
Remove only "_nos_pam"
Sep 5 '06 #2
Thanks Bruno

I also though about crating a class to hold all of the global crap. I guess
I can do that fro version 2.0 for the moment I got it to work by doing
something like...

private: int CheckFiles(System::String^ &aString)
{
// If file is in current directory
aString = ".\\" + fnOfFile;
// else If file is in parent directory
aString = "..\\" + fnOfFile;
// else
return ErrorFNF;

return OK;
}

private: System::Void btnGenerate_Click(System::Object^ sender,
System::EventArgs^ e)
{
System::String FileName = "";
// some code here
status = CheckFiles(FileName);
// Now FileName has the correct location (if the file was found)

"Bruno van Dooren [MVP VC++]" <br**********************@hotmail.comwrote
in message news:ed*************@TK2MSFTNGP06.phx.gbl...
>Hello all. Newbie question here...

I have a program that needs to do 1 of 2 things
1. declare a global System::String (which I think can't be done,
because VS 2005 complains)
2. modify the System::String in one function

Something like this would work, if you need a global:
ref class myGlobal
{
public:
static String ^globalString;
static myGlobal()
{
globalString = nullptr;
}

};

you cannot create globals, but it is perfectly valid to create a class,
solely for the purpose of containing public static (i.e. global)
variables. myGlobal::globalString = "Hello";
The static constructor ensures that the global has a know value at
startup.

Alternatively, if you want to change a string that you pass to a function,
you have to pass it by reference (%).
If you didn't use %, the caller would still point to the same string.
You do that like this:

reg class Test
{
// ..
static void ChangeString(String ^ %str)
{
str = "Test";
}
};

--

Kind regards,
Bruno van Dooren
br**********************@hotmail.com
Remove only "_nos_pam"

Sep 5 '06 #3
I'm trying to get the class idea to work since it seems that I'm going to
need to save the default path from where the program was started, and some
other seetings, but I'm running into some problems

here's what I have

#pragma once

// bunch of defines

ref class GlobalSettings
{
public:
static String^ appPath;

static GlobalSettings()
{
appPath = System::IO::Directory::GetCurrentDirectory();
}
}

namespace ReportGenerator {

private: System::Void btnGenerateReport_Click(System::Object^ sender,
System::EventArgs^ e)
{
// initialize some stuff...

MessageBox::Show(GlobalSettings::appPath, "", MessageBoxButtons::OK,
MessageBoxIcon::Error);
}
}

When I compile I get
error C2143: syntax error : missing ';' before '^'
error C4430: missing type specifier - int assumed. Note: C++ does not
support default-int
"Bruno van Dooren [MVP VC++]" <br**********************@hotmail.comwrote
in message news:ed*************@TK2MSFTNGP06.phx.gbl...
>Hello all. Newbie question here...

I have a program that needs to do 1 of 2 things
1. declare a global System::String (which I think can't be done,
because VS 2005 complains)
2. modify the System::String in one function

Something like this would work, if you need a global:
ref class myGlobal
{
public:
static String ^globalString;
static myGlobal()
{
globalString = nullptr;
}

};

you cannot create globals, but it is perfectly valid to create a class,
solely for the purpose of containing public static (i.e. global)
variables. myGlobal::globalString = "Hello";
The static constructor ensures that the global has a know value at
startup.

Alternatively, if you want to change a string that you pass to a function,
you have to pass it by reference (%).
If you didn't use %, the caller would still point to the same string.
You do that like this:

reg class Test
{
// ..
static void ChangeString(String ^ %str)
{
str = "Test";
}
};

--

Kind regards,
Bruno van Dooren
br**********************@hotmail.com
Remove only "_nos_pam"

Sep 5 '06 #4
Got passed that it was missing a ; at the end of the class definition. Now
my problem is that if I stick the definition before Form1's definition all
hell breaks loose and when I try to compile I get a message to the effect
that Form1 is not the first class. If I move the definition to after Form1
I get messages saying that it cannot find the definition for GlobalSettings

"Jose Cintron" <l0***********@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
I'm trying to get the class idea to work since it seems that I'm going to
need to save the default path from where the program was started, and some
other seetings, but I'm running into some problems

here's what I have

#pragma once

// bunch of defines

ref class GlobalSettings
{
public:
static String^ appPath;

static GlobalSettings()
{
appPath = System::IO::Directory::GetCurrentDirectory();
}
}

namespace ReportGenerator {

private: System::Void btnGenerateReport_Click(System::Object^ sender,
System::EventArgs^ e)
{
// initialize some stuff...

MessageBox::Show(GlobalSettings::appPath, "", MessageBoxButtons::OK,
MessageBoxIcon::Error);
}
}

When I compile I get
error C2143: syntax error : missing ';' before '^'
error C4430: missing type specifier - int assumed. Note: C++ does not
support default-int
"Bruno van Dooren [MVP VC++]" <br**********************@hotmail.comwrote
in message news:ed*************@TK2MSFTNGP06.phx.gbl...
>>Hello all. Newbie question here...

I have a program that needs to do 1 of 2 things
1. declare a global System::String (which I think can't be done,
because VS 2005 complains)
2. modify the System::String in one function

Something like this would work, if you need a global:
ref class myGlobal
{
public:
static String ^globalString;
static myGlobal()
{
globalString = nullptr;
}

};

you cannot create globals, but it is perfectly valid to create a class,
solely for the purpose of containing public static (i.e. global)
variables. myGlobal::globalString = "Hello";
The static constructor ensures that the global has a know value at
startup.

Alternatively, if you want to change a string that you pass to a
function, you have to pass it by reference (%).
If you didn't use %, the caller would still point to the same string.
You do that like this:

reg class Test
{
// ..
static void ChangeString(String ^ %str)
{
str = "Test";
}
};

--

Kind regards,
Bruno van Dooren
br**********************@hotmail.com
Remove only "_nos_pam"


Sep 5 '06 #5
Got passed that it was missing a ; at the end of the class definition.
Now my problem is that if I stick the definition before Form1's definition
all hell breaks loose and when I try to compile I get a message to the
effect that Form1 is not the first class. If I move the definition to
after Form1 I get messages saying that it cannot find the definition for
GlobalSettings
Could you post the exact compiler error message, possibly with some of the
code that causes the error?
Otherwise I'll be forced to make jokes about my crystal ball, in a monty
python'esque way.
;-)

--

Kind regards,
Bruno van Dooren
br**********************@hotmail.com
Remove only "_nos_pam"
Sep 6 '06 #6
I fugure the problem out... I just moved the definition of my global class
to inside the definition for the Form1 class and everything worked fine...
Go for it just <INSERT JOKE HEREI deserve it.

"Bruno van Dooren [MVP VC++]" <br**********************@hotmail.comwrote
in message news:eG**************@TK2MSFTNGP04.phx.gbl...
>Got passed that it was missing a ; at the end of the class definition.
Now my problem is that if I stick the definition before Form1's
definition all hell breaks loose and when I try to compile I get a
message to the effect that Form1 is not the first class. If I move the
definition to after Form1 I get messages saying that it cannot find the
definition for GlobalSettings

Could you post the exact compiler error message, possibly with some of the
code that causes the error?
Otherwise I'll be forced to make jokes about my crystal ball, in a monty
python'esque way.
;-)

--

Kind regards,
Bruno van Dooren
br**********************@hotmail.com
Remove only "_nos_pam"

Sep 6 '06 #7
>I fugure the problem out... I just moved the definition of my global class
>to inside the definition for the Form1 class and everything worked fine...
Go for it just <INSERT JOKE HEREI deserve it.
Hey,
We all have to start sometime.
When I was a C++ newbie, I also made a lot of basic mistakes. There's no
shame in making mistakes.
Besides, I already made my crystal ball joke.
;-)

--

Kind regards,
Bruno van Dooren
br**********************@hotmail.com
Remove only "_nos_pam"
Sep 6 '06 #8
I know you already solved it, but here's another way:
>
// Global Vars
System::String GlobalString;
gcroot<System::String^GlobalString;
Sep 6 '06 #9

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

Similar topics

8
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>...
10
by: Matt | last post by:
Greetings, What are people's thoughts on global variables in C++? Why are we taught not to use them in programming? Is it true that if you are running two copies of the C program one copy can...
3
by: Phil Lamey | last post by:
Hi All, I have the following code but for some reason I cannot get the Session_OnEnd event to fire. I am trying to limit the amount of connections a browser session can have. Where the...
59
by: seberino | last post by:
I've heard 2 people complain that word 'global' is confusing. Perhaps 'modulescope' or 'module' would be better? Am I the first peope to have thought of this and suggested it? Is this a...
3
by: Dalan | last post by:
I need some assistance or advise in composing code for a global function module or a related one for populating values in text boxes on reports and forms with a name, actually several different...
11
by: Capstar | last post by:
Hi, I am working on an application, which will run embedded without an OS. The app is build up out of a couple of well defined parts. At first I wanted to keep those parts seperated and use...
10
by: David P. Donahue | last post by:
When I wrote websites in VB .NET, I would often put functions in Global for all the pages to call. Now, in C#, doing so results in "references to non-static objects" and whatnot. I realize what...
0
by: Rick Hein | last post by:
I've got a problem with an app I've been working on, the Caching object and events not firing correctly. In a nutshell: When I'm debugging, and I set a breakpoint in the removed item call back, the...
6
by: Phoe6 | last post by:
I write a script: #!/usr/bin/python astring = "This is a String" def fun1(): astring = "I modify it in fun1" def fun2(): astring = "I modify it in fun2" def main(): print astring
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.