Global string or modify string in function

J

Jose Cintron

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)
}
}
}
 
B

Bruno van Dooren [MVP VC++]

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
(e-mail address removed)
Remove only "_nos_pam"
 
J

Jose Cintron

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)
 
J

Jose Cintron

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
 
J

Jose Cintron

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
 
B

Bruno van Dooren [MVP VC++]

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
(e-mail address removed)
Remove only "_nos_pam"
 
J

Jose Cintron

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 HERE> I deserve it.
 
B

Bruno van Dooren [MVP VC++]

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 HERE> I 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
(e-mail address removed)
Remove only "_nos_pam"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top