PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
studio 2012 split string with if statement not sure how to go about this-VBForums
Results 1 to 10 of 10

Thread: studio 2012 split string with if statement not sure how to go about this

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2013
    Posts
    122

    studio 2012 split string with if statement not sure how to go about this

    hey guys
    i have a working program that loops through a list and does stuff
    in the textbox1 i would have lets say "aaa"
    now i would like to add a job list to to script so if i put into the textbox "aaa,bbb,ccc"
    it would do the whole job 3 times with all three instructions

    i can split the textbox into an array like this
    Dim joblist As String() = TextBox1.Text.Split(New Char() {","c})

    and make a do while list with the size of the array and increment the array each time

    but how do i check to see if the textbox has the character "," and only run the loop if it does

    any ideas

  2. #2
    Frenzied Member HanneSThEGreaT's Avatar
    Join Date
    Nov 2003
    Location
    Vereeniging, South Africa
    Posts
    1,491

    Re: studio 2012 split string with if statement not sure how to go about this

    You could perhaps consider using IndexOf :

    http://msdn.microsoft.com/en-us/library/k8b1470s.aspx
    VB.NET MVP 2008 - Present

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    103,892

    Re: studio 2012 split string with if statement not sure how to go about this

    You wouldn't use a Do loop in this case. There are four types of loops: For, For Each, Do and While. You should only be using a Do or While loop if you don't know exactly how many times you need to loop when you start. In this case, you know exactly how many times you need to loop, i.e. the number of elements in the array. In that case, a For loop would be more appropriate than a Do or While loop. This case is even more specific though, i.e. you want to perform an action for each element in the array. What sort of lop do you think would be most appropriate then?

    To answer your question, don't bother. Just run the For Each loop and, if there's only one element, the loop will only run for one iteration.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    May 2013
    Posts
    122

    Re: studio 2012 split string with if statement not sure how to go about this

    Quote Originally Posted by jmcilhinney View Post
    You wouldn't use a Do loop in this case. There are four types of loops: For, For Each, Do and While. You should only be using a Do or While loop if you don't know exactly how many times you need to loop when you start. In this case, you know exactly how many times you need to loop, i.e. the number of elements in the array. In that case, a For loop would be more appropriate than a Do or While loop. This case is even more specific though, i.e. you want to perform an action for each element in the array. What sort of lop do you think would be most appropriate then?

    To answer your question, don't bother. Just run the For Each loop and, if there's only one element, the loop will only run for one iteration.
    i can't argue with that
    the issue i have now is making the array when there is no "," in the textbox
    using this
    Dim joblist As String() = TextBox1.Text.Split(New Char() {","c})

    it will work fine with "aaa,bbb,ccc,ddd"
    but will error when there is only "aaa"

    how would i overcome this

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    103,892

    Re: studio 2012 split string with if statement not sure how to go about this

    Firstly, if all you want to do is split on a single Char, there's no need to create a Char array. That overload of String.Split has its parameter declared as a ParamArray, meaning that it will accept a single Char array or zero, one or more discrete Chars. You only need this:
    Code:
    Dim joblist As String() = TextBox1.Text.Split(","c)
    As for the "issue", there's no issue at all. If the String doesn't contain the delimiter(s) then Split will simply return an array with a single element containing the original String. As I said, if you use a For Each loop, it will just work. If you get an error then you're doing it wrong but we can't tellyou what's wrong with your code if we haven't seen it or the error.

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    9,246

    Re: studio 2012 split string with if statement not sure how to go about this

    The split should not error no matter what is in the textbox. Try it like this

    Code:
            Dim joblist() As String = TextBox1.Text.Split(New Char() {","c}, StringSplitOptions.RemoveEmptyEntries)
            For x As Integer = 0 To joblist.Length - 1
                doSomething(joblist(x))
            Next
    
            'or
    
            Dim joblist() As String = TextBox1.Text.Split(New Char() {","c}, StringSplitOptions.RemoveEmptyEntries)
            For Each s As String In joblist
                doSomething(s)
            Next
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7

    Thread Starter
    Lively Member
    Join Date
    May 2013
    Posts
    122

    Re: studio 2012 split string with if statement not sure how to go about this

    its ok i sussed it
    Dim joblist As String() = TextBox1.Text.Split(New Char() {","c})


    Dim itm As Object
    For Each itm In joblist

    MsgBox(itm)
    '' my code here
    Next itm

    thanx for help guyz

  8. #8
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,329

    Re: studio 2012 split string with if statement not sure how to go about this

    Dim itm As Object??? Ahhhhh what planet are you learning on? Go into options are turn option strict on. You don't need to declare a variable for the loop either way.

  9. #9
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,329

    Re: studio 2012 split string with if statement not sure how to go about this

    Does post 6 look like that? http://www.vbforums.com/showthread.p...=1#post4528193

    If you are worried the string will throw an exception if it does not contain a , then you can either check the string contains the delimiter or maybe use regex.split,

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Example()
    4.  
    5.         Dim joblist() As String = System.Text.RegularExpressions.Regex.Split(Me.TextBox1.Text, ",")
    6.  
    7.         For Each job In joblist
    8.             MessageBox.Show(job)
    9.         Next
    10.  
    11.     End Sub
    12. End Class

  10. #10

    Thread Starter
    Lively Member
    Join Date
    May 2013
    Posts
    122

    Re: studio 2012 split string with if statement not sure how to go about this

    Quote Originally Posted by ident View Post
    Does post 6 look like that? http://www.vbforums.com/showthread.p...=1#post4528193

    If you are worried the string will throw an exception if it does not contain a , then you can either check the string contains the delimiter or maybe use regex.split,

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Example()
    4.  
    5.         Dim joblist() As String = System.Text.RegularExpressions.Regex.Split(Me.TextBox1.Text, ",")
    6.  
    7.         For Each job In joblist
    8.             MessageBox.Show(job)
    9.         Next
    10.  
    11.     End Sub
    12. End Class
    to be honest the way i learn is google something similar then alter it to work with and if i get issues i tend to ask for help and hopefully understand why it works
    but my code works well now

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width