如何在实例化时将值插入VB.NET词典? [英] How to insert values into VB.NET Dictionary on instantiation?

查看:121
本文介绍了如何在实例化时将值插入VB.NET词典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在创建VB.NET词典时将其插入值?我可以但不想对每个项目执行dict.Add(int, string)。

Is there a way that I can insert values into a VB.NET Dictionary when I create it? I can, but don't want to, do dict.Add(int, "string") for each item.

基本上,我想做如何在实例化时将值插入C#词典? 与VB.NET。

Basically, I want to do "How to insert values into C# Dictionary on instantiation?" with VB.NET.

var dictionary = new Dictionary<int, string>
    {
        {0, "string"},
        {1, "string2"},
        {2, "string3"}
    };


推荐答案

如果使用Visual Studio 2010或更高版本,则应使用 FROM 关键字像这样:

If using Visual Studio 2010 or later you should use the FROM keyword like this:

Dim days = New Dictionary(Of Integer, String) From {{0, "string"}, {1, "string2"}}

请参阅: http://msdn.microsoft.com/zh- us / library / dd293617(VS.100).aspx

如果您需要使用Visual Studio的早期版本,并且需要经常执行此操作,

If you need to use a prior version of Visual Studio and you need to do this frequently you could just inherit from the Dictionary class and implement it yourself.

它可能看起来像这样:

Public Class InitializableDictionary
    Inherits Dictionary(Of Int32, String)

    Public Sub New(ByVal args() As KeyValuePair(Of Int32, String))
        MyBase.New()
        For Each kvp As KeyValuePair(Of Int32, String) In args
            Me.Add(kvp.Key, kvp.Value)
        Next
    End Sub

End Class

这篇关于如何在实例化时将值插入VB.NET词典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆