Global string or modify string in function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jose Cintron

    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_Cli ck(System::Obje ct^ sender,
    System::EventAr gs^ 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(Syst em::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_Cli ck(System::Obje ct^ sender,
    System::EventAr gs^ e)
    {
    System::String FileName = "";
    // some code here
    status = CheckFiles(File Name);
    // Now FileName has the correct location (if the file was found)
    }
    }
    }


  • Bruno van Dooren [MVP VC++]

    #2
    Re: 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
    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::globa lString = "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(St ring ^ %str)
    {
    str = "Test";
    }
    };

    --

    Kind regards,
    Bruno van Dooren
    bruno_nos_pam_v an_dooren@hotma il.com
    Remove only "_nos_pam"


    Comment

    • Jose Cintron

      #3
      Re: Global string or modify string in function

      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(Syst em::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_Cli ck(System::Obje ct^ sender,
      System::EventAr gs^ e)
      {
      System::String FileName = "";
      // some code here
      status = CheckFiles(File Name);
      // Now FileName has the correct location (if the file was found)

      "Bruno van Dooren [MVP VC++]" <bruno_nos_pam_ van_dooren@hotm ail.comwrote
      in message news:edV9cAS0GH A.328@TK2MSFTNG P06.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::globa lString = "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(St ring ^ %str)
      {
      str = "Test";
      }
      };
      >
      --
      >
      Kind regards,
      Bruno van Dooren
      bruno_nos_pam_v an_dooren@hotma il.com
      Remove only "_nos_pam"
      >

      Comment

      • Jose Cintron

        #4
        Re: Global string or modify string in function

        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::Dir ectory::GetCurr entDirectory();
        }
        }

        namespace ReportGenerator {

        private: System::Void btnGenerateRepo rt_Click(System ::Object^ sender,
        System::EventAr gs^ e)
        {
        // initialize some stuff...

        MessageBox::Sho w(GlobalSetting s::appPath, "", MessageBoxButto ns::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++]" <bruno_nos_pam_ van_dooren@hotm ail.comwrote
        in message news:edV9cAS0GH A.328@TK2MSFTNG P06.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::globa lString = "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(St ring ^ %str)
        {
        str = "Test";
        }
        };
        >
        --
        >
        Kind regards,
        Bruno van Dooren
        bruno_nos_pam_v an_dooren@hotma il.com
        Remove only "_nos_pam"
        >

        Comment

        • Jose Cintron

          #5
          Re: Global string or modify string in function

          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" <l0rddarkf0rce@ hotmail.comwrot e in message
          news:%23q1hSsS0 GHA.4580@TK2MSF TNGP05.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::Dir ectory::GetCurr entDirectory();
          }
          }
          >
          namespace ReportGenerator {
          >
          private: System::Void btnGenerateRepo rt_Click(System ::Object^ sender,
          System::EventAr gs^ e)
          {
          // initialize some stuff...
          >
          MessageBox::Sho w(GlobalSetting s::appPath, "", MessageBoxButto ns::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++]" <bruno_nos_pam_ van_dooren@hotm ail.comwrote
          in message news:edV9cAS0GH A.328@TK2MSFTNG P06.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::globa lString = "Hello";
          >The static constructor ensures that the global has a know value at
          >startup.
          >>
          >Alternativel y, 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(St ring ^ %str)
          > {
          > str = "Test";
          > }
          >};
          >>
          >--
          >>
          >Kind regards,
          > Bruno van Dooren
          > bruno_nos_pam_v an_dooren@hotma il.com
          > Remove only "_nos_pam"
          >>
          >
          >

          Comment

          • Bruno van Dooren [MVP VC++]

            #6
            Re: Global string or modify string in function

            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
            bruno_nos_pam_v an_dooren@hotma il.com
            Remove only "_nos_pam"


            Comment

            • Jose Cintron

              #7
              Re: Global string or modify string in function

              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++]" <bruno_nos_pam_ van_dooren@hotm ail.comwrote
              in message news:eG2WwKc0GH A.4748@TK2MSFTN GP04.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
              bruno_nos_pam_v an_dooren@hotma il.com
              Remove only "_nos_pam"
              >

              Comment

              • Bruno van Dooren [MVP VC++]

                #8
                Re: Global string or modify string in function

                >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
                bruno_nos_pam_v an_dooren@hotma il.com
                Remove only "_nos_pam"


                Comment

                • Ben Voigt

                  #9
                  Re: Global string or modify string in function

                  I know you already solved it, but here's another way:
                  >
                  // Global Vars
                  System::String GlobalString;
                  gcroot<System:: String^GlobalSt ring;


                  Comment

                  Working...