1.vb

来自「Code for VB.NET教程源码 很好的源码」· VB 代码 · 共 66 行

VB
66
字号

Imports System.Collections
Imports System

Public Structure Person
    
    Dim Name As String
    Dim Age As Integer
    
End Structure


Public Module Main

	public Sub Main()

		dim dictionary as Hashtable
		dim p as Person

		' Create a Hastable - this is a reference type, so we use New
		' to create the object on the managed CLR heap.

		dictionary = new Hashtable()

		' Setup our Person Structure - This is value type, so we do
		' not use New, and the type is just allocated on the stack.


		p.Name = "Richard Anderson"
		p.Age = 29

		' Add our Person value type to the dictionary.  This will cause
		' the value type to be boxed within a reference type, and added
		' to the dictionary.

		dictionary.Add( "Rich", p )

		' Setup the value type (overwriting the old values)

		p.Name = "Sam Anderson"
		p.Age = 28
		
		' Add our Person value type to the dictionary.  Again, this will cause
		' the value type to be boxed within a reference type and adde to the
		' dictionary.

		dictionary.Add( "Sam", p )

		' Retreive a person by Name.  The CType keyword tells VB that
		' the object type returned by the Item function is a Person
		' type.  Since the Person type is a value type, the CLR knows
		' to unbox the Person type from the returned object type.

		p = CType( dictionary.Item("Rich"), Person )

		' We use the Person type

		Console.WriteLine("Name is {0} and Age is {1}", _
			p.Name, p.Age )
		
		
	End Sub

End Module

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?