Dim i As Long
Dim ParaAll As String
For i = 0 To 800000
  ParaAll = ParaAll & i & ","
Next 这段垃圾代码有运行完的时候吗?为什么状态和死机一样,怎么搞才能得到1,2,3,4....n
的字符串

解决方案 »

  1.   

    加DoEvents看看
    For i = 0 To 800000
      ParaAll = ParaAll & i & ","
      DoEvents
    Next
      

  2.   

    正确的书写应该是下面这样:
    Private Sub Command1_Click()
       Dim i As Integer
       Dim ParaAll As String
       For i = 0 To 3
            ParaAll = ParaAll & i & ","
       Next
       Text1.Text = ParaAll ‘将结果在一个文本框里面显示出来
    End Sub
      

  3.   


    正确的书写应该是下面这样:Private Sub Command1_Click()
       Dim i As Long
       Dim ParaAll As String
       For i = 0 To 800
            ParaAll = ParaAll & i & ","
       Next
       Text1.Text = ParaAll
    End Sub
      

  4.   

    VB做大字符串连接很慢的,并没有死循环,只是还没执行完而已。
    改成以下代码速度可以大幅提升:Dim i As Long
    Dim ParaStr() As String
    dim ParaAll As Stringredim ParaStr(800000)
    For i = 0 To 800000
      ParaStr(i) = cstr(i)
    Next ParaAll=join(ParaStr,",")这样 paraAll 就是你要的字符串了。速度可能提升上千倍。但是别把 i 搞得太大,不然可能会耗光内存造成溢出。
      

  5.   

    将以下内容保存成StringW.cls
    VERSION 1.0 CLASS
    BEGIN
      MultiUse = -1  'True
      Persistable = 0  'NotPersistable
      DataBindingBehavior = 0  'vbNone
      DataSourceBehavior  = 0  'vbNone
      MTSTransactionMode  = 0  'NotAnMTSObject
    END
    Attribute VB_Name = "StringW"
    Attribute VB_GlobalNameSpace = False
    Attribute VB_Creatable = True
    Attribute VB_PredeclaredId = False
    Attribute VB_Exposed = False
    Attribute VB_Ext_KEY = "SavedWithClassBuilder6" ,"Yes"
    Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
    Option Explicit
    Private cTxtLen As Long, cBuffLen As Long
    Public ValueB As String
    Public buffSize As LongPublic Property Get Value() As String
    Attribute Value.VB_UserMemId = 0
       Value = Mid$(ValueB, 1, cTxtLen)
    End PropertyPublic Property Let Value(vNewValue As String)
       ValueB = vNewValue
       cTxtLen = Len(vNewValue)
       cBuffLen = cTxtLen
    End PropertyPublic Property Get Length() As Long
       Length = txtLen
    End PropertyPublic Function Add(s As String) As String
       Dim oldLen As Long, addTxtlen As Long
       addTxtlen = Len(s)
       oldLen = cTxtLen
       cTxtLen = cTxtLen + addTxtlen
       If addTxtlen > buffSize Then
          ValueB = Value + s
       End If
       If cBuffLen < cTxtLen Then
          ValueB = ValueB + String$(buffSize, Chr$(0))
          cBuffLen = cBuffLen + buffSize
       End If
       Mid$(ValueB, oldLen + 1, addTxtlen) = s
    End FunctionPrivate Sub Class_Initialize()
       buffSize = 10240
    End Sub这样写代码:
    Dim i As Long
    Dim ParaAll As New StringW
    ParaAll.buffSize = 409600
    For i = 0 To 800000
       ParaAll.Add i & ","
    Next
    Text1.Text = ParaAll
      

  6.   

    发现点问题,修改下StringW.cls的Add函数
    Public Function Add(s As String) As String
       Dim oldLen As Long, addTxtlen As Long
       addTxtlen = Len(s)
       oldLen = cTxtLen
       cTxtLen = cTxtLen + addTxtlen
       If addTxtlen > buffSize Then
          ValueB = Value + s
          Exit Function
       End If
       If cBuffLen < cTxtLen Then
          ValueB = ValueB + String$(buffSize, Chr$(0))
          cBuffLen = cBuffLen + buffSize
       End If
       Mid$(ValueB, oldLen + 1, addTxtlen) = s
    End Function
      

  7.   

    Set ParaAll = Nothing就可以释放内存了,你调研ParaAll 的Sub结束也会自动释放内存吧,程序退出也会释放内存
    如果你还需要使用ParaAll ,那么ParaAll = ""基本上释放了内存了
      

  8.   

    死机是你的操作系统或者硬件原因,不会是还用Windows 98吧?
      

  9.   

    Dim i As Long
    Dim ParaStr() As String
    dim ParaAll As Stringredim ParaStr(800000)
    For i = 0 To 800000
      ParaStr(i) = cstr(i)
        DoEvents               '加入DoEvents,可以表现的不像死机,但是执行时间变长了,分配时间片嘛
    Next ParaAll=join(ParaStr,",")