ArrayList强类型 [英] ArrayList Strongly Typed
问题描述
我使用以下代码作为强类型的arraylist并且效果很好。
但是,我想知道这是否是正确的方法。我意识到
如果我想实现arraylist的排序,那么我必须用一个使用comparer的排序方法处理这个
。我可以直接引用
Arraylist的属性,例如
dim mylist作为新的FrameList
mylist.Add(new FrameStructure )
mylist(0).first =" blabla ..."
mylist(0).second =" bla2 bla2 ..."
''强类型arraylist类'FrameList"
朋友类FrameList
继承ArrayList
默认Public Shadows Property Item(ByVal Index As Integer)As
FrameStructure
获取
返回DirectCast(MyBase.Item(Index),FrameStructure)
结束获取
设置(ByVal值为FrameStructure)
MyBase.Item(Index)= Value
结束集
结束物业
结束班级
公共类FrameStructure
公开第一作为字符串
公共第二作为字符串
结束班
-
休斯顿的丹尼斯
I use the following code for a strongly typed arraylist and it works great.
However, I was wondering if this is the proper way to do it. I realize that
if I want to implement sorting of the arraylist then I have to handle this
with a sort method that uses comparer. I can reference the properties of the
Arraylist directly such as
dim mylist as new FrameList
mylist.Add(new FrameStructure)
mylist(0).first = "blabla..."
mylist(0).second = "bla2 bla2..."
''Strongly typed arraylist class "FrameList"
Friend Class FrameList
Inherits ArrayList
Default Public Shadows Property Item(ByVal Index As Integer) As
FrameStructure
Get
Return DirectCast(MyBase.Item(Index), FrameStructure)
End Get
Set(ByVal Value As FrameStructure)
MyBase.Item(Index) = Value
End Set
End Property
End Class
public class FrameStructure
public first as string
public second as string
end class
--
Dennis in Houston
推荐答案
嗨Dennis,
我是一个.Net新手,所以让我问一下......为什么要在FrameList类中创建
?这不行吗
公共类FrameStructure
公开第一作为字符串
公共第二作为字符串
结束班级
将myList作为新阵列列表调暗
将myFrameStructure作为新帧结构调暗
myFrameStructure.first =" blablabla"
myFrameStructure.second =" bla2bla2bla2"
myList.Add(myFrameStructure)
使用它:
myFrameStructure = ctype(myList(0),FrameStructure)
with myFrameStructure
messagebox.show(.first& vbcrlf& .second)
结束
我错过了一些关于强类型的信息或什么?
John
2005年3月24日星期四16:57:02 -0800,丹尼斯
< De **** @ discussion.microsoft.com>写道:
Hi Dennis,
I am a .Net newbie so let me ask...why go to the trouble of creating
the FrameList class? Wouldn''t this work
Public Class FrameStructure
Public first as string
Public second as string
End Class
Dim myList As New ArrayList
Dim myFrameStructure As New FrameStructure
myFrameStructure.first = "blablabla"
myFrameStructure.second = "bla2bla2bla2"
myList.Add(myFrameStructure)
To use it:
myFrameStructure = ctype(myList(0), FrameStructure)
with myFrameStructure
messagebox.show(.first & vbcrlf & .second)
end with
Am I missing something about strong typing or what?
John
On Thu, 24 Mar 2005 16:57:02 -0800, Dennis
<De****@discussions.microsoft.com> wrote:
我使用以下代码作为强类型的arraylist并且效果很好。
但是,我想知道这是否是正确的方法。我意识到如果我想实现arraylist的排序,那么我必须使用使用comparer的sort方法处理这个
。我可以直接引用
Arraylist的属性,比如
dim mylist作为新的FrameList
mylist.Add(new FrameStructure)
mylist(0).first = blabla ...
mylist(0).second =" bla2 bla2 ..."
''强类型的arraylist类'FrameList"
朋友类FrameList
继承ArrayList
默认公共阴影属性项(ByVal索引为整数)为
FrameStructure
获取返回DirectCast(MyBase.Item(Index),FrameStructure )
结束获取
设置(ByVal值为FrameStructure)
MyBase.Item(Index)= Value
End Set
End Property
End Class
public class FrameStructure
public first as string
public second as string
end class
I use the following code for a strongly typed arraylist and it works great.
However, I was wondering if this is the proper way to do it. I realize that
if I want to implement sorting of the arraylist then I have to handle this
with a sort method that uses comparer. I can reference the properties of the
Arraylist directly such as
dim mylist as new FrameList
mylist.Add(new FrameStructure)
mylist(0).first = "blabla..."
mylist(0).second = "bla2 bla2..."
''Strongly typed arraylist class "FrameList"
Friend Class FrameList
Inherits ArrayList
Default Public Shadows Property Item(ByVal Index As Integer) As
FrameStructure
Get
Return DirectCast(MyBase.Item(Index), FrameStructure)
End Get
Set(ByVal Value As FrameStructure)
MyBase.Item(Index) = Value
End Set
End Property
End Class
public class FrameStructure
public first as string
public second as string
end class
丹尼斯,
为什么你继承了Arraylist而不是直接来自收藏库
http:// msdn。 microsoft.com/library/de...classtopic.asp
该页面中包含一个很好的样本
In我的意见是更简单,更一致。
我希望这有帮助吗?
Cor
Dennis,
Why you inherit from Arraylist and not direct from the collectionbase
http://msdn.microsoft.com/library/de...classtopic.asp
A nice sample is included in that page
In my opinion is that simpler and more consistent.
I hope this helps?
Cor
>
2005-03-25,JL< jo ** @ marymonte.com>写道:
On 2005-03-25, J L <jo**@marymonte.com> wrote:
嗨丹尼斯,
我是.Net新手所以让我问...为什么要去创建FrameList类的麻烦?这不行吗
它会起作用,但是......
myFrameStructure = ctype(myList(0),FrameStructure)
with myFrameStructure
messagebox.show(.first& vbcrlf& .second)
结束
我是否遗漏了强类型或什么?
是的,你是。这一行
myFrameStructure = ctype(myList(0),FrameStructure)
Hi Dennis,
I am a .Net newbie so let me ask...why go to the trouble of creating
the FrameList class? Wouldn''t this work
It would work, but...
myFrameStructure = ctype(myList(0), FrameStructure)
with myFrameStructure
messagebox.show(.first & vbcrlf & .second)
end with
Am I missing something about strong typing or what?
Yes, you are. The line
myFrameStructure = ctype(myList(0), FrameStructure)
并不是强类型的。如果你意外地向myList添加了除了
FrameStructure之外的东西,那么直到运行时
CType失败才会知道它。强类型列表会阻止我首先在
中执行此操作。
将运行时错误转换为编译时错误是强类型
就是这样。
isn''t strongly typed. If you accidently add something other than a
FrameStructure to myList, you won''t know about it until runtime when the
CType fails. A strongly-typed list would prevent me from doing that in
the first place.
Converting runtime errors into compile-time errors is what strong typing
is all about.
这篇关于ArrayList强类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!