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
  1. Home
  2. General Programming
  3. Visual Basic
  4. String Object Referrence

String Object Referrence

Scheduled Pinned Locked Moved Visual Basic
csharphelpquestiondiscussion
5 Posts 4 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.
  • N Offline
    N Offline
    Nick Seng
    wrote on last edited by
    #1

    This is in VB.Net I want to make a string object refer to another string object so that whatever changes in the object will show in both variable:

    Dim a, b as string

    a = b.Clone()
    b = "123"

    I've tried the above method but the value of a is still "" instead of "123". Any thoughts or idea?? Notorious SMC


    The difference between the almost-right word & the right word is a really large matter - it's the difference between the lightning bug and the Lightning Mark Twain
    Get your facts first, and then you can distort them as much as you please Mark Twain

    H J A 3 Replies Last reply
    0
    • N Nick Seng

      This is in VB.Net I want to make a string object refer to another string object so that whatever changes in the object will show in both variable:

      Dim a, b as string

      a = b.Clone()
      b = "123"

      I've tried the above method but the value of a is still "" instead of "123". Any thoughts or idea?? Notorious SMC


      The difference between the almost-right word & the right word is a really large matter - it's the difference between the lightning bug and the Lightning Mark Twain
      Get your facts first, and then you can distort them as much as you please Mark Twain

      H Offline
      H Offline
      Hesham Amin
      wrote on last edited by
      #2

      Notorious SMC wrote: Dim a, b as string a = b.Clone() b = "123" are you sure this is the code ? or this : Dim a, b as string b = "123" a = b.Clone()

      1 Reply Last reply
      0
      • N Nick Seng

        This is in VB.Net I want to make a string object refer to another string object so that whatever changes in the object will show in both variable:

        Dim a, b as string

        a = b.Clone()
        b = "123"

        I've tried the above method but the value of a is still "" instead of "123". Any thoughts or idea?? Notorious SMC


        The difference between the almost-right word & the right word is a really large matter - it's the difference between the lightning bug and the Lightning Mark Twain
        Get your facts first, and then you can distort them as much as you please Mark Twain

        J Offline
        J Offline
        Jim Stewart
        wrote on last edited by
        #3

        Try creating a class with a string property.

        α.γεεκ

        Fortune passes everywhere.
        Duke Leto Atreides

        1 Reply Last reply
        0
        • N Nick Seng

          This is in VB.Net I want to make a string object refer to another string object so that whatever changes in the object will show in both variable:

          Dim a, b as string

          a = b.Clone()
          b = "123"

          I've tried the above method but the value of a is still "" instead of "123". Any thoughts or idea?? Notorious SMC


          The difference between the almost-right word & the right word is a really large matter - it's the difference between the lightning bug and the Lightning Mark Twain
          Get your facts first, and then you can distort them as much as you please Mark Twain

          A Offline
          A Offline
          Anonymous
          wrote on last edited by
          #4

          I know what your thinking with Clone on a string. Your getting a pointer that another string variable can be set with so two variables can point to the same memory location. You right, that's how it works! BUT! What you don't know is that in the .NET framework, STRINGS ARE IMMUTABLE. That means that once a string is created, you CANNOT modify it at all! How is this possible? You can do Search and Replace and change the value of, in your example, 'b'! Your right, you can! What you don't see is that when you change the value of 'b', your actually creating another string in memory and changing the 'b' pointer to look at the new string. 'a' is still looking at the old one and will NOT follow 'b' around. The same is true for replacing a character in the string. You are actually creating a new string (note that 'String .Replace(char,char)' actually returns a String!) and dumping the old one! The code you wrote is correct, the functionality you want is impossible in .NET, unless you want to go thru the hassle of writing your own String Class.

          N 1 Reply Last reply
          0
          • A Anonymous

            I know what your thinking with Clone on a string. Your getting a pointer that another string variable can be set with so two variables can point to the same memory location. You right, that's how it works! BUT! What you don't know is that in the .NET framework, STRINGS ARE IMMUTABLE. That means that once a string is created, you CANNOT modify it at all! How is this possible? You can do Search and Replace and change the value of, in your example, 'b'! Your right, you can! What you don't see is that when you change the value of 'b', your actually creating another string in memory and changing the 'b' pointer to look at the new string. 'a' is still looking at the old one and will NOT follow 'b' around. The same is true for replacing a character in the string. You are actually creating a new string (note that 'String .Replace(char,char)' actually returns a String!) and dumping the old one! The code you wrote is correct, the functionality you want is impossible in .NET, unless you want to go thru the hassle of writing your own String Class.

            N Offline
            N Offline
            Nick Seng
            wrote on last edited by
            #5

            Yep, that's exactly what I wanted to do, though i figured out the string immutability thing after countless reference books later. :-O But thanks for the reply. This limitation of .net makes me long for those good old C++ days :) Notorious SMC


            The difference between the almost-right word & the right word is a really large matter - it's the difference between the lightning bug and the Lightning Mark Twain
            Get your facts first, and then you can distort them as much as you please Mark Twain

            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