KeyFC欢迎致辞,点击播放
资源、介绍、历史、Q群等新人必读
KeyFC 社区总索引
如果你找到这个笔记本,请把它邮寄给我们的回忆
KeyFC 漂流瓶传递活动 Since 2011
 

请问一道简单的VB题……

[ 7918 查看 / 25 回复 ]

回复:请问一道简单的VB题……

我會這樣寫:

======

Private Sub Command1_Click()
Text4.Text = CStr((CSng(Text1.Text) + CSng(Text2.Text) + CSng(Text3.Text)) / 3)
End Sub

======

我不明白為什麼不教用 a = CSng(Text1.Text) 即把型別轉換都寫進去.
沒那概念而習慣用Varient的話, 以後學其他Language時會很麻煩的...

EDIT: 即使繼續寫VB也很麻煩的... 好像我這樣用一行式寫而不做型別轉換的話, 如果Text1到Text3的內容分別是2, 3, 4的話, Text4的內容就會是(234 / 3) = 78而不是3了
最后编辑cheong00 最后编辑于 2007-09-25 23:17:39
分享 转发
TOP

回复: 请问一道简单的VB题……

原帖由 王二 于 2007-9-25 19:02:00 发表
Dim a As Single, b As Single, c As Single, d As Single

Private Sub Command1_Click()
a = Text1.Text
b = Text2.Text
c = Text3.Text
d = (a + b + c) / 3
Text4.Text = d
End Sub

Private Sub C......

Form_Load那些都沒有效果的, 可以略過...
又不是被其他Form call出來的dialog, 反正a,b,c,d的初始值也是 0 嘛...

==

Dim a As Single, b As Single, c As Single, d As Single

Private Sub Command1_Click()
a = Text1.Text
b = Text2.Text
c = Text3.Text
d = (a + b + c) / 3
Text4.Text = d
End Sub

==

然後在把Text1到Text4拉到Form上時, 把Text屬性設成"0"就好.
TOP

回复: 请问一道简单的VB题……

原帖由 helenzhu 于 2007-9-25 22:54:00 发表
那个……运行的时候会提示  实时错误“424” 要求对象

你.. 不會是不記得把Text1至Text4和Command1拉到Form上吧...

原帖由 helenzhu 于 2007-9-25 22:54:00 发表
还有如果某个text里不输入任何数字的话会提示  实时错误“13” 类型不匹配 怎么解决呢?

把 a = Text1.Text 改成

If (Len(Text1.Text) > 0) Then
    a = Text1.Text
End If

其他兩個TextBox比照辦理...

EDIT: 想一想還是多做一些比較好, 不然第二次計算結果就可能不對了:

If (Len(Text1.Text) > 0) Then
    a = Text1.Text
Else
    a = 0
    Text1.Text = 0
End If
最后编辑cheong00 最后编辑于 2007-09-25 23:13:59
TOP

回复:请问一道简单的VB题……

你在 "a = Text1.Text" 那行設一個breakpoint(中斷點), 在按command1時就會停在那行上了.

之後慢慢按F8 step(逐行執行), 看在那一行出錯吧...
TOP

回复: 请问一道简单的VB题……

原帖由 helenzhu 于 2007-9-25 23:58:00 发表
a = Text1.Text  就有错误……


我剛剛做的... 你就自己看著照做一次吧...

======
上傳不了JPG...  才113KB也說太大了... :(

以下是Form1.frm的內容 (可以用Notepad開啟)...

======

VERSION 5.00
Begin VB.Form Form1
  Caption        =  "Form1"
  ClientHeight    =  3090
  ClientLeft      =  60
  ClientTop      =  450
  ClientWidth    =  4680
  LinkTopic      =  "Form1"
  ScaleHeight    =  3090
  ScaleWidth      =  4680
  StartUpPosition =  3  'Windows Default
  Begin VB.CommandButton Command1
      Caption        =  "Command1"
      Height          =  375
      Left            =  2400
      TabIndex        =  4
      Top            =  600
      Width          =  1335
  End
  Begin VB.TextBox Text4
      Height          =  375
      Left            =  50
      TabIndex        =  3
      Text            =  "0"
      Top            =  2040
      Width          =  2000
  End
  Begin VB.TextBox Text3
      Height          =  375
      Left            =  50
      TabIndex        =  2
      Text            =  "0"
      Top            =  1440
      Width          =  2000
  End
  Begin VB.TextBox Text2
      Height          =  375
      Left            =  50
      TabIndex        =  1
      Text            =  "0"
      Top            =  840
      Width          =  2000
  End
  Begin VB.TextBox Text1
      Height          =  375
      Left            =  50
      TabIndex        =  0
      Text            =  "0"
      Top            =  360
      Width          =  2000
  End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim a As Single, b As Single, c As Single, d As Single
Private Sub Command1_Click()
    If (Len(Text1.Text) > 0) Then
        a = CSng(Text1.Text)
    Else
        Text1.Text = "0"
        a = 0
    End If
    If (Len(Text2.Text) > 0) Then
        b = CSng(Text2.Text)
    Else
        Textb.Text = "0"
        b = 0
    End If
    If (Len(Text3.Text) > 0) Then
        c = CSng(Text3.Text)
    Else
        Text3.Text = "0"
        c = 0
    End If
    d = (a + b + c) / 3
    Text4.Text = d
End Sub
最后编辑cheong00 最后编辑于 2007-09-26 00:18:06
TOP

回复: 请问一道简单的VB题……

原帖由 helenzhu 于 2007-9-26 0:23:00 发表
If (Len(Text2.Text) > 0) Then
        b = CSng(Text2.Text)
    Else
        Textb.Text = "0"
        b = 0
    End If


这里红色部分

我改过来......

Emmm... 這樣說其他部份也要做相應的改變才對...

If (Len(Textb.Text) > 0) Then
        b = CSng(Textb.Text)
    Else
        Textb.Text = "0"
        b = 0
    End If
TOP

回复: 请问一道简单的VB题……

嗯, 有寫C++/C#的話應該知道當 a + b而a和b是不同型別的數字的話, 結果的型別會是a和b之間最大容量的型別. 這也叫implicit casting.

不過呢, 我還是比較習慣寫成 (typemax)a + (typemax)b, 把需要的轉換explicit地寫出來. 這樣在找bug的時候會快/容易一些找到型別轉換時的錯誤.

目前的目標是養成/改進寫程式碼的習慣, 以減少不小心而型成的bug和加快除錯速度為優先. 包括固定的block indention習慣, 寫比較式c==(a+b)寫成(a+b)==c, 還有即使編譯器會按operator priority計算但寫的時候決不省略括號等...

原帖由 王二 于 2007-9-26 2:45:00 发表
个人最喜欢的还是C/C++和Win32 API的组合,挖掘操作系统的秘密还是作为一个热爱写程序的程序员的终极目标啊~~~~(远目

這我很贊同. 我也是對這方面比較有興趣的. :D
TOP