|
-
Mar 1st, 2005, 08:39 AM
#1
Thread Starter
Lively Member
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:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim MyFrm As New Form
Dim MyFrm2 As Form
MyFrm.Text = "Hello World"
MyFrm2 = MyFrm
MyFrm.Text = "Hello Again"
MsgBox(MyFrm2.Text)
' *** Above Example Outputs "Hello Again" ***
Dim Temp As New Class1
Dim Temp2 As String
Temp2 = "Hello"
Temp.MyString = Temp2
Temp2 = "Hi2u"
MsgBox(Temp.MyString)
' *** Above Example Outputs "Hello" ***
End Sub
Public Class Class1
'Public Shared MyString As String
Public MyString As String
Public Sub New()
MyString = Nothing
End Sub
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]*
-
Mar 1st, 2005, 09:47 AM
#2
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.
-
Mar 1st, 2005, 09:54 AM
#3
Re: String Object References
VB Code:
Dim x As StringBuilder = New StringBuilder("Hello")
Dim y As Stringbuilder = x 'y == a reference to x
y.Append(" World") 'change should affect both x and y
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.
-
Mar 1st, 2005, 10:29 AM
#4
Thread Starter
Lively Member
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.
-
Mar 1st, 2005, 10:43 AM
#5
Re: String Object References
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|