简体   繁体   中英

How to use a string in My.Settings VB.net

For Each i In web
    web(i) = My.Settings.sweb(i)
    name(i) = My.Settings.sname(i)
Next

This code doesn't work, it is what I want to do. How do I make it so that I can change the name in my.settings without hard coding it? Basically I want to be able to change the name of the input to my.settings

How can I make the "sweb" part of My.Setting a variable so I can change it.

Use

For Each i In web
    web(i) = My.Settings("sweb" & i)
    name(i) = My.Settings("sname" & i)
Next

It looks like you are wanting to access your system settings like an array. If you are wanting to iterate through it I would suggest something like;

 Dim myArray() As String = {My.Settings.sweb1, My.Settings.sweb2, My.Settings.sweb3}

Then you can do:

For Each i in web
      web(i) = myArray(i)
      ...
Next

Couldn't you make all of your settings into an array of strings My.Settings.swed(10) so you could step through them like you propose.

I'm not sure the For Each loop is correct.

 Dim My.Settings.swed(10) As String

 For i = My.Settings.swed.LBound To My.Settings.swed.UBound
    web(i) = My.Settings.swed(i)
 Next 

When you have a For Each it's usually over each member of a collection

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM