如果值在if语句之外不存在,如何添加到字典中?

时间:2019-04-17 21:44:52

标签: asp.net vb.net dictionary

如果if语句之外的值不存在,如何添加到字典中?

我尝试将变量设为公开:

Public dictAdd As Dictionary(Of String, Integer) = New Dictionary(Of String, Integer)

我尝试为其创建函数:

Public Sub dictEdit(serviceName As String, serviceValue As Integer)
    dictAdd.Add(serviceName, serviceValue)
End Sub

添加:

'dictAdd.Add(serviceName, serviceValue)
 dictEdit(serviceName, serviceValue)

无论我做什么,我添加到字典中的任何内容都不在范围之外(如if语句)。我需要在整个代码中添加和删除值。

编辑:

这是我的代码:

Partial Class openapps_myapp
Inherits System.Web.UI.Page

Public dictAdd As Dictionary(Of String, Integer) = New Dictionary(Of String, Integer)

Public Sub dictEdit(serviceName As String, serviceValue As Integer)
    dictAdd.Add(serviceName, serviceValue)
End Sub

'Add CBLAvailable item to CBLModifications
 Protected Sub BtnAdd_Click(sender As Object, e As EventArgs) Handles BtnAdd.Click

    'Some code here that gets serviceName and serviceValue from a checkboxlist

    'Add each selected item from CBLAvailable to dictAdd
    'dictAdd.Add(serviceName, serviceValue)
    dictEdit(serviceName, serviceValue)

End Sub

Protected Sub BtnEdit_Click(sender As Object, e As EventArgs) Handles BtnEdit.Click

    'Some code here that defines replaceString

If dictAdd.ContainsKey(replaceString) Then
                CBLModifications.Items.Remove(CBLModifications.Items(i))
               'right here is my error - dictAdd doesn't have any values - it's empty
            End If

End Sub

'More irrelevant code here

0 个答案:

没有答案