我实在无法相信我无法找到这样的答案。
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
我希望testString
成为TextBoxControl.Text
的指针。但是,TextBoxControl.Text
未更改。相反,它出现
Dim testString As String = TextBoxControl.Text
相当于
Dim testString As String = TextBoxControl.Text.Clone()
但我不想要这种行为。我只想要引用TextBoxControl
的{{1}}属性。我可以这样做吗?为什么不通过引用传递字符串?
答案 0 :(得分:4)
你无法做你想做的事。字符串在.NET和Java中都是不可变的。您的示例也不适用于Java。
答案 1 :(得分:2)
这会有效,因为你将传递变量inref:
Public NotInheritable Class Tester
Public Shared Sub changeText(ByRef str As String)
str = "Changed!"
End Sub
End Class
并称之为:
Tester.changeText(myTextbox.Text)
答案 2 :(得分:0)
较短的方式如下,但你所追求的是什么?
Public NotInheritable Class Tester
Public Shared Sub changeText(ByVal TextBoxControl As Windows.Forms.TextBoxBase)
Dim testString As String = "Changed!"
End Sub
End Class
如果要将复制文本框条目复制到标签,请使用以下示例。我直接从文本框中复制并通过字符串:
.aspx.vb代码
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代码
<%@ 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>