7 Replies - 2718 Views - Last Post: 08 December 2012 - 02:49 AM Rate Topic: -----

#1 VisuallyBasic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 16-November 12

Returning Array from a Function

Posted 01 December 2012 - 07:50 PM

Hey guys, I've worked far long enough so this is kind of a last resort kinda post. I'm having trouble with functions and arrays, I need to write a function that returns an array of userinput (text from textbox, split by characters/spaces). I can't seem to work out returning the array to use on my other functions, anybody have an idea what I'm doing wrong? Maybe what I should be doing instead? I am a novice in VB.Net- sometimes it clicks sometimes it doesn't, please let me know what you guys think. Thanks.

This is the incomplete code I currently have in my function, for grabbing userinput to begin with...
    Public Function Example(ByVal test As String) As String()

        Dim str As String
        Dim strArr() As String
        Dim count As Integer
        Dim output As String

        str = Form1.Textbox1.Text
        strArr = str.Split(CChar(CChar(",") & (" ")))

        For count = 0 To strArr.GetUpperBound(0)
            MessageBox.Show(strArr(count))

        Next
       Return ?
    End Function




Below is my original sum formula for finding numbers between comma's, without using an Array.

Public Function SumFunction(ByVal srchTerm As String, ByVal stringToSearch As String) As Integer

    Dim intGetPosition As Integer = stringToSearch.IndexOf(srchTerm)

    Dim comma As Integer = 0
    Dim comma2 As Integer = 0
    Dim count As Integer = 0
    Dim total As Integer = 0
    Dim number As String

    Do While comma > -1
        Try
            comma = stringToSearch.IndexOf(srchTerm, comma2)

            'If/When there's no comma at the end...
            If comma = -1 Then
                comma = stringToSearch.IndexOf(srchTerm, comma2)
                number = stringToSearch.Substring(comma2)
                total = CInt(total + CDbl(number))
                comma2 = comma + 1
                count = count + 1
            End If

            'When there's still a comma at the end to look for...
            number = stringToSearch.Substring(comma2, comma - comma2)
            total = CInt(total + CDbl(number))
            comma2 = comma + 1
            count = count + 1
        Catch ex As Exception
            'Catch and do nothing
        End Try
    Loop

    'Return total sum
    Return total
End Function

Public Sub SumSub()

    'calling function "SumFunction" FOR a comma string, INSIDE of textbox1.text
    Dim Sum As Integer = SumFunction(",", t1.Text)

    'OUTPUT SUM FUNCTION TO TEXTBOX
    t1.Text = CStr(Sum)

End Sub

Public Sub btnTotal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTotal.Click

    Call SumSub()

End Sub


Is This A Good Question/Topic? 0
  • +

Replies To: Returning Array from a Function

#2 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Returning Array from a Function

Posted 01 December 2012 - 09:39 PM

The line you want is Return strArr

Of course you have to call it properly, assigning the return to a string array.

Here's a small test program, based on your Example() function:
Option Strict On
Option Explicit On

Public Class Form1

    Private Userinput() As String

    Public Function Example(ByVal test As String) As String()
        Dim strArr() As String
        strArr = test.Split(CChar(","))
        Return strArr
    End Function

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Userinput = Example(TextBox1.Text)
    End Sub
End Class


This post has been edited by lar3ry: 01 December 2012 - 09:40 PM

Was This Post Helpful? 0
  • +
  • -

#3 VisuallyBasic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 16-November 12

Re: Returning Array from a Function

Posted 01 December 2012 - 10:13 PM

View Postlar3ry, on 01 December 2012 - 09:39 PM, said:

The line you want is Return strArr

Of course you have to call it properly, assigning the return to a string array.

Here's a small test program, based on your Example() function:
Option Strict On
Option Explicit On

Public Class Form1

    Private Userinput() As String

    Public Function Example(ByVal test As String) As String()
        Dim strArr() As String
        strArr = test.Split(CChar(","))
        Return strArr
    End Function

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Userinput = Example(TextBox1.Text)
    End Sub
End Class



Thanks lar3ry, you know, I really thought this week with the array's would more straight forward. Come to findout I was wrong, again! I think I may pass on this one, it'll be a first but I'm approaching my limit. Probably try for another couple hours...

I hate asking but whatever- you know my original code? Is there a simpler way to convert that to an array that I'm not seeing? I have a feeling that I'm overlooking things and there could be a way easier method...
Was This Post Helpful? 0
  • +
  • -

#4 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Returning Array from a Function

Posted 01 December 2012 - 10:36 PM

View PostVisuallyBasic, on 01 December 2012 - 11:13 PM, said:

View Postlar3ry, on 01 December 2012 - 09:39 PM, said:

Option Strict On
Option Explicit On

Public Class Form1

    Private Userinput() As String

    Public Function Example(ByVal test As String) As String()
        Dim strArr() As String
        strArr = test.Split(CChar(","))
        Return strArr
    End Function

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Userinput = Example(TextBox1.Text)
    End Sub
End Class



Thanks lar3ry, you know, I really thought this week with the array's would more straight forward. Come to findout I was wrong, again! I think I may pass on this one, it'll be a first but I'm approaching my limit. Probably try for another couple hours...

I hate asking but whatever- you know my original code? Is there a simpler way to convert that to an array that I'm not seeing? I have a feeling that I'm overlooking things and there could be a way easier method...

It is straightforward!
Your Example() subroutine is the answer to how you can do it an esier way! Unless I am missing something, your original code's purpose was to take a string like "29,45,96,38", and fill an array with the numeric values, discarding the commas, then to sum the numeric contents of the array.

Well, the function I gave you back is the easiest way I can think of to do the string to array assignment. As for the sum of the values, a simple For loop, adding each CInt(array(n)) value to Total, will do the trick.

Perhaps I am missing something, though. Let me know if I am, will you?
Was This Post Helpful? 0
  • +
  • -

#5 VisuallyBasic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 16-November 12

Re: Returning Array from a Function

Posted 01 December 2012 - 11:57 PM

View Postlar3ry, on 01 December 2012 - 10:36 PM, said:

View PostVisuallyBasic, on 01 December 2012 - 11:13 PM, said:

View Postlar3ry, on 01 December 2012 - 09:39 PM, said:

Option Strict On
Option Explicit On

Public Class Form1

    Private Userinput() As String

    Public Function Example(ByVal test As String) As String()
        Dim strArr() As String
        strArr = test.Split(CChar(","))
        Return strArr
    End Function

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Userinput = Example(TextBox1.Text)
    End Sub
End Class



Thanks lar3ry, you know, I really thought this week with the array's would more straight forward. Come to findout I was wrong, again! I think I may pass on this one, it'll be a first but I'm approaching my limit. Probably try for another couple hours...

I hate asking but whatever- you know my original code? Is there a simpler way to convert that to an array that I'm not seeing? I have a feeling that I'm overlooking things and there could be a way easier method...

It is straightforward!
Your Example() subroutine is the answer to how you can do it an esier way! Unless I am missing something, your original code's purpose was to take a string like "29,45,96,38", and fill an array with the numeric values, discarding the commas, then to sum the numeric contents of the array.

Well, the function I gave you back is the easiest way I can think of to do the string to array assignment. As for the sum of the values, a simple For loop, adding each CInt(array(n)) value to Total, will do the trick.

Perhaps I am missing something, though. Let me know if I am, will you?


To be honest, there's always little snippets of knowledge I feel like I don't know in order to proceed. Anyways, got a spark of motivation after your reply, this' what I got as of now...

    Private userinput() As String

    Public Function GetValues(ByVal test As String) As String()

        Dim inputArray() As String
        inputArray = test.Split(CChar(CChar(",") & (" ")))

        Return inputArray

    End Function

    Sub ValuesSub()

        userinput = GetValues(Form1.Textbox1.Text)

    End Sub

    Public Function SumValues(ByVal test As String) As String

        Dim inputArray() As String
        inputArray = test.Split(CChar(CChar(",") & (" ")))
        Dim total As Integer = 0

        For count As Integer = 0 To inputArray.GetUpperBound(0)
            total = total + CInt(inputArray(count))
        Next

        Return CStr(total)

    End Function

    Sub SumSub()

        Dim Sum As Integer
        Sum = CInt(SumValues(Form1.Textbox1.Text))

        MessageBox.Show(Sum.ToString)

    End Sub


I feel like there should be another way to NOT have to copy and paste the code for every other function I decide to create... say total and average for instance, do I need to copy and paste it AGAIN? Or is there another way to return 2 values from function?
Was This Post Helpful? 0
  • +
  • -

#6 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: Returning Array from a Function

Posted 02 December 2012 - 02:02 AM

View PostVisuallyBasic, on 02 December 2012 - 03:50 AM, said:

I need to write a function that returns an array of userinput (text from textbox, split by characters/spaces).


You don't need to reinvent the wheel. A String is Char() (array of characters). So if you want to use it as Char(), you already have a .ToArray() extension function in .NET for String type members.

For example:
        Dim word As String = "something"
        Dim chrs() As Char = word.ToArray



You also discovered .Split extension function for String type members, so all you need is there.

Split large text by spaces, iterate through results (like with For...Next, or other suitable looping method), and transform each result ToArray. There you go.
Was This Post Helpful? 0
  • +
  • -

#7 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Returning Array from a Function

Posted 02 December 2012 - 08:13 AM

View PostVisuallyBasic, on 02 December 2012 - 12:57 AM, said:

I feel like there should be another way to NOT have to copy and paste the code for every other function I decide to create... say total and average for instance, do I need to copy and paste it AGAIN? Or is there another way to return 2 values from function?

Ahh! I see the problem. You think you have to call the Function that creates the array from the user's input every time you want to use it!

OK... a Subroutine (Sub)is a separate little package of code that does something. You write one when you want to do something more than once.

A Function is like a subroutine, but it returns something. You are not REQUIRED to use whatever it returns, but the whole point of a function is to call it and use whatever is returned. The parameters of the function, (the part inside the parentheses), are like temporary variables to hold whatever you pass to the function.

Take another look at your last code posted. ValuesSub() says "get the text from the textbox, pass it to the function GetValues(), and put the result in the string array called userinput." Easy enough to understand, right?

Now, you want to Sum all the values, and in your code, you use a function, and within that function, you Split something. Well, there are several problems here. The first is that you already have the entire user input. It's contained in the TextBox as a complete string, including commas, and also as a string array containing all the values, without the commas! userinput was declared outside any Sub or Function, but within the Class, so it is visible everwhere within the Class (between the Class and End Class statements).

The third problem in the SumValues() Function is that you are trying to split characters out of something, presumably the TextBox.Text string. You don't need to do this at all. What you need to do is to sum each value that is contained, in string form, within the userinput array.

So, in psuedo-code, here's what you do when you want to get the sum.
    Dim Total as Integer   ' this should go up there with userinput()

    Private Function SumValues(ByVal s() as String) as Integer
       Dim ttl as Integer
       'loop through s(), converting the string values to integer
       ' and adding each one to ttl
       Return ttl
    End Function



Now, right after calling GetValues, all you need to do is to code: Total = SumValues(userinput), and Total will contain the sm of values.

Then, do approximately the same for Average(), Mean(), LowestCommonDenominator(), or any other function you can think of.

A few notes:
  • in SumValues, s is a temporary variable, only visible in this function
  • ttl is the same sort of thing
  • you are returning an integer the (As Integer) on the Function Declaration line
  • You do not really NEED a function for SumValues, as you could do it in line, but it's a good thing to write it and refer to it later for possibly cutting/pasting into a new program.


Edit: There's no need to include so much. It just makes it difficult to wade through stuff that is already visible in the thread. Just quote a bit of relevant stuff if it would make things more clear.

This post has been edited by lar3ry: 02 December 2012 - 08:15 AM

Was This Post Helpful? 2
  • +
  • -

#8 VisuallyBasic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 16-November 12

Re: Returning Array from a Function

Posted 08 December 2012 - 02:49 AM

View Postlar3ry, on 02 December 2012 - 08:13 AM, said:

View PostVisuallyBasic, on 02 December 2012 - 12:57 AM, said:

I feel like there should be another way to NOT have to copy and paste the code for every other function I decide to create... say total and average for instance, do I need to copy and paste it AGAIN? Or is there another way to return 2 values from function?

Ahh! I see the problem. You think you have to call the Function that creates the array from the user's input every time you want to use it!

OK... a Subroutine (Sub)is a separate little package of code that does something. You write one when you want to do something more than once.

A Function is like a subroutine, but it returns something. You are not REQUIRED to use whatever it returns, but the whole point of a function is to call it and use whatever is returned. The parameters of the function, (the part inside the parentheses), are like temporary variables to hold whatever you pass to the function.

Take another look at your last code posted. ValuesSub() says "get the text from the textbox, pass it to the function GetValues(), and put the result in the string array called userinput." Easy enough to understand, right?

Now, you want to Sum all the values, and in your code, you use a function, and within that function, you Split something. Well, there are several problems here. The first is that you already have the entire user input. It's contained in the TextBox as a complete string, including commas, and also as a string array containing all the values, without the commas! userinput was declared outside any Sub or Function, but within the Class, so it is visible everwhere within the Class (between the Class and End Class statements).

The third problem in the SumValues() Function is that you are trying to split characters out of something, presumably the TextBox.Text string. You don't need to do this at all. What you need to do is to sum each value that is contained, in string form, within the userinput array.

So, in psuedo-code, here's what you do when you want to get the sum.
    Dim Total as Integer   ' this should go up there with userinput()

    Private Function SumValues(ByVal s() as String) as Integer
       Dim ttl as Integer
       'loop through s(), converting the string values to integer
       ' and adding each one to ttl
       Return ttl
    End Function



Now, right after calling GetValues, all you need to do is to code: Total = SumValues(userinput), and Total will contain the sm of values.

Then, do approximately the same for Average(), Mean(), LowestCommonDenominator(), or any other function you can think of.

A few notes:
  • in SumValues, s is a temporary variable, only visible in this function
  • ttl is the same sort of thing
  • you are returning an integer the (As Integer) on the Function Declaration line
  • You do not really NEED a function for SumValues, as you could do it in line, but it's a good thing to write it and refer to it later for possibly cutting/pasting into a new program.


Edit: There's no need to include so much. It just makes it difficult to wade through stuff that is already visible in the thread. Just quote a bit of relevant stuff if it would make things more clear.



WOW! Quite a bit of information! And it actually served me a great deal of help a week ago, mind you. Just wanted to check back in and say thanks for helping out, this week we're doing the same thing except using a text file- I've decided to start fresh just for the sake of understanding everything clearer. If/when I get to cleaning up my old code, I will be back here with any questions I've got for you/my finished code. I want to acknowledge all the issues you've brought up but I'll do that in a few hours after I get the txt file/Streamreader thing down...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1