重载决策失败,因为无法访问“项目”

时间:2018-02-10 00:34:31

标签: .net vb.net

我不知道为什么我在自定义类

中收到此错误
  

重载解析失败,因为第15行和第16行没有“项目”可访问。要查看代码,请点击链接

Imports System.Configuration

Public Class AppSettings 
  Private config As Configuration

  Public Sub AppSetting()
    config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
  End Sub

  Public Function GetConnectionString(ByVal key As String) As String
    Return config.ConnectionStrings.ConnectionStrings(key).ConnectionString
  End Function

  Public Sub SaveConnectionString(ByVal key As String, ByVal value As String)
    config.ConnectionStrings(key).ConnectionString = value
    config.ConnectionStrings(key).ProviderName = "MySql.Data.MySqlClient"
    config.Save(configurationSaveMode.Modified)
  End Sub
End Class

1 个答案:

答案 0 :(得分:0)

您在.ConnectionStrings方法中正确使用的config.ConnectionStrings(key)之间缺少GetConnectionString

config.ConnectionStrings.ConnectionStrings(key).ConnectionString = value
config.ConnectionStrings.ConnectionStrings(key).ProviderName = "MySql.Data.MySqlClient"

但你可以使用临时变量来简化

Dim connString = config.ConnectionStrings.ConnectionStrings(key)
connString.ConnectionString = value
connString.ProviderName = "MySql.Data.MySqlClient"

或使用With声明

With config.ConnectionStrings.ConnectionStrings(key)
    .ConnectionString = value
    .ProviderName = "MySql.Data.MySqlClient"
End With