Results 1 to 9 of 9

Thread: Wierd Function

  1. #1

    Thread Starter
    Hyperactive Member Jenova's Avatar
    Join Date
    Feb 2006
    Location
    Googleplex
    Posts
    413

    Wierd Function

    Hi there,

    I just recently found out that you can leave a colon at the end of a function and still be able to call it without VB crying about it. I then decided to add a variable to and called it and it worked. I just wanted to know if any one had seen this type of syntax for a function?

    VB Code:
    1. Option Explicit
    2.  
    3. Private Function TextFunction(): Dim x As String, y As String
    4.     x = "Hello"
    5.     y = "World"
    6.    
    7.     MsgBox x & " " & y
    8. End Function
    9.  
    10. Private Sub Form_Load()
    11.     Call TextFunction
    12. End Sub

    Regards,

    Jenova

  2. #2

  3. #3
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Wierd Function

    Yes. Most people regard it as bad programming because it makes code hard to read. I usually use it for statements which are in the same context:
    VB Code:
    1. text1.Text = "": text2.Text = ""
    etc..

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  4. #4

    Thread Starter
    Hyperactive Member Jenova's Avatar
    Join Date
    Feb 2006
    Location
    Googleplex
    Posts
    413

    Re: Wierd Function

    Ah I see

    Thanks man

    Regards,

    Jenova

  5. #5
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: Wierd Function

    Quote Originally Posted by MartinLiss
    Everything after the colon is treated as if it was a new line. In other words you can do this if you want but it's poor programming style.

    If x = 5 Then: y = 12: End If
    That may have been a bad example, as with VB you can just say
    VB Code:
    1. If x = 5 Then y = 12
    and leave it at that...

  6. #6
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Wierd Function

    I've always asked myself why VB doesn't allow to intialize a variable after the declaration.
    VB Code:
    1. Dim someVariable As Integer: someVariable = 10
    Of course, this must be done and it can only be done in a procedure.

  7. #7

  8. #8
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Wierd Function

    Quote Originally Posted by gavio
    I've always asked myself why VB doesn't allow to intialize a variable after the declaration.
    VB Code:
    1. Dim someVariable As Integer: someVariable = 10
    Of course, this must be done and it can only be done in a procedure.
    I believe .Net allows you to do that.

  9. #9

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