Results 1 to 5 of 5

Thread: String Object References [Resolved]

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 1999
    Posts
    77

    Resolved String Object References [Resolved]

    I'm trying to create an object reference for the String Class.

    Normally to create an object reference you declare the variable as the object type, without the New keyword, as shown by my first example, using the Form Class.

    Next I use the String class, but there's a problem. Strings automatically assume the New keyword, so how do I create an object reference for a String?

    Note: Class Class1 is shown below the Button1_Click( ... ) method.

    I tried setting the String Object to Nothing, with no change in outcome.

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.     Dim MyFrm As New Form
    3.     Dim MyFrm2 As Form
    4.    
    5.     MyFrm.Text = "Hello World"
    6.     MyFrm2 = MyFrm
    7.    
    8.     MyFrm.Text = "Hello Again"
    9.    
    10.     MsgBox(MyFrm2.Text)
    11.    
    12.     ' *** Above Example Outputs "Hello Again" ***
    13.    
    14.     Dim Temp As New Class1
    15.     Dim Temp2 As String
    16.    
    17.     Temp2 = "Hello"
    18.    
    19.     Temp.MyString = Temp2
    20.    
    21.     Temp2 = "Hi2u"
    22.    
    23.     MsgBox(Temp.MyString)
    24.    
    25.     ' *** Above Example Outputs "Hello" ***
    26. End Sub
    27.  
    28. Public Class Class1
    29.     'Public Shared MyString As String
    30.     Public MyString As String
    31.    
    32.     Public Sub New()
    33.         MyString = Nothing
    34.     End Sub
    35. End Class

    I tried searching for an answer, but none of them didn't seem to work or I didn't completely understand what was suggested at: http://www.codeguru.com/forum/showthread.php?t=257933

    The reason I need a String object reference is since I'm going to be creating a thread. The thread is wrapped inside a class, that will contain data used for the method that is threaded, since no actual arguments can be passed to a thread. Since I want this thread to be able to access a string outside of the thread, which may be modified by another thread/method, I want a String Object Reference.

    I hope I'm using the correct terminology and thanks for your help in advanced. I'm still not that great of a programmer, so if I'm making a huge mistake in the way I'm designing this application, please don't be shy to tell me so.
    Last edited by jgomes; Mar 1st, 2005 at 10:31 AM. Reason: *Changes Code tags to VBCode*, *2nd Edit: Adds [Resolved]*

  2. #2
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: String Object References

    You can easily set two string variables to refer to the same string object in memory but as soon as you change one of the variables to something else, that variable has a new string assigned to it.

    The only way you can take a reference AND edit the string it refers to is by using a method that takes a byref argument.

    Otherwise this will be a problem, it may be impossible. There is probably an API for this.

    However! MSDN has this to say...

    A String is called immutable because its value cannot be modified once it has been created. Methods that appear to modify a String actually return a new String containing the modification. If it is necessary to modify the actual contents of a string-like object, use the System.Text.StringBuilder class.
    Is the stringbuilder compatible with your program?
    I don't live here any more.

  3. #3
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: String Object References

    VB Code:
    1. Dim x As StringBuilder = New StringBuilder("Hello")
    2.     Dim y As Stringbuilder = x 'y == a reference to x
    3.  
    4.     y.Append(" World") 'change should affect both x and y
    5.  
    6.     Console.WriteLine(x.ToString & ", " & y.ToString & ", x points to the same object as y: " & CBool(x Is y))

    This tells you that both x and y contain the string "Hello World" because they are both referring to the same stringbuilder instance.
    I don't live here any more.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jul 1999
    Posts
    77

    Re: String Object References

    I've never used/seen Stringbuilder before, but I'm sure I'll be able to use it.

    It's interesting how it completely creates a new string, rather than changing the old object.

    Thanks a lot for your help, I'm sure I would have been stuck on that one for awhile without any aid.

    ***

    On a side note; I tried everything to raise your rating up, but it always complains in Internet Explorer that I'm not logged in. I went through the process of clearing my cookies, making sure all cookies were allowed. Then tried logging back in a few times, with remember me marked, closing my browser and it still would never remember my user when trying to rate.

    Mozilla has a similar issue (it wouldn't remember me being logged in), but when I attempted to rate your post up, it complained "No Post specified. If you followed a valid link, please notify the webmaster".

    I'll be sure to try again on my other computer.

  5. #5
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: String Object References

    Quote Originally Posted by jgomes
    I'll be sure to try again on my other computer.
    You're too kind thanks.
    I don't live here any more.

Posting Permissions

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



Click Here to Expand Forum to Full Width