just a notice,
did you know...?
VB Code:
dim tst as string = "123" Mid(tst, 1, 1)="b"
is syntaxly correct :eek:
Printable View
just a notice,
did you know...?
VB Code:
dim tst as string = "123" Mid(tst, 1, 1)="b"
is syntaxly correct :eek:
yes... the old VB6 mid function gets passed a reference to the string, so it can change its contents, while its .net replacement (string.substring) returns a new string reference.
And if you try this
Messagebox will display "913" :eek2:VB Code:
Dim MyString As String = "123" Mid(MyString, 1, 1) = "9" MessageBox.Show(MyString)
but
is'nt syntaxly correctVB Code:
Dim MyString As String = "123" MyString.Substring(1, 1) = 9 MessageBox.Show(MyString)
Check this out too:
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1036/vblr7/html/vastmMid.htm
But the System.String Object is immutable. Whatever that mid function may be able to do, it should still create a new string whenever it 'changes' it.Quote:
Originally Posted by kleinma
it appears what is happening is that the Microsoft.VisualBasic namespace has 2 Mid() routines... one is a sub (statement) and one is a function..
if you look in the MSDN help, it shows a Mid statement, which accepts a ByRef string (the example shown in this thread) and also a Mid function, which takes a ByVal string, and returns a string as well.
Quote:
Mid Statement
Mid( _
ByRef Target As String, _
ByVal Start As Integer, _
Optional ByVal Length As Integer _
) = StringExpression
Quote:
Mid Function
Public Shared Function Mid( _
ByVal Str As String, _
ByVal Start As Integer, _
Optional ByVal Length As Integer _
) As String
Yes, but ByRef shouldn't matter, you cannot change a string without creating a new one.
what do you mean? its pretty much the same as this
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim S As String = "ORIGINAL TEXT" ChangeString(S) MsgBox(S) End Sub Private Sub ChangeString(ByRef text As String) text = "NEW TEXT" End Sub
C'mon it's quite simple, if you use Mid() in a statement it will not modify the source string, if you assign a value to Mid() then it will modify the source string, as it takes the string REFERENCE, which we should all know in OOP if you use value ByVal you are given the contents of an object or variable(or a clone if you like), and if you use ByRef you are given a reference to the original object, meaning it can be modified.
Mid() = String 'will re-assign that area of the string
String = Mid() 'will create a string
not really much different to anything else
As grilkip says, the String class is immutable. That Mid function is not editing the contents of the original string because that is not possible. It is creating a new string with the specified characters set to the new values and then assigning that new string to the argument, which is passed ByRef and therefore can be editied to refer to a new object. In short, the original variable is referring to a different object after the call to Mid. That said, it's a handy trick rather than having to create a StringBuilder or Char array yourself. It's probably less efficient than coding it yourself but negligibly so in the grand scheme of things
I try to stay away from the visualbasic namespace all together.. makes code easier to port to C# if needed
That's a valid consideration for those who work in both languages, but the majority of VB.NET developers won't care about C# and just want to be as productive as they can in VB.NET.Quote:
Originally Posted by kleinma
wow, creating quite a fuss over mid there... Anyways, I just found it odd that mid had this perticular way of functionning...!
I wonder if it works with Left, Right and other string manipulation functions... :eek2:
If you do a help search for Mid, the results will contain topics for the Mid Function and the Mid Statement, which is the one that you have noted here. There are Left and Right functions but no corresponding statements, i.e. procedures.