It's Easy!Private Sub Form_Load()
    Dim source As String
    source = "0000000"
    MyWrite source, 1
    MyWrite source, 3
    MsgBox source
End SubPrivate Function MyWrite(sSrc As String, iPos As Long) As String
    Mid(sSrc, iPos) = "1"
    MyWrite = sSrc
End Function

解决方案 »

  1.   

    我觉得你是在做二进制的处理,其实二进制的处理应该从低位开始处理才好,就是第一位是右边起的第一位,这样就可以把他当做二进制来处理了,不过,我还是按着你的思路来做一个吧:)Option ExplicitPrivate Sub Form_Load()
        Dim Str As String
        Str = "000000"
        WriteBin Str, 1     '执行完后为"100000"
        WriteBin Str, 3     '执行完后为"101000"
    End SubPrivate Function WriteBin(Source As String, iPos As Long) As String
        '
        Dim iSel As Long
        If iPos < 1 Then
            WriteBin = Source
            Exit Function
        End If
        If iPos > Len(Source) Then iPos = Len(Source)
        '取第iPos位
        iSel = Val(Mid(Source, iPos, 1))
        '0,1反转
        iSel = 1 - iSel
        '重组0,1序列
        Source = Mid(Source, 1, iPos - 1) + Trim(Str(iSel)) + Mid(Source, iPos + 1, Len(Source) - iPos)
        '返回函数值
        WriteBin = Source
    End Function不过我还是觉得用二进制的方法更直接。
      

  2.   

    奇妙的一点: lumine与我用的参数同为iPos。其实改为以下代码更好,解决lumine的不能写"0"的问题:Option ExplicitPrivate Sub Form_Load()
        Dim Str As String
        Str = "000000"
        WriteBin Str, 1    '执行完后为"100000"
        WriteBin Str, 3    '执行完后为"101000"
    End SubPrivate Function WriteBin(Source As String, iPos As Long) As String
        '
        Dim iSel As Long
        If iPos < 1 And iPos > Len(Source) Then
            WriteBin = Source
            Exit Function
        End If
        '取第iPos位
        iSel = Val(Mid(Source, iPos, 1))
        '0,1反转
        iSel = 1 - iSel
        '写回0,1序列
        Mid(Source, iPos) = Trim(Str(iSel))
        '返回函数值
        WriteBin = Source
    End Function
      

  3.   

    实际上我贴了这问题后就在MSDN中发现了MID还可以更改字串,不过不是谢谢大家的帮助
      

  4.   

    还有,我觉的最后一句writebin=source...可以不要,因为VB默认为传地址方式,所以已经改了
    source字串,直接在主程序中就可用了
      

  5.   

    sowrd_hero: 不对,作为一个好习惯,函数后面一般都要返回一个值,记住,这是习惯。
    子程序则不必。
      

  6.   

    shines,你找扁啊,我上次给分了,系统出问题了
    VB很可恶