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

    技术2022-05-11  64

    从过去到未来、 VisualBasicNet 

     

    小气的神 2001.08.08

     

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

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

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

     

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

    过去的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

     

    现在的VBNET

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

    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.05.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

     

    现在的VBNET

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

       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.ShowMessageBox要好看一些,另外sr.Close() Close #fileNum 更对象化一些吧。

     

    3.   ListBox的多选问题。如何知道一个允许多选的ListBox中那些选项是选中的。看看下面的实现吧,尽管很不喜欢VBRedim ,但是有时用它真的能很快解决问题。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程序中如何启动另外一个程序,等待并且知道它何时退出。没有VBNET前,这个问题是需要专业的作法的,就像许多问题一样,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中已经变得同步了,两个程序在一个进程空间中了。

     


    最新回复(0)