1

I actually can't believe I wasn't able to find an answer to something like this.

Public NotInheritable Class Tester
    Public Shared Sub changeText(ByVal TextBoxControl As Windows.Forms.TextBoxBase)
        Dim testString As String = TextBoxControl.Text
        testString = "Changed!"
    End Sub
End Class

I'd expect testString to be a pointer to TextBoxControl.Text. However, TextBoxControl.Text is not changed. Instead, it appears

Dim testString As String = TextBoxControl.Text

is equivalent to

Dim testString As String = TextBoxControl.Text.Clone()

but I don't want that behavior. I just want a reference to TextBoxControl's Text property. Can I do this? Why isn't the string passed by reference?

Joseph Nields
  • 5,527
  • 2
  • 32
  • 48
  • What are you looking to do with the code, it might a different approach is required? – indofraiser Mar 06 '15 at 15:24
  • 1
    Well, I'm obviously able to change the text... All you have to do is `TextBoxControl.Text = testString`. I'm just looking for better understanding of how things work. – Joseph Nields Mar 06 '15 at 15:26
  • That's why I put direct to a label (it could go out to a function instead) and showed a string. If you use a function I would put it in app code. http://stackoverflow.com/questions/10792603/how-are-strings-passed-in-net – indofraiser Mar 06 '15 at 15:30
  • Passing ByRef means the param passed can be changed inside the function/sub (and usually not a good idea). The flaw in your thinking is that `Dim str = Textbox1.Text` creates `str` as a pointer (reference) to the Text. Thankfully, it does not. – Ňɏssa Pøngjǣrdenlarp Mar 06 '15 at 15:39
  • @Plutonix then why does `ReferenceEquals(str, Textbox1.Text)` evaluate to true? – Joseph Nields Mar 06 '15 at 15:51
  • 1
    @JosephNields [ReferenceEquals working wrong with strings](http://stackoverflow.com/questions/9112610) – Bjørn-Roger Kringsjå Mar 06 '15 at 16:14
  • @Plutonix No, `ReferenceEquals(str, Textbox1.Text)` will always evaluate to true if `str` is declared `Dim str As String = Textbox1.Text`. It is assigned a pointer to `Textbox1.Text` (because `String` is a reference type) and so they point to the same object. Editing `str` simply changes where it points. – Joseph Nields Mar 06 '15 at 19:46
  • Yes, I bothched that in my haste. In the case of strings, ReferenceEquals indicates is whether that string text is interned. They can have the same text content and still [return false](https://dotnetfiddle.net/7MWllH) – Ňɏssa Pøngjǣrdenlarp Mar 06 '15 at 20:16

3 Answers3

4

You can't do what you want. Strings are immutable both in .NET and in Java. Your example won't work in Java, either.

MicSim
  • 26,265
  • 16
  • 90
  • 133
3

This would work, as you would be passing the variable in byref:

Public NotInheritable Class Tester
    Public Shared Sub changeText(ByRef str As String)
       str = "Changed!"
    End Sub
End Class

And call it like:

Tester.changeText(myTextbox.Text)
ps2goat
  • 8,067
  • 1
  • 35
  • 68
0

A shorter way would be as below but is that what you are after?

Public NotInheritable Class Tester
    Public Shared Sub changeText(ByVal TextBoxControl As Windows.Forms.TextBoxBase)
        Dim testString As String = "Changed!"        
   End Sub
End Class

If you want to say copy textbox entry to a label use the below as an example. I have copied directly from the textbox and via string:

.aspx.vb code

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Label1.Text = TextBox1.Text

        Dim vString = TextBox1.Text

        Label5.Text = vString
    End Sub
End Class

.aspx code

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
          <asp:Label ID="Label2" runat="server" Text="Add new text:"></asp:Label>
        <p> </p>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
           <p> </p>
        <asp:Button ID="Button1" runat="server" Text="Copy text to label" />
           <p> </p>
         <asp:Label ID="Label3" runat="server" Text="Result from textbox:"></asp:Label>
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>

          <p> </p>
         <asp:Label ID="Label4" runat="server" Text="Result from string:"></asp:Label>
        <asp:Label ID="Label5" runat="server" Text=""></asp:Label>
    <div>

    </div>
    </form>
</body>
</html>
indofraiser
  • 1,014
  • 3
  • 18
  • 50