如何在VB NET中创建全局对象

发布于 2024-10-29 12:54:24 字数 1497 浏览 5 评论 0原文

我正在尝试在 VB NET 中构建语言字典,以便能够在应用程序中使用多种语言。

该字典有一个 init 方法,可以从数据库加载整个字典并将其存储在内存中。

在项目的其余类中,我添加了引用,我可以直接使用该对象而无需创建新实例,因为它们的方法是共享的。

我的问题是我必须如何加载字典内容,以便仅访问 Language.GetWord 方法的其余类获得集合的正确记录,与 My.Settings 类相同。

当从任何类使用 My.Settings 时,都会加载值。我正在寻找同样的效果。

这是该类的代码:

Public Class LanguageProvider
    Private Shared CurrentLanguage As Collection

    Public Shared ReadOnly Property GetWord(ByVal Label As String)
        Get
            Try
                Return CurrentLanguage.Item(Label)
            Catch ex As Exception
                Return Label
            End Try
        End Get
    End Property
    Public Shared Sub LoadLanguage(ByVal CultureId As Integer)
        Dim SQLController As New SqlDataAccessController
        SQLController.SQLSentence = "SELECT DNF_CULTURE_LANGUAGE_LABELS.LABEL, DNF_CULTURE_LANGUAGE_LABELS.VALUE FROM DNF_CULTURE_LANGUAGE_LABELS WHERE DNF_CULTURE_LANGUAGE_LABELS.CULTUREID = " & CultureId & " ORDER BY LABEL;"

        SQLController.OpenConnection()
        Dim dr As SqlClient.SqlDataReader
        dr = SQLController.GetData()

        CurrentLanguage = New Collection

        While dr.Read()
            CurrentLanguage.Add(dr.Item("Value"), dr.Item("Label"))
        End While

        dr.Close()
        SQLController.CloseConnection()
    End Sub
End Class

应用程序加载时必须调用 LoadLanguage 函数,以便访问数据库一次。

之后,属性 GetWord 必须访问包含单词的集合并返回结果。问题是实例不一样,并且当类使用字典时不会加载字典。

谢谢。

I'm triying to build a language dictionary in VB NET in order to be able to have several languages in the application.

This dictionary has a init method that loads from database the entire dictionary and stores it in memory.

In the rest of the classes of the project, I added the reference and I can use directly the object without create a new instance because their methods are shared.

My question is how i have to load the dictionary content in order that the rest of classes only accessing to Language.GetWord method obtains the properly record of the collection, same way as My.Settings class.

When My.Settings is used from any class the values are loaded. I'm looking for the same effect.

This is the code of the class:

Public Class LanguageProvider
    Private Shared CurrentLanguage As Collection

    Public Shared ReadOnly Property GetWord(ByVal Label As String)
        Get
            Try
                Return CurrentLanguage.Item(Label)
            Catch ex As Exception
                Return Label
            End Try
        End Get
    End Property
    Public Shared Sub LoadLanguage(ByVal CultureId As Integer)
        Dim SQLController As New SqlDataAccessController
        SQLController.SQLSentence = "SELECT DNF_CULTURE_LANGUAGE_LABELS.LABEL, DNF_CULTURE_LANGUAGE_LABELS.VALUE FROM DNF_CULTURE_LANGUAGE_LABELS WHERE DNF_CULTURE_LANGUAGE_LABELS.CULTUREID = " & CultureId & " ORDER BY LABEL;"

        SQLController.OpenConnection()
        Dim dr As SqlClient.SqlDataReader
        dr = SQLController.GetData()

        CurrentLanguage = New Collection

        While dr.Read()
            CurrentLanguage.Add(dr.Item("Value"), dr.Item("Label"))
        End While

        dr.Close()
        SQLController.CloseConnection()
    End Sub
End Class

Function LoadLanguage must be called when the application loads, in order to access once to database.

After that property GetWord must access to the collection with the words and return the result. The problem is that the instances are not the same and the dictionary is not loaded when a class uses it.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

亽野灬性zι浪 2024-11-05 12:54:24

当然

Public Shared ReadOnly Property GetWord(ByVal Label As String)
    Get
        Try
            If CurrentLanguage Is Nothing then
                  LoadLanguage(theDefaultCultureIdYouWant)
                  ' Now CurrentLanguage is not Nothing any more,
                  ' so LoadLanguage won't be called again
            end if
            Return CurrentLanguage.Item(Label)
        Catch ex As Exception
            Return Label
        End Try
    End Get
End Property

,您必须为 theDefaultCultureIdYouWant 提供一个值,这取决于您希望在用户仅调用 GetWord 而不明确提及特定区域性的情况下发生的情况。

Something along the lines of

Public Shared ReadOnly Property GetWord(ByVal Label As String)
    Get
        Try
            If CurrentLanguage Is Nothing then
                  LoadLanguage(theDefaultCultureIdYouWant)
                  ' Now CurrentLanguage is not Nothing any more,
                  ' so LoadLanguage won't be called again
            end if
            Return CurrentLanguage.Item(Label)
        Catch ex As Exception
            Return Label
        End Try
    End Get
End Property

Of course, you have to provide a value for theDefaultCultureIdYouWant, depends on what you want to happen if the user just calls GetWord without explicitly mentioning a specific culture.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文