王朝网络
分享
 
 
 

从过去到未来、 从Visual和Basic到Net。(从VB到VB.NET的12个技巧)

王朝vb·作者佚名  2006-01-08
宽屏版  字体: |||超大  

从过去到未来、 从Visual和Basic到Net

小气的神 2001.08.08

最初的VB被MS称为“Thunder”计划,早在1990年就开始进行了,那时Gates还亲自在杂志上为VB撰文写稿,告诉人们“Gates on BASIC's Future”;10年之后的VB还在发展和变化,现在被称为“VB。NET”。Gates也仍然在杂志上为VB撰文写稿,告诉人们“How VB Will Program the Future”,我想Gates本人对BASIC语言是有偏爱的。不仅是他,还有许多人也同样的偏爱着比尔和VB。事实上,这个世界有一半的开发人员在使用这种语言,而在中国几乎是百分之八十的程序开发人员在使用它,不过也有事实证明,VB的实际使用率是在下降,因为精通多种语言的程序员在C、C++、Java、Script语言中随意切换出入、成熟的程序员们已不再争论编程语言的优劣,而是根据应用本身按行业的标准实现需要的功能。不管你用什么语言实现,关键是你的实现是否遵守这个行业的标准或协议。

在五年前Java给我们带来一杯热饮,里面的咖啡因让整个网络业兴奋起来;五年之后蜕变中的VB是否能象一杯带有丰富元素的矿泉水,给营养不良的网络带来一点健康呢?希望它还是简单、可视化的。也许比尔想告诉你:BASIC语言不仅是Visaul的和Basic的,现在还是Net的。

新的VB。NET如何?会比我现在正用的VB6好吗,让我们一起来看看,并且迅速掌握这12个技巧。

1. 面向对象的改变,也许它想更面向对象,因为以前它不是,只是自己说自己是面向对象的。VB从 4.0开始宣称自己是面向对象的,也就有了让人有些怀疑的 .cls文件,尽管现在我们用这种.Cls文件写着我们的DB或是Business层组件,丝毫没有任何怀疑和担忧,用惯了也就会觉得一个文件一个Class比C++那样清楚得多,真的这样觉得吗?

过去的VB

======================================================

Private m_VBProperty as Variant

Public Property Let VBProperty( byval strData as Variant )

m_VBProperty = strData

End Property

Public Property Set VBProperty ( Byval strData as Variant )

m_VBProperty = strData

End Property

Public Property Get VBProperty() as Variant

If IsObject(m_VBProperty ) Then

Set VBProperty = m_VBProperty

Else

VBProperty =m_VBProperty

End if

End Property

现在的VB。NET

======================================================

Private m_VBNetProperty as String

Public Property VBNetProperty as String

Get

VBNetProperty = m_VBNetProperty

End Get

Set

m_VBNetProperty = VBNetProperty

End Set

End Property

新增加了ReadOnly 和 WriteOnly 关键字来实现你定义只读或只写的函数,Let 和 Set已不再支持。 以前的 Set objB = objA 现在会产生一个语法错误,VB.NET中你要写成: objB = objA

2. 读取文件,一开始VB中的文件存取就是变化不同的,3.0 、4.0、5.0 都在变化,FileSystemObject的出现让我们可以有一个看似不变的对象可以记忆,但是#、$ 符号始终让我联想到一个黑洞的感觉。危险而黑暗,还好这些都快成为过去。下面的代码完成从系统目录中打开Win.ini文件,并且把它显示到一个Text控件中。

过去的VB

======================================================

Private Sub Command1_Click()

Dim fnum As Integer

Dim s As String

Dim fname As String

Dim winPath As String

On error goto ErrReadTextFile

fnum = FreeFile

' get the windows folder name

winPath = Environ$("SystemRoot")

If winPath = "" Then

MsgBox "Unable to retrieve the Windows path.", _

vbInformation, "Error Reading Windows Path"

Exit Sub

End If

' create a file name

fname = winPath & "\win.ini"

' ensure the file exists

If Dir$(fname) <> "" Then

' open the file

Open fname For Binary As #fnum

If Err.Number = 0 Then

s = Space$(LOF(fnum))

' read the file

Get #fnum, 1, s

Close #fnum

Text1.Text = s

Else

Text1 = "Unable to read the file " & _

fname & "."

End If

Else

Text1 = "The file " & fname & " does not exist."

End If

ExitReadTextFile:

Exit Sub

ErrReadTextFile:

MsgBox Err.Number & ": " & Err.Description

Exit Sub

End SubEnd Sub

现在的VB。NET

======================================================

Private Sub btnReadTextFile_Click(ByVal sender _

As System.Object, ByVal e As System.EventArgs) _

Handles btnReadTextFile.Click

Dim winPath As String

Dim s As String

Dim fname As String

Dim sr As StreamReader

Dim ex As Exception

' get the windows folder name

winPath = System.Environment. _

GetEnvironmentVariable("SystemRoot")

If winPath = "" Then

MessageBox.Show("Unable to retrieve the " & _

"Windows path.", "Error Reading Windows Path", _

MessageBoxButtons.OK, MessageBoxIcon.Information)

Return

End If

fname = winPath & "\win.ini"

If File.Exists(fname) Then

Try

sr = File.OpenText(fname)

s = sr.ReadToEnd

sr.Close()

textbox1.text = s

Catch ex

MessageBox.Show(ex.message)

return

End Try

Else

MessageBox.Show("The file " & fname & _

" does not exist.")

Return

End If

End Sub

新的MessageBox.Show比MessageBox要好看一些,另外sr.Close() 比Close #fileNum 更对象化一些吧。

3. ListBox的多选问题。如何知道一个允许多选的ListBox中那些选项是选中的。看看下面的实现吧,尽管很不喜欢VB的Redim ,但是有时用它真的能很快解决问题。VB.NET中,你可以直接使用你需要的数据了。

过去的VB

======================================================

Private Sub cmdGetListSelections_Click()

Dim i As Integer

Dim sArr() As String

Dim selCount As Integer

For i = 0 To List1.ListCount - 1

If List1.Selected(i) Then

ReDim Preserve sArr(selCount) As String

sArr(selCount) = List1.List(i)

selCount = selCount + 1

End If

Next

Call showStringArray(sArr)

End Sub

Private Sub showStringArray(arr() As String)

Dim s As String

Dim i As Integer

Dim arrCount As Integer

On Error Resume Next

arrCount = UBound(arr) + 1

On Error GoTo 0

If arrCount > 0 Then

For i = 0 To arrCount - 1

s = s & arr(i) & vbCrLf

Next

MsgBox s

End If

End Sub

现在的VB.NET

======================================================

Private Sub btnGetListSelections_Click(ByVal sender _

As System.Object, ByVal e As System.EventArgs) _

Handles btnGetListSelections.Click

Dim sArr(ListBox1.SelectedItems.Count - 1) _

As String

Dim anItem As String

Dim i As Integer

For Each anItem In ListBox1.SelectedItems

sArr(i) = anItem

i +=1

Next

End Sub

Private Sub showStringArray(ByVal arr() As String)

MessageBox.Show(Join(arr, _

System.Environment.NewLine))

End Sub

End Class

4. VB程序中如何启动另外一个程序,等待并且知道它何时退出。没有VB。NET前,这个问题是需要专业的作法的,就像许多问题一样,VB不是解决不了,是需要调用一些罕为人知的函数,MS把诸如此类的问题收集在一起,叫做MS Knowledge Base,现在应该是叫MSDN Knowledge Base 了,也许你已经猜到是要用Shell函数了,过去的VB在如何等待启动程序退出并且获得退出的状态上象我们投资股票一样,需要拿准时候,这样才处理得好。

过去的VB

======================================================

你可以去下面的网址中获得帮助和代码。

http://support.microsoft.com/support/kb/articles/Q129/7/96.ASP

现在的VB.NET

======================================================

Private Sub btnLaunch_Click(ByVal sender As _

System.Object, ByVal e As System.EventArgs) _

Handles btnLaunch.Click

Dim sysPath as String

SysPath = System.Environment.GetFolderPath _

(Environment.SpecialFolder.System)

Shell(sysPath & "\notepad.exe", _

AppWinStyle.NormalFocus, True)

MessageBox.Show("You just closed Notepad", _

"Notepad Closed")

End Sub

新的VB.NET中已经变得同步了,两个程序在一个进程空间中了。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有