locked
String to an array RRS feed

  • Question

  • For a project I need to take a string of numbers from a textbox that the user will input and assign the string of inputted numbers to an array, I need to do this for two separate arrays and textboxes. Then I need to be able to add and subtract the arrays. I'm not even sure where to begin. I've only declared my variables, any help would be greatly appreciated!
    Thursday, November 24, 2016 5:06 AM

All replies

  • For a project I need to take a string of numbers from a textbox that the user will input and assign the string of inputted numbers to an array, I need to do this for two separate arrays and textboxes. Then I need to be able to add and subtract the arrays. I'm not even sure where to begin. I've only declared my variables, any help would be greatly appreciated!

    Is the user is inputting all the numbers into the text box as one string?  If so, you need to specify the format of the string so that a rule can be set out which enables the string to be split apart for each number.

    If your user is going to be entering the numbers one at a time, then you need to indicate how the user interface is going to work.  For instance, will they enter a number and then press a OK button?  How will they indicate that they have finished entering numbers?

    Generally, if you take the time to write out the task in detail, considering every single tiny step of the process, the solution in code becomes much clearer.

    Thursday, November 24, 2016 5:13 AM
  • Well TextBox's contain strings. Numeric types are not string types. So at some point all numbers input in a TextBox would need to be converted to the correct numeric type that you want to perform math operations on.

    A string of numbers in a TextBox is what? Numeric string characters separated by spaces? If so then split the TextBox text to a string array based on a space characters.

    Then you will need to verify that every thing in the string array is valid numeric type for whatever you want to provide them to for performing math on them.

    It doesn't do any good to pre-declare variables if you have no concept of how to perform operations to get a string from a TextBox that will end up needing to be split probably and placed at some point in some type of numeric array (Integer, Double, Decimal, whatever).

    What does adding and subtracting arrays mean? Arrays have indexes containing some data type typically. So even if you had an array of double with 3 indexes containing double values then what does adding or subtracting it mean?


    La vida loca

    Thursday, November 24, 2016 5:15 AM
  • VBN,

    English is not the easiest language to describe this kind of problems. However, in English there is a word for cipher(or digit). A number is just a word to tell the place from zero to x. Also there are words like count,  quantity and value which describes how a string (array) of ciphers is used.

    Please use those words because now your question asks probably something else than you mean.

    Sorry guys that I tell this, but I know how difficult it often is  as not a born English speaker to describe this kind of things.

      


    Success
    Cor

    Thursday, November 24, 2016 10:47 AM
  • If the user were to enter numbers into a TextBox with a delimiter the following is one method to convert the items to a integer array.

    If TextBox1.Text.Contains(",") Then
        Dim IntegerArray() As Integer = TextBox1.Text.Split(","c).Select(
        Function(item)
            Dim result As Integer = 0
            If Integer.TryParse(item, result) Then
                Return result
            Else
                Return 0
            End If
        End Function).ToArray()
    
        MessageBox.Show(String.Join(Environment.NewLine, IntegerArray))
    End If

     

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Proposed as answer by Neda Zhang Friday, November 25, 2016 4:55 AM
    Thursday, November 24, 2016 11:24 AM