for/next loop within a fuction
for/next loop within a fuction
for/next loop within a fuction
for/next loop within a fuction
for/next loop within a fuction
for/next loop within a fuction for/next loop within a fuction for/next loop within a fuction for/next loop within a fuction for/next loop within a fuction for/next loop within a fuction for/next loop within a fuction for/next loop within a fuction
for/next loop within a fuction for/next loop within a fuction
for/next loop within a fuction
Go Back  Xtreme Visual Basic Talk > > > for/next loop within a fuction


Reply
 
Thread Tools Display Modes
  #1  
Old 10-06-2014, 11:01 AM
Amerigo's Avatar
Amerigo Amerigo is offline
Centurion
 
Join Date: Jan 2009
Location: USA
Posts: 175
Question for/next loop within a fuction


I have this:
Code:
    Private Function AddExisting(ByVal Entry As String) As Boolean
        For Each item As ListViewItem In ListView1.Items
            If item.Text.ToLower = Entry.ToLower Then
                Dim result As Integer = MessageBox.Show("Do you really want to duplitate the entry: " & Entry, "Duplicate?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
                If result = DialogResult.Yes Then
                    Return True
                Else
                    Return False
                End If
            Else
                Return True
            End If
        Next
    End Function
but it's not looping through each item in the list to check for a match. Only the first entry is checked.
I also have a warning that the above function doesn't return a value on all code paths.
__________________
Amerigo
Reply With Quote
  #2  
Old 10-06-2014, 11:05 AM
PlausiblyDamp's Avatar
PlausiblyDampfor/next loop within a fuction PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

You are returning true or false from every check, including the first one, regardless of if it is a match or not. You don't want the final return true to be part of the loop.

Code:
If item.Text.ToLower = Entry.ToLower Then
    Dim result As Integer = MessageBox.Show("Do you really want to duplitate the entry: " & Entry, "Duplicate?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
    If result = DialogResult.Yes Then
        Return True
    Else
        Return False
    End If
Else
    Return True  'this will cause the function to exit if the entries don't match.
End If
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #3  
Old 10-06-2014, 11:25 AM
Amerigo's Avatar
Amerigo Amerigo is offline
Centurion
 
Join Date: Jan 2009
Location: USA
Posts: 175
Default

Thank you, Great Lactating One.
This is working:
Code:
    Private Function AddExisting(ByVal Entry As String) As Boolean
        For Each item As ListViewItem In ListView1.Items
            If item.Text.ToLower = Entry.ToLower Then
                Dim result As Integer = MessageBox.Show("Do you really want to duplitate the entry: " & Entry, "Duplicate?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
                If result = DialogResult.Yes Then
                    Return True
                Else
                    Return False
                End If
            End If
        Next
        Return True
    End Function
__________________
Amerigo
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
for/next loop within a fuction
for/next loop within a fuction
for/next loop within a fuction for/next loop within a fuction
for/next loop within a fuction
for/next loop within a fuction
for/next loop within a fuction for/next loop within a fuction for/next loop within a fuction for/next loop within a fuction for/next loop within a fuction for/next loop within a fuction for/next loop within a fuction
for/next loop within a fuction
for/next loop within a fuction
 
for/next loop within a fuction
for/next loop within a fuction
 
-->