| 网站首页 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛 |
 
| 技术教程首页 | 开发语言 | WEB开发 | .NET技术 | 数据库 | 操作系统 | 网页制作 |
 
 
您现在的位置: 编程中国 >> 技术教程 >> .NET技术 >> VB.NET >> VB.NET教程 >> 正文
  ►  VB.NET 入门教程
VB.NET 入门教程
作者:未知    阅读人次:……    文章来源:microsoft    发布时间:2004-9-27    网友评论()条
 

步骤 4. 控制台输入

现在,我们将继续编写 QuickSort 应用程序。我们需要做的第一件事就是提示用户提供输入和输出文件。

修改源代码

更改 Visual Basic 源文件 (module1.vb),如下面以斜体突出显示的代码所示。其他的差异(如类名)可忽略不计。

' Import namespaces
Imports System
Module Module1
    Sub Main()
        ' Describe program function
        Console.WriteLine ("QuickSort VISUAL BASIC .NET Sample Application")
        Console.WriteLine ()
        ' Prompt user for filenames
        Dim szSrcFile, szDestFile As String
        Console.Write("Source: ")
        szSrcFile = Console.ReadLine()
        Console.Write("Output: ")
        szDestFile = Console.ReadLine()
    End Sub
End Module

从控制台进行读取

Console 类的 ReadLine() 方法提示用户输入,并返回输入的字符串。它会自动地为字符串处理内存分配,由于使用了 .NET 垃圾回收器,您不需要做任何释放内存的工作。

程序输出

从菜单中选择 Debug | Start Without Debugging 来运行程序。这是到此为止来自 QuickSort 应用程序的输出的屏幕截图。

 


步骤 5. 使用数组

在对从输入读取的行进行排序之前,程序需要将其存储到一个数组中。我们将简要讨论可实现对象数组的 .NET 基类的用法。

修改源代码

更改 Visual Basic 源文件 (module1.vb),如下面以斜体突出显示的代码所示。其他的差异(如类名)可忽略不计。

' Import namespaces
Imports System
Imports System.Collections
Module Module1
    Sub Main()
        ' Describe program function
        Console.WriteLine ("QuickSort Visual Basic .NET Sample Application")
        Console.WriteLine ()
        ' Prompt user for filenames
        Dim szSrcFile, szDestFile As String
        Console.Write("Source: ")
        szSrcFile = Console.ReadLine()
        Console.Write("Output: ")
        szDestFile = Console.ReadLine()
        ' TODO: Read contents of source file
        Dim szContents As ArrayList
        szContents = New ArrayList()
    End Sub
End Module 

使用 ArrayList 类

我们将导入 System.Collections 命名空间,这样我们就可以直接引用 ArrayList。此类实现大小可动态调整的对象数组。要插入新的元素,可以简单地将对象传递到 ArrayList 类的 Add() 方法。新的数组元素将引用原始的对象,而垃圾回收器将处理它的释放。

Dim szElement As String
Dim szArray As New ArrayList ()
szElement = "insert-me"
szArray.Add (szElement) 

要检索现有的元素,请将所需元素的索引传递给 Item() 方法。另外,作为一种简写形式,还可以使用圆括号 operator (),它实际上映射到 Item() 方法。

Console.WriteLine (szArray(2))Console.WriteLine (szArray.Item (2))

ArrayList 类中还有许多其他方法,但是插入和检索都是我们需要在此示例中使用的。请查阅 MSDN 库以获得完整的参考指南。

上一页  [1] [2] [3] [4] [5] [6] [7] [8] 下一页

 

 
文章录入:静夜思    责任编辑:静夜思 
  • 上一篇文章: 没有了

  • 下一篇文章:

  •  
    相关文章
    没有相关文章
    原创地带
    24小时热门帖子