문제

I would like to be able to take a string such as the following and put in in MySettings and have the application dynamically parse the expression. Is that possible?


Name: ClientName
Type: String
Scope: Application
Value: MyData(i).FirstName & " " & MyData(i).LastName


Dim name As String = My.Settings.ClientName

도움이 되었습니까?

해결책

It's not really possible to do if using just the simple String class. You'll either have to create a method to parse it out or create your own object type that will do the parsing. Either way, it requires that you write code to parse out that data.

If you need some help with parsing of the data, using your above example, it's simple enough to parse.

Dim s As String = "First Last"
Dim names() As String = s.Split(" "c)

Dim firstName As String = names(0)
Dim lastName As String = names(1)

I'm also not sure what your MyData object is. You could put a parsing routine in that object to automatically parse the name and set the properties as needed. It could even be part of the constructor.

Public Sub DoWhateverINeedToDo()
    Dim md As New MyData(My.Settings.ClientName)

    Console.WriteLine("First Name: " & md.FirstName)
    Console.WriteLine("Last Name: " & md.LastName)
End Sub

Public Class MyData

    Public Property FirstName As String
    Public Property LastName As String

    Public Sub New(ByVal fullName As String)
        Dim names() As String = fullName.Split(" "c)

        Me.FirstName = names(0)
        Me.LastName = names(1)
    End Sub

End Class
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top