Page 1 of 2 12 LastLast
Results 1 to 40 of 42

Thread: [RESOLVED] How many"k" are in my textboxes

  1. #1

    Thread Starter
    Fanatic Member memas's Avatar
    Join Date
    Jan 2010
    Location
    athens,greece
    Posts
    531

    Resolved [RESOLVED] How many"k" are in my textboxes

    How many"k" are in my textboxes?
    I want help for the code that will check a collection of 10 textboxes how many letters"k"are in my textboxes and to see the number in a label1
    thank you

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: How many"k" are in my textboxes

    vb.net Code:
    1. Dim CharCounter As Integer = 0
    2. For Each txtbox As TextBox In Me.Controls.OfType(Of TextBox)()
    3.     Dim c = Aggregate chars In txtbox.Text Where chars = "k"c Into Count()
    4.     CharCounter += c
    5. Next
    6. MsgBox("Total number of character k in all textboxes is:" + CharCounter.ToString)

  3. #3
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: How many"k" are in my textboxes

    That code will only work on .NET 3.5 wont it though cicatrix?

    An alternative that will work in .NET 2.0 would be something like:
    vb.net Code:
    1. Dim CharCounter As Integer = 0
    2. For Each txtbox As TextBox In Me.Controls.OfType(Of TextBox)()
    3.     For Each letter As Char In txtbox.Text
    4.          If letter = "k"c Then
    5.              CharCounter += c
    6.          End If
    7.      Next
    8. Next
    9. MessageBox.Show("Total number of character k in all textboxes is:" + CharCounter.ToString)
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  4. #4
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: How many"k" are in my textboxes

    Yes, my code is for .NET 3.5. I really don't see any reason to continue developing for .NET 2.0 after a newer version is available (and it's free).
    LinQ helps enormously and I can't imagine how I managed without it. Still, it's a little more than 'syntactic sugar' but it allows to focus on the main things, not the peculiarities of implementation.

  5. #5
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: How many"k" are in my textboxes

    Quote Originally Posted by cicatrix View Post
    Yes, my code is for .NET 3.5. I really don't see any reason to continue developing for .NET 2.0 after a newer version is available (and it's free).
    That is a little naive... a lot of people either have an existing program that is using .NET 2.0 that they have to support or they simply dont want to force their users to have to download and install the rather large 3.5 framework (plus SP1) package. A lot more people have .NET 2.0 installed (every Vista user for example as it comes pre-installed) than people have .NET 3.5 installed. I'm sure there are other reasons but those are the first things I think of - after all if there was no reason then every .NET developer would be using 3.5... and that clearly isn't the case.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  6. #6

  7. #7
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: How many"k" are in my textboxes

    Yeah I'm sure in this particular case using LINQ wont be a problem at all, I just thought it worth mentioning that it requires 3.5 in case the OP was not aware
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  8. #8

    Thread Starter
    Fanatic Member memas's Avatar
    Join Date
    Jan 2010
    Location
    athens,greece
    Posts
    531

    Re: How many"k" are in my textboxes

    cicatrix
    how can i will put the collection of mytextboxes? i dont want all the textboxes

  9. #9
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: How many"k" are in my textboxes

    Well, either you should have some criteria or use your custom collection:

    vb.net Code:
    1. Dim myCol As New List(Of TextBox)
    2.  
    3. myCol.Add(Textbox1)
    4. myCol.Add(Textbox5)
    ...
    etc

    the rest is the same:

    vb.net Code:
    1. Dim CharCounter As Integer = 0
    2. For Each txtbox As TextBox In myCol
    3.     For Each letter As Char In txtbox.Text
    4.          If letter = "k"c Then
    5.              CharCounter += c
    6.          End If
    7.      Next
    8. Next
    9. MessageBox.Show("Total number of character k in all textboxes is:" + CharCounter.ToString)

    P.S. If you use VS2003 or VS2005 you should use Chris's example.

  10. #10

    Thread Starter
    Fanatic Member memas's Avatar
    Join Date
    Jan 2010
    Location
    athens,greece
    Posts
    531

    Re: How many"k" are in my textboxes

    cicatrix
    1..if i have labels to take the k how will be the code for a group of labels ?

  11. #11
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: How many"k" are in my textboxes

    Quote Originally Posted by memas View Post
    cicatrix
    1..if i have labels to take the k how will be the code for a group of labels ?
    Guess what?

    vb.net Code:
    1. Dim myCol As New List(Of Label)

    In all examples of this thread all it really takes is change TextBox with Label

  12. #12
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: How many"k" are in my textboxes

    There is a really hideous pun that can be made about this thread, but it may be American only, so I shall spare you all.
    My usual boring signature: Nothing

  13. #13
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: How many"k" are in my textboxes

    Quote Originally Posted by shaggy hiker View Post
    there is a really hideous pun that can be made about this thread, but it may be american only, so i shall spare you all.
    do it
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  14. #14
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: How many"k" are in my textboxes

    Excuse me, kind sirs, but what is 'a pun'? (I know I'm good with my English, but not that good, apparently).

  15. #15

    Thread Starter
    Fanatic Member memas's Avatar
    Join Date
    Jan 2010
    Location
    athens,greece
    Posts
    531

    Re: How many"k" are in my textboxes

    cicatrix
    is correct this code because the textbox says 2 and the k are 5
    Private Sub Button72_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    'HOW MANY K
    Dim myCol As New List(Of Label)
    myCol.Add(Label201)
    myCol.Add(Label200)
    myCol.Add(Label199)
    myCol.Add(Label198)
    myCol.Add(Label197)
    myCol.Add(Label196)
    myCol.Add(Label195)
    myCol.Add(Label193)
    myCol.Add(Label190)
    myCol.Add(Label189)
    myCol.Add(Label188)
    myCol.Add(Label185)
    myCol.Add(Label181)
    myCol.Add(Label178)


    Dim CharCounter As Integer = 0
    For Each label As Label In Me.Controls.OfType(Of Label)()
    Dim c = Aggregate chars In label.Text Where chars = "k"c Into Count()
    CharCounter += c
    Next
    num34TextBox.Text = CharCounter.ToString

    End Sub

  16. #16
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: How many"k" are in my textboxes

    Quote Originally Posted by memas View Post
    cicatrix
    is correct this code because the textbox says 2 and the k are 5
    This is because you're not paying attention to what people tell you.
    If you use a custom collection then obviously you should iterate through it.

    Here you iterate through ALL texboxes, you need to iterate only throught myCol.

    Code:
    Private Sub Button72_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'HOW MANY K
            Dim myCol As New List(Of Label)
            myCol.Add(Label201)
            myCol.Add(Label200)
            myCol.Add(Label199)
            myCol.Add(Label198)
            myCol.Add(Label197)
            myCol.Add(Label196)
            myCol.Add(Label195)
            myCol.Add(Label193)
            myCol.Add(Label190)
            myCol.Add(Label189)
            myCol.Add(Label188)
            myCol.Add(Label185)
            myCol.Add(Label181)
            myCol.Add(Label178)
    
    
            Dim CharCounter As Integer = 0
            For Each label As Label In MyCol
                Dim c = Aggregate chars In label.Text Where chars = "k"c Into Count()
                CharCounter += c
            Next
            num34TextBox.Text = CharCounter.ToString
    
        End Sub

  17. #17

    Thread Starter
    Fanatic Member memas's Avatar
    Join Date
    Jan 2010
    Location
    athens,greece
    Posts
    531

    Re: How many"k" are in my textboxes

    cicatrix the textbox always says 0 zero why?

  18. #18
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: How many"k" are in my textboxes

    Hmm, this should work.
    Try renaming the label variable:

    Code:
    ...
    For Each lbl As Label In MyCol
        Dim c = Aggregate chars In lbl.Text Where chars = "k"c Into Count()
        CharCounter += c
    Next
    ...
    And it won't probably find 'K' (CAPITAL) - only 'k' (small)

  19. #19

    Thread Starter
    Fanatic Member memas's Avatar
    Join Date
    Jan 2010
    Location
    athens,greece
    Posts
    531

    Re: How many"k" are in my textboxes

    the same thing zero

  20. #20
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: How many"k" are in my textboxes

    Quote Originally Posted by cicatrix View Post
    Excuse me, kind sirs, but what is 'a pun'? (I know I'm good with my English, but not that good, apparently).
    Some people would call a pun the lowest form of humor, but it is actually just a play on words. For example, I should note that your code will find ALL the ks in a string, including the very rare case where two Ks come one after the other. This is very rare, and could be considered a special K because it refers to serial ks.

    Now, in case it is not sold everywhere, Special K is a type of breakfast cereal.
    My usual boring signature: Nothing

  21. #21

    Thread Starter
    Fanatic Member memas's Avatar
    Join Date
    Jan 2010
    Location
    athens,greece
    Posts
    531

    Re: How many"k" are in my textboxes

    shaggy hiker
    why you dont try to help?

  22. #22
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: How many"k" are in my textboxes

    its sold here in the UK... and congrats on the terrible pun haha
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  23. #23
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: How many"k" are in my textboxes

    Quote Originally Posted by memas View Post
    the same thing zero
    Do these labels actually hold text containing small k English characters?

    What is the text in your labels? Can you post some example?


    P.S.
    @Shaggy hiker: I may be not in a position to judge, but I too think it's a terrible pun

  24. #24
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: How many"k" are in my textboxes

    Quote Originally Posted by memas View Post
    shaggy hiker
    why you dont try to help?
    yeahhhh shaggy hiker, you're so unhelpful. Sometimes it makes me want to cry.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  25. #25

    Thread Starter
    Fanatic Member memas's Avatar
    Join Date
    Jan 2010
    Location
    athens,greece
    Posts
    531

    Re: How many"k" are in my textboxes

    cicatrix is working the mistake is mine thank you

  26. #26
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: How many"k" are in my textboxes

    Quote Originally Posted by memas View Post
    shaggy hiker
    why you dont try to help?
    Sorry, I don't deal in LINQ, so as long as Cicatrix is going down that road and is approaching a solution to the problem, I have nothing to add.

    However, I would suggest that doing something like this:
    Code:
    For Each label As Label In MyCol
        CharCounter += System.Text.RegularExpressions.Regex.Matches(label.Text,"[Kk]").Count
    Next
    would probably be faster. Haven't tested it, though, but it should match all K or k in the string and return the count of all such matches in the string.
    My usual boring signature: Nothing

  27. #27
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: How many"k" are in my textboxes

    Simpler (and without regex):
    Code:
     c = Aggregate chars In txtbox.Text Where (chars = "k"c Or chars = "K"c) Into Count()

  28. #28
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: How many"k" are in my textboxes

    Perhaps, but LINQ isn't fast, so I would question whether or not Regex isn't the faster solution.
    My usual boring signature: Nothing

  29. #29
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: How many"k" are in my textboxes

    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim myCol As New List(Of Label)
            For Each l As Label In myCol
                num34TextBox.Text = countK(l.Text, False).ToString
            Next
    
        End Sub
    
        Dim theCount As Integer = 0
        Private Function countK(ByVal inWhat As String, _
                                Optional ByVal CaseMatters As Boolean = True) As Integer
    
            theCount += inWhat.Count(Function(c) c = "k"c)
            If Not CaseMatters Then theCount += inWhat.Count(Function(c) c = "K"c)
            Return theCount
    
        End Function
    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

  30. #30

    Thread Starter
    Fanatic Member memas's Avatar
    Join Date
    Jan 2010
    Location
    athens,greece
    Posts
    531

    Re: How many"k" are in my textboxes

    CICATRIX
    my mistake was that the k was with capital letter thats why i have problem now that i change the k in code with capital K is working perfect
    thank you again

  31. #31

    Thread Starter
    Fanatic Member memas's Avatar
    Join Date
    Jan 2010
    Location
    athens,greece
    Posts
    531

    Re: How many"k" are in my textboxes

    dbasnett
    with this code i have errors

  32. #32
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: How many"k" are in my textboxes

    Quote Originally Posted by memas View Post
    dbasnett
    with this code i have errors
    My internet telepathy is down today, so if you could be more explicit in the details of your difficulty, perhaps I could help. Post the code.
    Last edited by dbasnett; Feb 8th, 2010 at 06:20 PM.
    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

  33. #33

    Thread Starter
    Fanatic Member memas's Avatar
    Join Date
    Jan 2010
    Location
    athens,greece
    Posts
    531

    Re: How many"k" are in my textboxes

    relax for today tomorow i hope you are ok and i will ask you a difficult question
    memas

  34. #34
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: How many"k" are in my textboxes

    Quote Originally Posted by Shaggy Hiker View Post
    Perhaps, but LINQ isn't fast, so I would question whether or not Regex isn't the faster solution.

    Test it for yourself then. Regex was slower

    Here's my results:


    vb.net Code:
    1. Imports System.Text.RegularExpressions
    2.  
    3. Public Class Form1
    4.     Const MAX_ELEMENTS As Integer = 1000000
    5.  
    6.     Dim rng As New Random
    7.     Dim btnLINQ, btnREGEX As Button
    8.     Dim RefCount As Integer = 0 ' For reference purposes
    9.     Dim TestArray() As String
    10.     Dim TestList As List(Of String) ' To test Arrays vs Collections issue
    11.  
    12.  
    13.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    14.         btnLINQ = New Button With {.Text = "Test LINQ", .Width = 200, .Height = 30, .Left = 10, .Top = 10}
    15.         btnREGEX = New Button With {.Text = "Test REGEX", .Width = 200, .Height = 30, .Left = 10, .Top = btnLINQ.Bottom + 20}
    16.         Me.ClientSize = New Size(btnLINQ.Right + 10, btnREGEX.Bottom + 10)
    17.         Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedDialog
    18.         Me.Controls.AddRange(New Control() {btnLINQ, btnREGEX})
    19.         Me.Text = "LINQ vs REGEX"
    20.  
    21.         PrepareTestObjects()
    22.         AddHandler btnLINQ.Click, AddressOf TestLINQ
    23.         AddHandler btnREGEX.Click, AddressOf TestREGEX
    24.     End Sub
    25.  
    26.     Private Sub PrepareTestObjects()
    27.         btnLINQ.Enabled = False
    28.         btnREGEX.Enabled = False
    29.         RefCount = 0
    30.         TestList = New List(Of String)
    31.         ReDim TestArray(MAX_ELEMENTS - 1)
    32.         Dim x, c As Integer
    33.         Randomize(Now.Ticks)
    34.         For x = 0 To MAX_ELEMENTS - 1
    35.             Dim l As Integer = rng.Next(10, 21) ' make a random length
    36.             Dim element As String = ""
    37.             For c = 0 To l
    38.                 Dim randomChar As Char = Chr(rng.Next(33, 123)) ' random character from "!" to "z"
    39.                 element += randomChar
    40.                 If randomChar = "k"c Or randomChar = "K"c Then RefCount += 1 ' We'll know for sure how many k's here
    41.             Next
    42.             TestArray(x) = element
    43.             TestList.Add(element)
    44.         Next
    45.         btnLINQ.Enabled = True
    46.         btnREGEX.Enabled = True
    47.     End Sub
    48.  
    49.     Private Sub TestLINQ(ByVal sender As Object, ByVal e As EventArgs)
    50.  
    51.         Dim KCount As Integer = 0
    52.         Dim t As DateTime = Now
    53.         For Each st In TestArray
    54.             Dim Cnt = Aggregate chars In st Where (chars = "k"c Or chars = "K"c) Into Count()
    55.             KCount += Cnt
    56.         Next
    57.         Dim ArrayTestDuration As TimeSpan = Now - t
    58.  
    59.         KCount = 0
    60.         t = Now
    61.         For Each st In TestList
    62.             Dim Cnt = Aggregate chars In st Where (chars = "k"c Or chars = "K"c) Into Count()
    63.             KCount += Cnt
    64.         Next
    65.         Dim ListTestDuration As TimeSpan = Now - t
    66.  
    67.         Dim ResultsStr As String = String.Format( _
    68.         "Test on {4} elements of random strings using LINQ:{1}Test on array:{3}{0:mm:ss.FFF} ms {1}Test on list:{3}{2:mm:ss.FFF} ms{1}Found elements:{3}{5}{1}Reference elements:{3}{6}", _
    69.         New Object() {ArrayTestDuration, Environment.NewLine, ListTestDuration, ControlChars.Tab, MAX_ELEMENTS, KCount, RefCount})
    70.         MsgBox(ResultsStr, MsgBoxStyle.Information, "Test results for LINQ")
    71.  
    72.     End Sub
    73.  
    74.     Private Sub TestREGEX(ByVal sender As Object, ByVal e As EventArgs)
    75.         Dim KCount As Integer = 0
    76.         Dim t As DateTime = Now
    77.         For Each st In TestArray
    78.             KCount += Regex.Matches(st, "[Kk]").Count
    79.         Next
    80.         Dim ArrayTestDuration As TimeSpan = Now - t
    81.         KCount = 0
    82.         t = Now
    83.         For Each st In TestList
    84.             KCount += Regex.Matches(st, "[Kk]").Count
    85.         Next
    86.         Dim ListTestDuration As TimeSpan = Now - t
    87.  
    88.         Dim ResultsStr As String = String.Format( _
    89.         "Test on {4} elements of random strings using REGEX:{1}Test on array:{3}{0:mm:ss.FFF} ms {1}Test on list:{3}{2:mm:ss.FFF} ms{1}Found elements:{3}{5}{1}Reference elements:{3}{6}", _
    90.         New Object() {ArrayTestDuration, Environment.NewLine, ListTestDuration, ControlChars.Tab, MAX_ELEMENTS, KCount, RefCount})
    91.         MsgBox(ResultsStr, MsgBoxStyle.Information, "Test results for REGEX")
    92.  
    93.     End Sub
    94. End Class

  35. #35
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: How many"k" are in my textboxes

    So there I was, going to test the results, and my machine locked up. Numerous times I have tried to test regex and had this happen. Basically my code timer calls the init code once, and then each of the tests hundreds of thousands of times. I must be missing something.

    Code:
        Dim testSTR As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        Dim myCount As Integer = 0
        Private Function TestCase1() As Boolean 'One
            'Test One Code Follows
            myCount = Aggregate chars In testSTR Where (chars = "k"c Or chars = "K"c) Into Count()
            'End Test One Code
            Return True
        End Function
        '
        Private Function TestCase2() As Boolean 'Two
            'Test Two Code Follows
            myCount = System.Text.RegularExpressions.Regex.Matches(testSTR, "[Kk]").Count
            'End Test Two Code
            Return True
        End Function
        '
        Private Sub _init()
            'Code needed to get the Test to run.  NOT part of the timing.
            testSTR &= testSTR.ToLower
            testSTR &= testSTR
            'Only called once!!!
        End Sub
    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

  36. #36
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: How many"k" are in my textboxes

    Yup, LINQ turns out to beat both Regex and the old fashioned looping through the characters. I have seen cases where LINQ was slower, but it is not in this case.

    However, I haven't used LINQ much, nor have I used the type inference that is present in the LINQ example. When I copied that into my testapp, I found that type inference simply wasn't working in that app, despite using VS2008 targeting the 3.5 framework. Is there something else that I need to set?
    My usual boring signature: Nothing

  37. #37
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: How many"k" are in my textboxes

    Are you absolutely sure that the most optimized form of looping possible couldn't beat it? (Normally I would test it, but I have VS 2005.)

    vb Code:
    1. Dim i As Integer = 0, count As Integer = 0, kL As Char = "k", kU As Char = "K"
    2. While i < str.Length
    3.      If str.Chars(i) = kL OrElse str.Chars(i) = kU Then count += 1
    4.      i += 1
    5. End While

    sort of thing? Or call ToCharArray() first? There are just too many different ways to use the simple loop.

  38. #38
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: How many"k" are in my textboxes

    Yeah, I tested that one, too. LINQ beat it handily. I didn't compare it directly to Regex, but they were in the same vicinity.
    My usual boring signature: Nothing

  39. #39
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390

    Re: How many"k" are in my textboxes

    haven't read all the posts but:

    vb Code:
    1. MsgBox((From xItem In asd Where LCase(xItem) = "k").Count)

  40. #40
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390

    Re: How many"k" are in my textboxes

    and actually linq is always faster ... except in cases where you need to evaluate the same thing multiple times... such as:

    From xItem in asd where xItem.Somefunction() = "a" or xItem.Somefunction() = "b"

    as Somefunction will be evaluated 2x where in a loop you could store this in a variable... of course u could do it like this:

    From xItem In (From xItem In asd Select xItem, xItem.Somefunction) Where xItem.Somefunction = "a" Or xItem.Somefunction = "b" Select xItem.xItem

    Thus only evaluating it in the inner linq query 1 time.

Page 1 of 2 12 LastLast

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