Results 1 to 6 of 6

Thread: Array Issue

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2011
    Posts
    14

    Array Issue

    I have the following snippit of code.

    Dim intMax As Integer = 5
    Dim OriginalString As String = txtRecord.Text
    Dim OriginalArray As String() = Strings.Split(OriginalString, ",")

    Dim FinalArrayGeneric As New List(Of String)
    Dim FinalArray As String()



    For intCount As Integer = 0 To UBound(OriginalArray)
    If intCount <= intMax Then
    FinalArrayGeneric.Add(OriginalArray(intCount))
    End If
    Next



    FinalArray = FinalArrayGeneric.ToArray()

    If IsNothing(FinalArray(5)) Then
    FinalArray(5) = " "

    End If

    txtField1.Text = FinalArray(0).ToString()
    txtField2.Text = FinalArray(1).ToString()
    txtField3.Text = FinalArray(2).ToString()
    txtField4.Text = FinalArray(3).ToString()
    txtField5.Text = FinalArray(4).ToString()
    txtField6.Text = FinalArray(5).ToString()



    This is a class project and it does basically what I need it to do. Here is a quick breakdown.

    There is a textbox area for someone to enter a sentence that turns to an array and they want us to break it out by commas up to 6 fields. Anything over 6 fields I need to just drop which this seems to do. My issue seems to be when I dont have enough commas (indexes for the text boxes).

    In my mind I thought this would work

    If IsNothing(FinalArray(5)) Then
    FinalArray(5) = " "

    but unfortunatly it doesnt. Also I wanted to do it more programatically than listing out all 6 boxes incase more would be added.


    This is a test sentence

    Record #10, John E. Doe, 123 Main Street, Columbus, Ohio 43232

    Currently it breaks everything out but since the FinalArray(5) is null I get an error.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Array Issue

    The number of elements in your array is determined by the number of items in your List. You can't assume that the array has 6 elements because it might have anything from 1 to 6. You need to write code that will work regardless of the number of elements. The obvious solution is to use a loop that goes from 0, which is the index of the first element, to the index of the last element, which you can get by calling GetUpperBound on the array.

    Also, is 'FinalArrayGeneric' really a sensible name for something that is neither final nor an array?

    Having said all that, I just had a closer look at your code and it is needlessly complex. You already have an array so do you really need to create a List and then another array? You could simply loop though the first array from 0 to the appropriate upper bound, which would be the lesser of 5 and the actual upper bound of the array. You can use Math.Min to get the lesser of two numbers.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2011
    Posts
    14

    Re: Array Issue

    jmcilhinney,

    I'm still pretty new to VB. I'm taking classes on it right now. The book that we're to use for the classes, I have not found anything similar to what I need unless I've missed something.

    As for the naming. I once I make sure the code is working as it should I'll rename it.

    You said that it's needessly complex, Can you give an example by pseudocode by chance ? Arrays are still very new to me and loops are still new to me as well. I just learned about those less than a month ago. I have the tendency to make things harder than they really are so by all means if there is an easier way then I'm all ears.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Array Issue

    You can write your own pseudo-code. Programming doesn't exist in a vacuum. Let's say that I had a number of egg cartons with different numbers of eggs in them. Let's say that I handed them to you one by one and I told you to crack the eggs in each carton up to a maximum of six. You could do that, right? So, write out a set of steps that you could follow to do it, then convert that to pseudo-code, then write VB code to implement it. This is basically the same scenario.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,470

    Re: Array Issue

    quick fix

    dimension your final array to the size you need not ()

    initialise the final array i.e. give it default values, in your case a space - chr(32)

    so when you underfill it the remainder exists as blanks - a single space

    but if you had made your textboxes an array of textboxes then you would have initialised and then filled them directly how is a nother story - i am not on my development machine - away from home!

    there are better ways to do this but the fix is all you require to "be done"

    here to talk

  6. #6
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,470

    Re: Array Issue

    or even easier

    forget the above and ensure you have enough to fill your boxes

    when you have a digital clock number it need to be 01 02 03 so you add 0 to the front of every number and just use the last 2 digits

    that approach removes all testing and is way quicker unless the numbers all come correct all of the time, but if that was true you would never have had the problem to solve would you?

    your problem is possibly not enough stuff to show

    so make some up, enough that is to ensure you no longer have the problem!

    so simply add the number of "must collect" empty inputs

    inputtextbox=inputtextbox & ",' ',' ',' ',' ',' '" or however you can add the comma separated spaces

    now even an empty inputtextbox can be processed

    here to talk

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