[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 226
  • Last Modified:

Function return value

Hello, Experts!
Let say I have function like

    Function newStr(ByVal str As String)
        Dim str_double As String
        Dim str_tripple As String

        str_double = str & str
        str_tripple = str & str & str
        Return str_double
        Return str_tripple
    End Function

How to call this function on a page, and be able to see the str_double and str_tripple?

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub


Thanks for any help.
0
bogdem
Asked:
bogdem
  • 5
  • 4
  • 3
  • +1
1 Solution
 
KudzullcCommented:
bogdem,
Private Function newStr(ByVal str As String)
        Dim str_double As String
        Dim str_tripple As String

        str_double = str & str
        str_tripple = str & str & str
        Return str_double
        Return str_tripple
    End Function


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

newStr(MsgBox(str_double & " is Double the Fun" & vbCrLf & str_tripple & " is even better!"))

End Sub

Try that!

gl,
Lucas
0
 
KudzullcCommented:
Ha, rather this!  doh!

Private Function newStr(ByVal str As String)
        Dim str_double As String
        Dim str_tripple As String

        str_double = str & str
        str_tripple = str & str & str
        Return str_double
        Return str_tripple
    End Function


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

newStr(TextBox1.Text.toString)

End Sub

Sorry was in hurry!

Lucas
0
 
bogdemAuthor Commented:
Empty textBox on the page.
What string are you passing to the function?

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
KudzullcCommented:
bogdem,

I am not sure if you can return two values from a function.  Try this...

Dim str_double as String
Dim str_tripple as String

Private Function newStr(ByVal str As String)
        str_double = str & str
        str_tripple = str & str & str
End Function


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim test as string = "apple"
newStr(test)
MsgBox(str_double & " is Double the Fun" & vbCrLf & str_tripple & " is even better!"))
End Sub

Sorry for the number of posts with bad code...  :-/

Lucas
0
 
YZlatCommented:
I'd use a Sub instead of a function since you can't have two return statements:

 Sub newStr(ByVal str As String,ByVal str_double as string, ByVal str_tripple as string )
        str_double = str & str
        str_tripple = str & str & str
 End Sub

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
          Dim str_double As String
        Dim str_tripple As String
        newStr(str,str_double,str_tripple)
         MsgBox(str_double & " is Double the Fun" & vbCrLf & str_tripple & " is even better!"))
    End Sub
0
 
bogdemAuthor Commented:
Here is what I have

Partial Class _Default
    Inherits System.Web.UI.Page



    Sub newStr(ByVal str As String, ByVal str_double As String, ByVal str_tripple As String)


        str_double = str & str
        str_tripple = str & str & str

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim str_double As String
        Dim str_tripple As String
        Dim str As String = "Hello "
        newStr(str, str_double, str_tripple)
        Response.Write("double " & str_double & "<br>")
        Response.Write("tripple " & str_tripple)
    End Sub
End Class


NO values from Sub
0
 
KudzullcCommented:
bogdem,
Are you trying this code for ASP?
Lucas
0
 
bogdemAuthor Commented:
Sorry, I'm in the wront branch :(.
Yes I'm trying it for ASP.NET
0
 
KudzullcCommented:
No Problem!  :)

I know very little about ASP!

gl,
Lucas
0
 
YZlatCommented:
Try adding two Labels:
 Label1 and Label2:

Sub newStr(ByVal str As String, ByVal str_double As String, ByVal str_tripple As String)


        str_double = str & str
        str_tripple = str & str & str

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim str_double As String
        Dim str_tripple As String
        Dim str As String = "Hello "
        newStr(str, str_double, str_tripple)
        Label1.Text="double " & str_double
        Label2.Text="tripple " & str_tripple
    End Sub
0
 
YZlatCommented:
change ByVal to ByRef
0
 
YZlatCommented:
Partial Class _Default
    Inherits System.Web.UI.Page



    Sub newStr(ByVal str As String, ByRef str_double As String, ByRef str_tripple As String)


        str_double = str & str
        str_tripple = str & str & str

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim str_double As String
        Dim str_tripple As String
        Dim str As String = "Hello "
        newStr(str, str_double, str_tripple)
        Response.Write("double " & str_double & "<br>")
        Response.Write("tripple " & str_tripple)
    End Sub
End Class
0
 
farsightCommented:
Better to have 2 separate functions, each with a single well-defined purpose.
Plus, it makes the code a LOT simpler, because we don't need all those pesky variables.

    Function DoubleString(ByVal str As String) As String
        Return str & str
    End Function

    Function TrippleString(ByVal str As String) As String
        Return str & str & str
    End Function

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim str As String = "Hello "

        Response.Write("double " & DoubleString(str) & "<br>")
        Response.Write("tripple " & TrippleString(str))
    End Sub

Simple, huh?
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 5
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now