Results 1 to 14 of 14

Thread: Mid syntax! [comment]

  1. #1

    Thread Starter
    Fanatic Member Ruku's Avatar
    Join Date
    Jul 2002
    Location
    Canada
    Posts
    655

    Mid syntax! [comment]

    just a notice,

    did you know...?

    VB Code:
    1. dim tst as string = "123"
    2. Mid(tst, 1, 1)="b"

    is syntaxly correct

    Using VB.NET 2005/.NET 2.0, NetBeans IDE 5, Fujitsu Cobol85,
    Website: http://DreamForgery.com

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Mid syntax! [comment]

    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.

  3. #3
    Frenzied Member Zakary's Avatar
    Join Date
    Mar 2005
    Location
    Canada, Quebec, Montreal
    Posts
    1,654

    Re: Mid syntax! [comment]

    And if you try this
    VB Code:
    1. Dim MyString As String = "123"
    2. Mid(MyString, 1, 1) = "9"
    3. MessageBox.Show(MyString)
    Messagebox will display "913"

    but
    VB Code:
    1. Dim MyString As String = "123"
    2. MyString.Substring(1, 1) = 9
    3. MessageBox.Show(MyString)
    is'nt syntaxly correct
    Using VS 2010 on Fw4.0

  4. #4
    New Member
    Join Date
    Sep 2005
    Posts
    4

    Re: Mid syntax! [comment]

    Check this out too:
    ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1036/vblr7/html/vastmMid.htm

  5. #5
    Fanatic Member
    Join Date
    May 2005
    Posts
    898

    Re: Mid syntax! [comment]

    Quote Originally Posted by kleinma
    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.
    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.
    "so just keep in mind that fantasy is not the same as realtiy and make sure u remember that wii sports may be fun but u cant count on it as exercise ok cool bye" - HungarianHuman

  6. #6
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Mid syntax! [comment]

    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.
    Mid Statement
    Mid( _
    ByRef Target As String, _
    ByVal Start As Integer, _
    Optional ByVal Length As Integer _
    ) = StringExpression
    Mid Function
    Public Shared Function Mid( _
    ByVal Str As String, _
    ByVal Start As Integer, _
    Optional ByVal Length As Integer _
    ) As String

  7. #7
    Fanatic Member
    Join Date
    May 2005
    Posts
    898

    Re: Mid syntax! [comment]

    Yes, but ByRef shouldn't matter, you cannot change a string without creating a new one.
    "so just keep in mind that fantasy is not the same as realtiy and make sure u remember that wii sports may be fun but u cant count on it as exercise ok cool bye" - HungarianHuman

  8. #8
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Mid syntax! [comment]

    what do you mean? its pretty much the same as this
    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim S As String = "ORIGINAL TEXT"
    3.         ChangeString(S)
    4.         MsgBox(S)
    5.     End Sub
    6.  
    7.     Private Sub ChangeString(ByRef text As String)
    8.         text = "NEW TEXT"
    9.     End Sub

  9. #9
    Frenzied Member Phill64's Avatar
    Join Date
    Jul 2005
    Location
    Queensland, Australia
    Posts
    1,201

    Re: Mid syntax! [comment]

    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

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Mid syntax! [comment]

    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
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Mid syntax! [comment]

    I try to stay away from the visualbasic namespace all together.. makes code easier to port to C# if needed

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Mid syntax! [comment]

    Quote Originally Posted by kleinma
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13

    Thread Starter
    Fanatic Member Ruku's Avatar
    Join Date
    Jul 2002
    Location
    Canada
    Posts
    655

    Re: Mid syntax! [comment]

    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...

    Using VB.NET 2005/.NET 2.0, NetBeans IDE 5, Fujitsu Cobol85,
    Website: http://DreamForgery.com

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Mid syntax! [comment]

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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