Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Looking for guidance with regards to System::String variables and functions.

Looking for guidance with regards to System::String variables and functions.

Scheduled Pinned Locked Moved Managed C++/CLI
c++data-structurestutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BubbaGeeNH
    wrote on last edited by
    #1

    I'm attempting to pass a String^ variable to a function and updating the variable within the function. When I return from the function I want the updated variable to be what was assigned in the function. (I'm still trying to learn the Managed C++ coding and can't find a good example.) #include "stdafx.h" using namespace System; void ChangeString(String^); int main(array ^args) { String^ Outside = gcnew String("Outside"); ChangeString(Outside); Console::WriteLine(Outside); return 0; } void ChangeString(String^ variable) { Console::WriteLine("Recieved: {0}",variable); variable = "Changed"; Console::WriteLine("New: {0}",variable); };

    George

    M D 2 Replies Last reply
    0
    • B BubbaGeeNH

      I'm attempting to pass a String^ variable to a function and updating the variable within the function. When I return from the function I want the updated variable to be what was assigned in the function. (I'm still trying to learn the Managed C++ coding and can't find a good example.) #include "stdafx.h" using namespace System; void ChangeString(String^); int main(array ^args) { String^ Outside = gcnew String("Outside"); ChangeString(Outside); Console::WriteLine(Outside); return 0; } void ChangeString(String^ variable) { Console::WriteLine("Recieved: {0}",variable); variable = "Changed"; Console::WriteLine("New: {0}",variable); };

      George

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      System.String is immutable - you cannot change the string's contents once it's initialized. You could use a System.Text.StringBuilder instead, which is mutable. You could also return a new System.String instead of trying to modify the passed String. Mark

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      1 Reply Last reply
      0
      • B BubbaGeeNH

        I'm attempting to pass a String^ variable to a function and updating the variable within the function. When I return from the function I want the updated variable to be what was assigned in the function. (I'm still trying to learn the Managed C++ coding and can't find a good example.) #include "stdafx.h" using namespace System; void ChangeString(String^); int main(array ^args) { String^ Outside = gcnew String("Outside"); ChangeString(Outside); Console::WriteLine(Outside); return 0; } void ChangeString(String^ variable) { Console::WriteLine("Recieved: {0}",variable); variable = "Changed"; Console::WriteLine("New: {0}",variable); };

        George

        D Offline
        D Offline
        Dave Doknjas
        wrote on last edited by
        #3

        void ChangeString(String %^ variable) Mark is right - System::String is immutable - but that's fine: a new one is passed back if you use '%'. I think StringBuilder is overkill for this - StringBuilder, as its name implies, is for building up large strings - you'll not gain anything by using it in cases like this.

        David Anton http://www.tangiblesoftwaresolutions.com Convert VB to C#, C++, or Java Convert C# to VB, C++, or Java Convert C++ to C#, VB, or Java Convert Java to C#, C++, or VB

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups