个人资料

跳过导航链接首页 > 博客列表 > 博客正文

VB.Net使用泛型,定义泛型方法和调用

分类:

Vb.Net定义泛型方法并调用,示例代码为UWP存储设置的VB实现方式.

定义(和C#相比多了一个of 关键字)

Imports Windows.Storage


Public Class AppSetting


   '写入存储信息

   Public Shared Sub WriteSetting(Of T)(ByVal key As String, ByVal Tvalue As T)

       Dim root As ApplicationDataContainer = ApplicationData.Current.LocalSettings

       Dim oldkey As Object = Nothing

       If root.Values.TryGetValue(key, oldkey) Then


           root.Values(key) = Tvalue

       Else

           root.Values.Add(key, Tvalue)

       End If

   End Sub


   '读取存储信息

   Public Shared Function ReadSetting(Of T)(ByVal key As String) As T

       Dim root As ApplicationDataContainer = ApplicationData.Current.LocalSettings


       Dim value As Object

       If root.Values.TryGetValue(key, value) Then

           Return CType(value, T)

       Else

           Return Nothing

       End If

   End Function


   '有条件的写入存储信息

   Public Shared Sub WriteSettingIfBigger(Of T)(ByVal key As String, ByVal Tvalue As T)

       Dim root As ApplicationDataContainer = ApplicationData.Current.LocalSettings

       Dim oldvalue As Object = Nothing

       If root.Values.TryGetValue(key, oldvalue) Then

           If Convert.ToDouble(oldvalue) < Convert.ToDouble(Tvalue) Then

               root.Values(key) = Tvalue

           End If

       Else

           root.Values.Add(key, Tvalue)

       End If

   End Sub

End Class



调用

AppSetting.WriteSetting(Of Double)("Music Volume", e.NewValue)

menu_background_music.Volume = AppSetting.ReadSetting(Of Double)("Music Volume")


admin
最初发表2019/1/22 23:40:38 最近更新2019/1/23 0:05:20 7217
为此篇作品打分
10
   评论