Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 8th, 2011, 08:27 AM
jasonlenso jasonlenso is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2011
Posts: 2 jasonlenso User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 25 m
Reputation Power: 0
Vb sub procedure

hi, I have got some problem with the vb sub procedure

Below is the program
Private Sub btnDisplay_Click(...) Handles btnDisplay.Click
Dim a As String = "abc"
cut(a)
textbox1.Text =a
End Sub
Sub cut(ByRef a As String)
a= a.Substring(2)
End Sub

for this the return is c

but for another program
Private Sub btnDisplay_Click(...) Handles btnDisplay.Click
Dim a As String = "abc"
cut((a))
textbox1.Text =a
End Sub
Sub cut(ByRef a As String)
a= a.Substring(2)
End Sub

The return is abc

Can any one explain it? Thanks

Reply With Quote
  #2  
Old December 9th, 2011, 01:27 PM
medialint's Avatar
medialint medialint is offline
Type Cast Exception
Dev Shed God 20th Plane (14500 - 14999 posts)
 
Join Date: Apr 2004
Location: OAKLAND CA | Adam's Point (Fairyland)
Posts: 14,938 medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)  Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 6 Months 2 Weeks 2 Days 26 m 49 sec
Reputation Power: 8490
Facebook
Both could be replaced by a single line of code. Those procedures look like they were built using the "smack it until it works" approach.

Instead of passing things to a function you're using byref to operate on the variable in the sub. I don't know what platform you're on so a VBA equivalent would be:

Code:
Sub Cut(ByRef a As String)
    a = Mid(a, 2, 1)
End Sub

Sub Test()
    Dim c As String
    c = "abc"
    Cut c
    Debug.Print c
    c = "abc"
    Cut (c)
    Debug.Print c
End Sub


When you add the parenthesis () you're forcing it to pass by value instead of by reference. Write a function instead of this nonsense.
__________________
medialint.com

“Today you are You, that is truer than true. There is no one alive who is Youer than You.” - Dr. Seuss

Last edited by medialint : December 9th, 2011 at 01:34 PM.

Reply With Quote
  #3  
Old December 9th, 2011, 08:33 PM
jasonlenso jasonlenso is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2011
Posts: 2 jasonlenso User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 25 m
Reputation Power: 0
Smile

Quote:
Originally Posted by medialint
Both could be replaced by a single line of code. Those procedures look like they were built using the "smack it until it works" approach.

Instead of passing things to a function you're using byref to operate on the variable in the sub. I don't know what platform you're on so a VBA equivalent would be:

Code:
Sub Cut(ByRef a As String)
    a = Mid(a, 2, 1)
End Sub

Sub Test()
    Dim c As String
    c = "abc"
    Cut c
    Debug.Print c
    c = "abc"
    Cut (c)
    Debug.Print c
End Sub


When you add the parenthesis () you're forcing it to pass by value instead of by reference. Write a function instead of this nonsense.


Thanks for your help;

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming > Vb sub procedure

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap