How can i do a for loop with's my.settings name and value?

i would like some help with this, i have tried searching on google and have tried searching here with no luck.

what i have tried so far is the following:-

For Each value As Configuration.SettingsPropertyValue In My.Settings.PropertyValues

    Dim myrecord As String = My.Settings.PropertyValues.ToString

    sql_all_matching_records = String.Format("")


but this just gets skipped when i run in debugger mode and have put a break point within the for loop.

what i want to do is be able to loop through the variable names in my.settings and then take its value and compare it to an SQL lookup. the only thing i am struggling with is looping through the my.settings.

EDIT1: ABOVE has been answered, however getting the error in picture, have a wrote the code wrong? not sure i understand what is trying to tell me. i thought it was correct.

A bit of a disclaimer, I'm not familiar with how/when settings are loaded and the PropertyValues collection gets filled, but it doesn't seem to hold anything until a settings value is accessed. It's a wierd workaround, but grabbing a value from settings before looping through the collection seems to fill it. Your InvalidCast exception occurs because you're trying to set myrecord = a collection.tostring. We already know that the collection has multiple values because we're iterating them, so we can't just call .tostring on it. That's like saying Array.ToString(), just doesn't work that way. I think what you're actually looking for is value.PropertyValue.ToString, that will hold the settings value you're trying to get. With both those changes applied, you get this:

    Dim dummy As String = My.Settings.dummySetting
    For Each value As System.Configuration.SettingsPropertyValue In My.Settings.PropertyValues

        'Dim myrecord As String = My.Settings.PropertyValues.ToString
        Dim myrecord As String = value.PropertyValue.ToString

        sql_all_matching_records = String.Format("")

The dummySetting is just an empty value that I put in settings to call on. Hope this helps

ForNext Statement, VB.Net - Do Loop - It repeats the enclosed block of statements while a Boolean condition is True or until the condition becomes True. It could be terminated at  VB.NET program that uses For Step Module Module1 Sub Main () ' This loop uses Step to go down 2 each iteration. For value As Integer = 10 To 0 Step -2 Console.WriteLine (value) Next End Sub End Module Output 10 8 6 4 2 0. Nested For. In many programs, nested loops are essential.

I think your line #2 is wrong...

For Each value As Configuration.SettingsPropertyValue In My.Settings.PropertyValues

    Dim myrecord As String = value.ToString

    sql_all_matching_records = String.Format("")


I have not tried your code but I think what You are doing in your line 3 is taking all of the properties of each setting and concatenating them into one.

Edited to point at correct line #2 not #3

VB.Net - Do Loop, VB.Net provides following types of loops to handle looping requirements. Click the following links to check their details. Loop Type, Description. Do Loop. It repeats  A loop statement allows us to execute a statement or group of statements multiple times and following is the general form of a loop statement in most of the programming languages −. VB.Net provides following types of loops to handle looping requirements. Click the following links to check their details.

Some storage areas do not know where they begin or end! When you send the cursor to the top, it travels upward until it runs out of records and stops with the first record just below the cursor. Your first command must be down to reach the first record.

VB.Net - Loops, The two bounds are inclusive (0 and 5 will be reached). Step 2: We see the Exit For statement—this statement breaks out of the enclosing For-loop. Exit works in all  finalValue specifies how many times the loop will be executed (more precisely to what value the loop should count to). The commands in the loop are actually executed when the value of the control variable is <= finalValue. The finalValue is typically integer. [Step] - The Step keyword is optional.

If looking for a case-insensitive lookup, you may do something like this:

        For Each AProperty As Configuration.SettingsPropertyValue In My.Settings.PropertyValues
            If AProperty.Name.ToLower = ("ExPectedName").ToLower Then
                ReturnValue = AProperty.PropertyValue
                Exit For
            End If

VB.NET For Loop Examples (For Each), VB.NET Do While Loop Examples (While). Use the Do While and While loops. Keep looping as long as a condition remains True. While, Do While. Suppose we​  In VB.NET we can use a Do Until loop to mean "continue until the condition is matched." A Do While loop can be used in the same way. Here: We use the "not equals" operator with a Do While loop to continue until an array element is equal to 30. And: We show a Do Until loop that has the same effect as the Do While loop. Use whichever is clearest

VB.NET Do While Loop Examples (While), FOR NEXT Loop, FOR EACH Loop , WHILE Loop and DO WHILE Loop are the Commonly used loops in Visual Basic.NET. The amount by which counter is incremented each time through the loop. statements: Optional. One or more statements between For and Next that run the specified number of times. Continue For: Optional. Transfers control to the next loop iteration. Exit For: Optional. Transfers control out of the For loop. Next: Required. Terminates the definition of the For loop.

How to use FOR NEXT loop in, Use DoLoop to repeat a block of statements While or Until a condition is true, checking the condition either at the beginning or at the end of the loop. Dim x As​  Loop construction allows you to test a condition at either the beginning or the end of a loop structure. You can also specify whether to repeat the loop while the condition remains True or until it becomes True. For more information, see DoLoop Statement. For Loops. The ForNext construction performs the loop a set number of times. It uses

Visual Basic .NET/Loop statements, There are two Do Loops in Visual Basic: the Do While and Do Until. The Do While loops something which is true, and the Do Until loops until a certain condition  Separate function: Put the loops inside a separate function, which can be exited with return. This might require you to pass a lot of parameters, though, depending on how many local variables you use inside the loop. An alternative would be to put the block into a multi-line lambda, since this will create a closure over the local variables.

  • I was able to reproduce your problem, weird solution but if you access a value from your settings before the loop (i.e. Dim dummyString As String = My.Settings.aSettingName) then it seems to work. This feels like a situation where the settings haven't been loaded yet, but I must admit I'm not too familiar with how/when settings are initialized. But adding that line in front of the loop and grabbing the value of a "dummy" setting seemed to fill the PropertyValues collection and allow to the loop to execute
  • wow - good answer - it worked but now it has given me a new problem, i will edit question, then post the above as answer as it is right and will put as answer.
  • Hi, as said in the answers it doesn't even get to line 2 in the debugger, i put a break on line 2 but never breaks just skips the whole loop.