首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

泛型相关,该怎么处理

2012-01-31 
泛型相关C#publicclassMyList T:IEnumerable T{//ImplementGetEnumeratortoreturnIEnumerator Ttoen

泛型相关
C#
public   class   MyList <T>   :   IEnumerable <T>
{
          //   Implement   GetEnumerator   to   return   IEnumerator <T>   to   enable
          //   foreach   iteration   of   our   list.   Note   that   in   C#   2.0  
          //   you   are   not   required   to   implement   Current   and   MoveNext.
          //   The   compiler   will   create   a   class   that   implements   IEnumerator <T> .
          public   IEnumerator <T>   GetEnumerator()
          {
                    //DoSomething1;
          }

          //   We   must   implement   this   method   because  
          //   IEnumerable <T>   inherits   IEnumerable
          System.Collections.IEnumerator   System.Collections.IEnumerable.GetEnumerator()
          {
//DoSomething2;
          }
}

VB
Public   Class   MyList
          Implements   IEnumerable(Of   String)

          '   Implement   GetEnumerator   to   return   IEnumerator <T>   to   enable
          '   foreach   iteration   of   our   list.   Note   that   in   C#   2.0  
          '   you   are   not   required   to   implement   Current   and   MoveNext.
          '   The   compiler   will   create   a   class   that   implements   IEnumerator <T> .
          Public   Function   GetEnumerator()   As   IEnumerator(Of   String)   Implements   IEnumerable(Of   String).GetEnumerator
'DoSomething1
          End   Function

          '   We   must   implement   this   method   because  
          '   IEnumerable <T>   inherits   IEnumerable
          Function   GetEnumerator()   As   IEnumerator   Implements   IEnumerable.GetEnumerator
'DoSomething2
          End   Function

End   Class

C#的代码可以正常编译通过,VB的就不行。
VB的错误:只有返回值类型不同,不能overload。
请帮忙看看是哪里错了。

[解决办法]
overload的重要特点是函数名相同,参数不同(顺序不同),但不能依靠返回值类型不同来重载函数
[解决办法]
泛型 是单个函数 参数的多个不同类型的问题

函数重载 是解决 函数名 相同 但是对于不同的操作 有不同个数的参数
[解决办法]
一样的
C# 的隐式实现接口,在vb中就用private
[解决办法]
我以前写过一个(现在看着有点不舒服,但也不想改了):
Public Class SinceLinkItemCollection(Of T_ID_DataType, T As SinceLinkItemBase(Of T_ID_DataType))


Inherits System.Collections.ObjectModel.Collection(Of T)

<NonSerialized()> _
Private gNode As Node(Of T)

Private gCodeFormat As String
Private gFileName As String = LzmTW.Global.EntryAssemblyInfo.GetApplicationDataCompanyTitleVersionPath & "{0}.{1}s.dat " ' AppDomain.CurrentDomain.BaseDirectory & "{0}.{1}s.dat "

Public ReadOnly Property FileName() As String
Get
Return gFileName
End Get
End Property

Sub New()
gFileName = String.Format(gFileName, System.Reflection.Assembly.GetEntryAssembly.ManifestModule.Name, GetType(T).Name)
End Sub

' ' ' <param name= "codeFormat "> 形如“00,000,0000” </param>
Sub New(ByVal codeFormat As String)
gCodeFormat = codeFormat
gFileName = String.Format(gFileName, System.Reflection.Assembly.GetEntryAssembly.ManifestModule.Name, GetType(T).Name)
End Sub

Public ReadOnly Property Node() As Node(Of T)
Get
If gNode Is Nothing Then
Me.RefleshNode()
End If
Return gNode
End Get
End Property

Public Shadows Function Add(ByVal code As String, ByVal name As String) As T
Dim mItem As T = CType(System.Activator.CreateInstance(GetType(T), New Object() {code, name}), T)
Me.Add(mItem)

Return mItem
End Function

Public Shadows Sub Add(ByVal items As T())
For Each item As T In items
Add(item)
Next
End Sub

Public Shadows Function Add(ByVal item As T) As T
item.UpdateInformations(gCodeFormat)

MyBase.Add(item)

Return item
End Function
[解决办法]
学习

热点排行