List(Of TKey, TValue)
を継承し、いくつかの機能を追加するクラスを作成しました。その目的は、実行時にアイテムを追加するのではなく、初期化するときです。私は実際にクラスから追加/削除メソッドを削除する(現在は継承されたクラスからそれらを公開するので)。VB .Netかなり高度な状況ですが、多くの情報が提供されています
TestApp
クラスが表示されている場合は、List(Of String, Integer)
をクラスに追加すると正しく動作します。しかし、以下のコードでは、クラスコンストラクタでMe.Add()
を使用しようとしました。それを追加するには動作しません。実際には、StringをTKeyに変換できず、IntegerをTValueに変換できないことがわかります。それは真実ではありません!テストアプリでうまく動作します。
アイデア?ここにコードがあります。 VarsクラスとTestアプリケーションの2つのクラス。
注:Me.Add(String, Integer)
が動作しないようであっても、クラス内からオーバーライドAdd
方法を使用して(同じエラー)
Imports System
Imports System.Collections.Generic
Imports System.Text
Public Class Vars(Of TKey, TValue)
Inherits List(Of KeyValuePair(Of TKey, TValue))
''' <summary>
''' Initializes the class and fills with Key->Value Pairs
''' </summary>
''' <remarks>
''' This does not work when adding directly from the class. When I overload
''' the Add() function, and call it with a KeyValuePair(Of String, String) in a
''' class instance it works, see below
''' </remarks>
Public Sub New()
Dim kv = New KeyValuePair(Of TKey, TValue)("This", 1)
Me.Add(kv)
End Sub
''' <summary>
''' Adds an item to the class by String, Integer
''' </summary>
''' <param name="aKey">A String containing the Key of the element to be added.</param>
''' <param name="aValue">An Integer containing the Value of the element to be added.</param>
''' <remarks>
''' This Works fine when called as in the demo shown below.
''' </remarks>
Public Overloads Sub Add(ByVal aKey As TKey, ByVal aValue As TValue)
Dim kv = New KeyValuePair(Of TKey, TValue)(aKey, aValue)
Me.Add(kv)
End Sub
''' <summary>
''' Finds a Value stored in the class based on a given Key.
''' </summary>
''' <param name="key">A String containing a Key to search for.</param>
''' <remarks>Works.</remarks>
Public Function FindByKey(ByVal key As TKey) As List(Of KeyValuePair(Of TKey, TValue))
Return Me.FindAll(Function(item As KeyValuePair(Of TKey, TValue)) (item.Key.Equals(key)))
End Function
''' <summary>
''' Finds a Key stored in the class based on a given Value.
''' </summary>
''' <param name="value">An Integer containing the Value to search for.</param>
''' <remarks>Works</remarks>
Public Function FindByValue(ByVal value As TValue) As List(Of KeyValuePair(Of TKey, TValue))
Return Me.FindAll(Function(item As KeyValuePair(Of TKey, TValue)) (item.Value.Equals(value)))
End Function
End Class
Public Class TestApp
Shared Sub Main()
Dim varTest As Vars(Of String, Integer) = New Vars(Of String, Integer)
varTest.Add("Kung", 1)
varTest.Add("Foo", 2)
For Each var In varTest
Console.WriteLine(var.Key & ":" & var.Value)
Next
'Output would be
'
' Kung:1
' Foo:2
End Sub
End Class
これはすばらしい答えです。私が追加する唯一のことは、新しいクラスについて話すときに、リストの周りのラッパーで、必要なものだけを公開できることです。それはリストを含み、それを再作成する作業を保存しますが、ほとんどの呼び出しをそのリストに渡します。私はこれがほぼ確実に望ましいソリューションであると考えているので、その価値について言及しています。とにかくこのコメントでは、ここにそれがない場合。 :) – Chris
偉大な答え、ちょうど私が聞く必要があった。 –