转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> VB.NET程序 >> 正文
VB.NET Data Types         ★★★★

VB.NET Data Types

作者:闵涛 文章来源:闵涛的学习笔记 点击数:2753 更新时间:2009/4/23 19:01:02
string(s.Length - 5) '''' returns "World"

From the two examples, the Mid$ can easily be replaced by the Substring method as well.

Trim$
In VB6, you could use the Trim$ function as in the following code.

Dim s1 As String, s2 As String
s1 = "    Hello World   "
s2 = Trim$(s1)

In VB7, use the Trim method of the System.String class, instead.

Imports System
Dim s1 As String, s2 As String
s1 = "    Hello World   "
s2 = s1.Trim()

LCase$ and UCase$
In VB6, the LCase$ returns a new string in lower case and the UCase$ function returns a new string in upper case. These two functions are complemented by the ToLower() and ToUpper methods of the String class. As an example,

LCase$("Hello World")

is the same as

"Hello World".ToLower()

and

UCase$("Hello World") 

returns the same result as

"Hello World".ToUpper()

Replace
In VB6, the Replace function is used to replace part of a string with a substring. For example, the following VB6 code will result in "kingkong."

Dim s As String
s = Replace("dingdong", "d", "k")  '''' s = "kingkong"

The String class has the Replace function that does a similar task. For example, the following VB7 code does the same thing.

Dim s as string = "dingdong".Replace("d"c, "k"c)

Note that the Replace method of the String class accepts two Char arguments, not String arguments.

String comparison
You can still use the string manipulation functions:

If s1 = s2 Then

which is the same as writing the following:

If s1.Equals(s2) Then

The StringBuilder Class
The System.String class represents an immutable string of characters, just like String in VB6. This means the value of a String variable cannot be modified. If you try to modify a String, a new String object is created instead. Remember that object creation is an expensive operation. The .NET Framework also provides the System.Text.StringBuilder class that is more efficient to work with. For more information, see the .NET Framework Reference.

Dates

Forget the Date and Time statements in VB6. VB7 now uses the DateTime structure in the System namespace to represent a date and time value. This structure has a large number of methods and properties that are more than sufficient to work with to get any date and time value. The most important property is probably Now, which retrieves the current date and time. And then there are Day, Date, Month and Year properties that you can use to display the date in any format. For example, the following code displays the current date and time in the "dd/mm/yyyy" format.

imports System
Module Module1
  Sub Main()
    Dim now As DateTime
    now = DateTime.Now
    Console.writeline(now.Day & "/" & _
      now.Month & "/" & now.Year)
  End Sub
End Module

When using DateTime, you will probably use its Ticks property a lot to retrieve the 100-nanosecond tick count for this instance. This property can replace the Timer function in VB6 when you want to benchmark an operation, as in the following code.

imports System
Dim t1 As Long, t2 As Long
t1 = DateTime.Now.Ticks

'''' operation to be measured here

t2 = DateTime.Now.Ticks
System.Console.writeline(t2 - t1)

The DateTime structure is also convenient to use for some "date-time mathematics." For example, you can add x hour or y minutes to an instance of DateTime using the AddHours and AddMinutes methods.

Object

As mentioned above, the Object class in VB7 represents the .NET Framework Object class. This class is the root of every other type in the .NET Framework. In VB7, when you create a new class, that new class implicitly inherits from Object. Of particular importance are the GetType and ToString methods. The GetType method enables you to obtain the Type of the object, and the ToString method returns a String that represents the current object.

Structure

In VB6, a structure is declared using the Type...End Type construction. The structure and its members all default to public access. Explicit access declaration is optional. The following example shows a valid structure declaration:

Type Employee 
  ID As Integer 
  FirstName As String 
  LastName As String   
End Type

In VB7, the Type statement is not supported. You must declare structures using the Structure ... End Structure construction. Every member of a structure must have an access modifier, which can be Public, Protected, Friend, Protected Friend, or Private. You can also use the Dim statement, which defaults to public access. The structure in the preceding example can be declared as follows:

Structure Employee
  Public ID As Integer '''' Must declare access, even if Public.
  Dim FirstName As String '''' Still defaults to Public access.
  Private LastName As String '''' Can be made Private inside Structure.
End Structure

VB7 unifies the syntax for structures and classes. Structures support most of the features of classes, including methods. Structures can serve as light-weight classes that are cheap to create. However, structures do not support inheritance.

Some Other Changes

The following are some important changes that can ease your migration from VB6.

Option Explicit is Imposed by default
You must now declare a variable before using it.

Declare and Assign in the Same Line
You can now declare and assign a variable in the same line. A long-awaited feature.

Dim myVar As Integer = 9

Multiple Variable Declaration
In VB6, you can declare variables of different types in the same statement, but you must specify the data type of each variable or it defaults to Variant. The following example shows multiple declarations and their resulting data types:

Dim I, J As Integer             '''' I is Variant, J is Integer. 
Dim L As Integer, M As Integer  '''' L is Integer, M is Integer. 
Dim N As Integer, X As Double   '''' N is Integer, X is Double.

In VB7, you can declare multiple variables of the same data type without having to repeat the type keyword. The declarations equivalent to those in the preceding example are as follows:

Dim I                           '''' I is Object. 
Dim J As Integer                '''' J is Integer. 
'''' -- OR -- 
Dim I As Object, J As Integer   '''' I is Object, J is Integer. 
Dim L, M As Integer             '''' L is Integer, M is Integer. 
Dim N As Integer, X As Double   '''' N is Integer, X is Double.

External Procedure Declaration
In Visual Basic 6.0, when you declare a reference to an external procedure with the Declare statement, you can specify As Any for the data type of any of the parameters and for the return type. The As Any keyword disables type checking and allow any data type to be passed in or returned.

VB7 does not support the Any keyword. In a Declare statement, you must specifically declare the data type of every parameter and of the return. This improves type safety. You can overload your procedure declaration to accommodate various return types.

Variable Scopes
In VB6, any variable declared inside a procedure has procedure scope, so it can be accessed anywhere else within the same procedure. If the variable is declared inside a block -- that is, a set of statements terminated by an End, Loop, or Next statement -- the variable is still accessible from outside the block. The following example illustrates procedure scope:

For I = 1 To 10 
  Dim N As Long '''' N has procedure scope although 
                '''' it was declared within a block. 
  N = N + Incr(I) 
Next 
W = Base ^ N    '''' N is still visible outside the block

In VB7, a variable declared inside a block has block scope, and it is not accessible from outside the block. The preceding example can be rewritten as follows:

Dim N As Long  '''' N is declared outside the block 
               '''' and has procedure scope. 
For I = 1 To 10 
  N = N + Incr(I) 
Next 
W = Base ^ N   '''' N is visible outside the block but I is not.

However, the lifetime of a variable is still that of the entire procedure. This is true whether the variable has block scope or procedure scope. If you declare a variable inside a block, and if you enter that block several times during the lifetime of the procedure, you should initialize the variable to avoid unexpected values.

Static Local Variables
In VB6, you can declare a procedure with the Static modifier. This causes every local variable within the procedure to be static

上一页  [1] [2] [3]  下一页


[办公软件]GETPIVOTDATA函数语法介绍  [常用软件]桌面搜索新利器 “88Data”
[VB.NET程序]定制VB.NET控件编程之拦截击键动作  [VB.NET程序]VB.NET VS C#.
[VB.NET程序]VB.NET 中调用浏览目录对话框  [VB.NET程序]VB.NET and C# 语法比较手册
[VB.NET程序]VB.NET 拖动无边框的窗体  [VB.NET程序]C# to VB.NET 翻译器
[VB.NET程序]101 VB.NET Applications 读书笔记(1)  [VB.NET程序]vb.net 程序设计规范(1)
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台