How can i do a for loop with vb.net'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("") Next
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 vb.net 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("") Next
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 - ForNext Loop - It repeats a group of statements a specified number of times and a loop index counts the number of loop iterations as the loop executes.
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("") Next
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 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 VB For index As Integer = 1 To 100000 ' If index is between 5 and 7, continue ' with the next iteration. If index >= 5 AndAlso index <= 8 Then Continue For End If ' Display the index. Debug.Write (index.ToString & " ") ' If index is 10, exit the loop.
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 Do While Loop Examples (While), 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 How to use FOR NEXT loop in vb.net. Whenever you face a situation in programming to repeat a task for several times (more than one times ) or you have to repeat a task till you reach a condtition, in these situations you can use loop statements to achieve your desired results. This kind of for loop is useful for iterating over arrays and for other applications in which you know in advance how many times you want the loop to iterate.
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 Next
VB.NET For Loop Examples (For Each), 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 The for loop syntax is the following: For variable As Integer = initialValue To finalValue [ Step 1 ] 'Some commands Next. variable is the control variable which is set to an initial value (usually 0, because in programming, everything starts from zero, never from one). For example: i As Integer = 0.
Visual Basic Do Loops, 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 If you have nested loops of different types, for example a Do loop within a For loop, you can skip to the next iteration of either loop by using either Continue Do or Continue For. Example The following code example uses the Continue While statement to skip to the next column of an array if a divisor is zero.
Visual Basic .NET Language, VB For Loop – For…Next Statement in Visual Basic.NET 2008 · Control Flow Statements in Visual Basic 2008. Chapter: Programming Fundamentals. 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 any time with the E
VB While Loop - Do While Loop, Do Until Loop , FOR NEXT Loop, FOR EACH Loop , WHILE Loop and DO WHILE Loop are the Commonly used loops in Visual Basic.NET. 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.
- 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.