我很尷尬以分享此功能,但我需要幫助它整齊。我很久以前寫了這個非常簡單的使用方法,但它已經失控,我不知道如何正確處理它。處理大量的可選參數
Public Shared Function SetVariables(msg As String, Optional name As String = "", Optional target As String = "", Optional amount As Decimal = 0, Optional cost As String = "0", Optional keyword As String = "", Optional time As String = "", Optional reward As String = "", Optional participantList As String = "", Optional participantCount As Integer = 0, Optional game As String = "", Optional viewers As String = "", Optional followers As String = "", Optional link As String = "", Optional _options As String = "", Optional Year As String = "", Optional Month As String = "", Optional Day As String = "", Optional Hour As String = "", Optional Minute As String = "", Optional grpname As String = "")
Dim balance As Decimal
Dim holdings As Decimal
If name > "" Then
If Options.Accounts.ContainsKey(name) Then
If Options.Holdings.ContainsKey(Options.Accounts.Item(name)) Then
holdings = Options.Holdings.Item(Options.Accounts.Item(name))
End If
balance = Options.Accounts.Item(name).Points
End If
End If
msg = msg.Replace("[name]", StrConv(name, VbStrConv.ProperCase))
If holdings > 0 Then
msg = msg.Replace("[balance]", balance & "[" & holdings & "]")
Else
msg = msg.Replace("[balance]", balance)
End If
msg = msg.Replace("[channel]", Subs.UppercaseFirstLetter(Options.Channel.TrimStart("#")))
msg = msg.Replace("[target]", Subs.UppercaseFirstLetter(target))
msg = msg.Replace("[amount]", amount)
msg = msg.Replace("[cost]", cost)
msg = msg.Replace("[keyword]", keyword)
msg = msg.Replace("[time]", time)
msg = msg.Replace("[reward]", reward)
msg = msg.Replace("[participantList]", participantList)
msg = msg.Replace("[participantCount]", participantCount)
msg = msg.Replace("[botname]", Subs.UppercaseFirstLetter(Options.User))
If msg.Contains("[groups]") Then msg = msg.Replace("[groups]", GetSortedGroups(name))
If msg.Contains("[group]") Then msg = msg.Replace("[group]", GetSortedGroups(name, True))
msg = msg.Replace("[game]", StrConv(game, VbStrConv.ProperCase))
msg = msg.Replace("[viewers]", viewers)
msg = msg.Replace("[followers]", followers)
msg = msg.Replace("[link]", link)
msg = msg.Replace("[options]", options.ToUpper)
msg = msg.Replace("[years]", Year)
msg = msg.Replace("[months]", Month)
msg = msg.Replace("[days]", Day)
msg = msg.Replace("[hours]", Hour)
msg = msg.Replace("[minutes]", Minute)
msg = msg.Replace("[grpname]", StrConv(grpname, VbStrConv.ProperCase))
If balance = 1 Or amount = 1 Then
msg = msg.Replace("[currency]", Options.PName)
Else
msg = msg.Replace("[currency]", Options.PNames)
End If
Return msg
End Function
基本上我把一個字符串傳遞給這個函數,它包含了一些這樣的:[name] [keyword]等,用其他的替換。有時候我也必須傳遞數據來替換這些數據,這就是問題出現的地方。我想要這些功能,但我現在有很多參數。我從不在函數的單個調用中使用所有這些參數,隨着時間的推移,我將增加更多的參數。
有關如何更好地處理這樣的事情的任何意見?我是否應該將這個功能拆開,並根據個人情況處理替換件?
由這些參數組成的課程應使其更簡潔 – Plutonix
您可能是對的。我實際上想過這樣做,並忘記了這個想法。謝謝! – Josh
@Plutonix使用我在OP中共享的內容,你能夠寫出一個粗略的例子來說明你將如何構建這個類?它仍然覺得我在使用這個課程時會有很長的字符串,但也許我看錯了。 – Josh