dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Beginner's String questions

  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,573

    Beginner's String questions

    In all the C++/CLI examples I've seen, strings seem to get allocated like this:-

    Code:
    String^ MyString = "Hello World";
    To my (C++ mind) this feels more like char* than an actual String object. Doesn't C++/CLI have any actual string object type?

    Also... if I have a simple int containing some value (lets say, the value 1234) what's the easiest way of allocating it to a String (such that the string contents would then become "1234")
    "A problem well stated is a problem half solved. - Charles F. Kettering

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,921

    Re: Beginner's String questions

    This is the .net String class, boxed by ^ as it is a .net managed object. See https://docs.microsoft.com/en-us/dot...tframework-4.8

    Re int. See https://docs.microsoft.com/en-us/dot...tframework-4.8

    Note re the references. To see the examples using C++/cli, choose C++ from the top down language list top-right.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.4.0)

  3. #3
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,573

    Re: Beginner's String questions

    Quote Originally Posted by 2kaud View Post
    Thanks 2kaud - it looks like (in C++/CLI) the int type is actually an object type, rather than the simple type we know from regular C
    "A problem well stated is a problem half solved. - Charles F. Kettering

  4. #4
    Join Date
    Feb 2017
    Posts
    483

    Re: Beginner's String questions

    Quote Originally Posted by John E View Post
    it looks like (in C++/CLI) the int type is actually an object type, rather than the simple type we know from regular C
    That doesn't sound right to me because C++/CLI is an extension of standard C++ (just like C++ is an extension of C).

    It means all fundamental types of C++ like int, float, char etcetera are available in C++/CLI as well.

  5. #5
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,573

    Re: Beginner's String questions

    Well here's what I needed to do to convert int to String^

    Code:
    int n = 56;
    String^ s = n.ToString();
    "A problem well stated is a problem half solved. - Charles F. Kettering

  6. #6
    Join Date
    Feb 2017
    Posts
    483

    Re: Beginner's String questions

    Quote Originally Posted by John E View Post
    Well here's what I needed to do to convert int to String^

    Code:
    int n = 56;
    String^ s = n.ToString();

    I suppose this is an example of so called coersion polymorphism in the C++/CLI type system.

    It would mean that n is an int in the C++ sense but it is implicitly cast to an equivalent C++/CLI managed type (probably System::Int32 or System::Int64) before the ToString() method is applied.
    Last edited by wolle; November 1st, 2019 at 05:46 PM.

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,173

    Re: Beginner's String questions

    All types are derived from a base class in .net.

    int and long for example are syntactic sugar for System.Int32 and System.Int64 classes.

    In .Net string (or System.String) is a class.

    When you make an assignment (C# syntax shown), the string object is created and the string value is assigned.
    Code:
    var myString = "Hello";
    Btw, strings in .Net are immutable so the following creates a new string object that is assigned to myString:
    Code:
    myString += " World!";

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)