asp.net - 如果值在 if 语句之外不存在,如何添加到字典中?
问题描述
如果值在 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
解决方案
推荐阅读
- c++ - 使用 cmake 链接到自定义框架 - 构建时位置正确,运行时位置错误
- android-fragments - Android Studio JetPack 3.2 Preview Beta 5 中只有“Fragment + View Model”
- java - 全局配置 ObjectMapper(通过 @Bean 或 smth)以从“数据”节点解析
- android - 我如何在 android studio 中使用 BottomNavigationViewEx 更改或添加图标后面的形状
- debugging - 无法为 fortran 运行调试器。视觉工作室代码
- flexdashboard - Flexdashboard CSS 不正确
- python - 错误“builtin_function_or_method”对象不可下标-在for循环中附加列表时
- node.js - Electron - 日志记录在哪里?
- vb.net - 应用程序不会显示气球提示
- ruby - PATH 中的 GeckoDriver,但出现 Selenium Webdriver 错误