精华内容
下载资源
问答
  • 案例说明本案例实现了如何在删除数组中的任意位置的一个元素,使数组成为一个新的数组。效果如下图所示。关键技术由于对数组进行删除元素的操作,会减少数组的长度,因此数组必须定义为动态数组。如果是静态数组则...

    案例说明

    本案例实现了如何在删除数组中的任意位置的一个元素,使数组成为一个新的数组。效果如下图所示。

    8e93822b67de1388e6e14d9688d8d208.png

    关键技术

    由于对数组进行删除元素的操作,会减少数组的长度,因此数组必须定义为动态数组。如果是静态数组则无法完成数组插入这项操作。由于VB并未直接提供删除数组元素的方法,因此我们需要间接的完成删除元素的操作。

    删除一个数组元素时,需要逐一将要删除元素后面的元素前移一个位置,最后在利用redim方法重新定义数组,这样就可以实现删除数组元素的要求了。

    设计过程

    步骤一

    按照下图完成程序界面的搭建,其中下方打印区使用的是PictureBox控件。

    整个界面,包括1个窗体、1个标签、1个文本框控件,以及2个按钮和1个图像框。

    cf6bce43ac041d7d87395cf9ed18c517.png

    步骤二

    首先,需要在代码区顶部声明一个全局的动态数组arr

    Option ExplicitDim arr()

    步骤三

    然后,编写窗体加载的代码,在这里我们为arr数组赋予一个初始的内容。

    Private Sub Form_Load()    '初始化数组    arr = Array(1, 3, 5, 7, 9)End Sub

    步骤四

    接着,我们编写显示数组内容的代码,该代码可以将数组所有的元素打印到picturebox控件中。

    Private Sub Command2_Click()    Dim i%    '遍历并输出arr数组元素    For i = LBound(arr) To UBound(arr)        Picture1.Print arr(i) & " ";    Next i    Picture1.Print  '换行End Sub

    步骤五

    最后,编写插入数组元素的代码。

    Private Sub Command1_Click()    Dim i%    '防止未输入元素下标    If Text1 = "" Then Exit Sub        '防止下标超出数组的上下标范围    If Val(Text1) < LBound(arr) Or Val(Text1) > UBound(arr) Then        MsgBox "下标超出范围"        Exit Sub    End If        '循环移动数组元素    For i = Val(Text1) + 1 To UBound(arr)        arr(i - 1) = arr(i)    Next i        '重新定义数组长度(减少1个长度,代表舍弃了数组最后一个元素)    ReDim Preserve arr(UBound(arr) - 1)        MsgBox "删除成功"End Sub

    步骤六

    最后,我么来进行测试。运行程序,首先点击显示数组,将数组初始内容打印出来,然后输入要插入的位置和数值,点击插入元素,再点击显示数组,查看插入的效果。

    8e93822b67de1388e6e14d9688d8d208.png

    总结

    数组在VB中具有非常非常重要的作用,对于处理数据具有绝对的优势,因此掌握数组的基本操作,尤其是增加、删除和插入以及查询都是相当重要的。小伙伴们一定要勤加练习哦。

    友情提示:

    添加QQ群 一起学VB 1138946358 可第一时间获取本案例源代码~~

    展开全文
  • vb 如何定义一个未知大小一维数组? 2012-12-09 10:33 酷依航 | 分类:VB | 浏览1941次我是这样定义 dim a(100) as string,但是经常性出现下标越界,又不想定义的范围太大,就想问下可不可以定义一个自适应的一维...
    <div id="wgt-ask" class="wgt-ask accuse-response line mb-5"><h1 class="mb-5" accuse="qTitle"><span class="ask-title">vb 如何定义一个未知大小一维数组?</span> </h1><div id="ask-info" class="line f-aid mb-10"><span class="grid-r ask-time"></span><ins style="DISPLAY: none" class="accuse-area"></ins>2012-12-09 10:33 <a target=_blank class="user-name" aria-describedby="ui-tooltip-0" href="http://www.baidu.com/p/%E9%85%B7%E4%BE%9D%E8%88%AA?from=zhidao" rel="nofollow" target="_blank" alog-action="qb-ask-uname">酷依航</a> <span class="f-pipe">|</span> <span class="classinfo f-simsun" alog-group="qb-cate-nav">分类:VB<a target=_blank id="modify-category" class="i-added-gray ml-5" href="http://zhidao.baidu.com/link?url=GEiHOE9sugyaH2XjxryAp9yxlEdbFQyaydO_0iWm24qHOW5z4DXRECDeVoXKlYMFXFQRQEuwwuV9Hia3H5lU5_#"></a> </span><span id="v-times" class="f-pipe">|</span><span class="browse-times"> 浏览1941次</span></div><pre class="line mt-10 q-content" accuse="qContent">我是这样定义 dim a(100) as  string,但是经常性出现下标越界,又不想定义的范围太大,就想问下可不可以定义一个自适应的一维数组?
    删除问题
    举报违规检举侵权投诉|2012-12-09 10:51 提问者采纳
    可以的,先定义一个动态数组:dim a() as string
    到使用的地方,再重定义上下限:
    n=15
    redim a(1 to n)  ',当然,n 在重定义前就要确定
    如果a中放了数据了,还要增加上限,还可以用 ReDim Preserve 语句来扩充数组:
    a(15)=15
    ReDim Preserve a(UBound(a) + 10)  '只增加上限,不改变元素值

    
    
    展开全文
  • 比如定义这样一个数组 第二维多个数不定 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 1 8 28 56 70 56 28 8 1 1 9 36 84 126 126 84 36 9 1 1 10 45 ...
  • 各位:我现在在JavaScript中定义一个一维数组,然后调用VB编写的DLL对象,在DLL对象给此数组赋值,然后在JavaScript读出已经赋值的数组。请问如何操作。 DLL对象: TestPrj.Test PublicSubTest(strName()AsVariant...
      各位:我现在在JavaScript中定义一个一维数组,然后调用VB编写的DLL对象,在DLL对象给此数组赋值,然后在JavaScript读出已经赋值的数组。请问如何操作。 
    

      DLL对象:

      TestPrj.Test

      PublicSubTest(strName()AsVariant)
       strName(0)="MR"
       strName(1)="zhang"
      EndSub

      JavaScript:

      <Scriptlanguage='JavaScript'>
       varA=newArray();
       varTest=newActiveObjectX("TestPrj.Test");
       Test.Test(A);
       Response.Write(A.length);
      </Script>

      请问出错在哪里????

      ---------------------------------------------------------------------------------------------------------------------------------------------------------

      您可以在VB中建立AciveXdll,处理含数组参数的函数,在ASP中调用AciveX对象的函数。由于Javascript的array对象与VB中的array以不同的方式存储,因此Javascript的array对象作为VBActiveXdll函数的参数将出现类型不匹配的错误。建议您在ASP中用VBscript。

      如下例,先在VB中建立AciveXdll工程,项目名为ASPArray,类名clsArray

      PublicFunctionTestArray(ByRefvArrayAsVariant)AsString
        DimnCntAsInteger
        'Checkthattheargumentpassedisanarray
        IfNotIsArray(vArray)Then
         TestArray="ParameterisnotanArray"
         ExitFunction
        EndIf
        FornCnt=LBound(vArray)ToUBound(vArray)
          'changeArrayElement
          vArray(nCnt)="aqqq"
        NextnCnt  
        TestArray="ParameterisanArray"
      EndFunction

      在ASP中引用ASPArray.clsArray对象,

      <%
       DimoTestObj,vMyArray(2),vRtnValue
          
        vMyArray(0)="Element1"
        vMyArray(1)="Element2"
        vMyArray(2)="Element3"
     
        SetoTestObj=Server.CreateObject("ASPArray.clsArray")
     
        vRtnValue=oTestObj.TestArray(vMyArray)
        Response.Write(vRtnValue)
       %>
       vMyArray(1)=<%=vMyArray(1)%>

     

      详细信息请参考以下链接:

      HOWTO:ImplementArrayArgumentsinVBCOMObjectsforASP

      http://support.microsoft.com/support/kb/articles/q217/1/14.asp

      您可以使用JavaScript中vbArray对象的toArray()方法把VBScript的数组(Array)转化为Javascript数组,但是截至到目前VBScript(4.0)版,还没有把JavaScript数组转化为VBscript数组的方法。

      详细信息请参考以下文档中PassingArraysfromVBScripttoJscript部分:

      UsingVBScriptandJScriptonaWebPage

      http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvid/html/msdn_vbnjscrpt.asp

    展开全文
  • VB6 给对象数组如何定义事件?

    千次阅读 2017-08-14 18:35:42
    and VB will give you a nice, helpful, error message that tells you exactly why it isn't allowed. Now really the reason why you cannot use  WithEvents  on an array of objects is quite ...
    During the course of your programming, if you have used classes at all you will probably have also used the keyword WithEventsWithEvents is
     used when declaring an object variable, to indicate that you also wish to receive events raised by that object. It is nice because you can then use the object variable just like a control on a form, except that it can be used in a class.
    
    Private WithEvents myObject As SomeClass

    You have probably also used arrays of object variables too. This comes in handy when you want to manage multiple objects cleanly, as you can loop through them and carry out the same actions to each in turn (See this thread for an explanation of arrays).
    Private myObjects() As SomeClass

    Now the trouble comes when you, as happens eventually, want to combine the two. You need to handle events from your objects, but you also need to have an array of them, and handle events from all of the object instances. Since programmers are inherently logical, you'd try this:
    Private WithEvents myObjects() As SomeClass
    ... and VB will give you a nice, helpful, error message that tells you exactly why it isn't allowed.

    Now really the reason why you cannot use WithEvents on an array of objects is quite simple - if you had a single event handler for each object, you would have no way of knowing which object fired the event. You will probably have noticed that event handlers for controls that are part of control arrays will have an extra Index parameter - this is because control arrays were invented before the days of COM and it required some fair hacking on Microsoft's part to add the Index property to control events under the COM system. This same hacking could not be done for arrays of objects, because the objects do not have Index properties - your array simply holds references to each object.

    So do we give up? No... of course there is always a solution  In this case it's one that sounds complex, but in reality (and you'll see an example of it) it's not too hard. I am going to offer two solutions - one for classes that you create yourself, and one for those that you do not have the source to.

    If you have the source it's easy. First you need to get rid of all events - since we can't use them. You can replace the RaiseEvent calls with calls to named methods in your event-handling class or form, that will form the new event handlers. Now you run into the first potential problem which is that these methods may not exist. To ensure that they do, we will have to require that the event-handling class/form Implements an interface class which defines all of our events and their parameters. Implementing an interface means that the class/form will have to contain all the event handling methods, so we eliminate the possibility that they do not exist when we are trying to fire an event.

    You define an "event" in the interface class like this:
    Sub MyEvent(AnyParams)
    End Sub
    Note that it is not necessary for the function body to be empty, but whatever you put in there we are not going to use anyway.

    Once you've defined all events you then need to know where to send them. For that purpose we can define a property in our custom class. Note that we type this property as the type of our event interface - that forces potential event handling classes to contain the proper event handlers and eliminates any possibility of errors on our side - it is up to "them" to ensure that they meet the requirements 
    Private mCallback As IMyEventInterface

    Property Set Callback(newObj As IMyEventInterface)
        Set mCallback = newObj
    End Property

    Property Get Callback() As IMyEventInterface
        Set Callback = mCallback
    End Property

    Now to raise an event we can simply call the appropriate event handler in our "callback" class. Of course, we must first check to see if it exists - if no callback class has been set, we cannot call anything (obviously)
    ' raise an event
    If (Not mCallback Is Nothing) Then _
        mCallback.MyEvent anyParams

    And finally we want to implement an array of the classes - and now you can see the final solution that does NOT use WithEvents 
    Implements IMyEventInterface

    Private myObject() As SomeClass

    Private Sub Form_Load()
        ReDim myObject(5)
        For i = 0 To 5
            Set myObject(i) = New SomeClass
            Set myObject(i).Callback = Me
        Next i
    End Sub

    Private Sub IMyEventInterface_MyEvent(AnyParams)
        ' our event handler
    End Sub



    Now you might recall I mentioned a solution for classes for which you do not have the source code for, such as classes in DLLs. Well here you can apply a bit of creative adaptation of the first solution. My SomeClass class is now going to a "wrapper" class for the closed-source class - so called because it will contain a reference to an instance of this class, but we will be using the wrapper class to access it. The instance of the closed-source class will need to be declared using WithEvents, because you can't change the way it raises events. However you still have the same setup in place for your callback and interface (you will need to include in your interface all events of the closed-source class that you handle in your wrapper class) - so you can declare an array of wrapper classes and use them to handle the events from the "wrapped" classes.

    You will need to also add a property so that you can access the wrapped class.
    Private WithEvents mWrapped As WrappedClass
    Private mCallback As IMyEventInterface

    ' Callback() property snipped...

    Property Set Wrapped(newObj As WrappedClass)
        Set mWrapped = newObj
    End Property

    Property Get Wrapped() As WrappedClass
        Set Wrapped = mWrapped
    End Property

    ' for each event handled, raise a new one by the callback method
    Private Sub mWrapped_SomeEvent()
        If (Not mCallback Is Nothing) Then _
            mCallback.SomeEvent
    End Sub
    Now you use the wrapper class, something like this:
    Implements IMyEventInterface
    Private myObject() As SomeClass

    Private Sub Form_Load()
        ReDim myObject(5)
        For i = 0 To 5
            Set myObject(i) = New SomeClass
            Set myObject(i).Callback = Me
            Set myObject(i).Wrapped = New WrappedClass
        Next i
    End Sub

    Private Sub IMyEventInterface_SomeEvent()
        ' event handler
    End Sub

    Hopefully that gives you the idea of how to handle events raised from arrays of objects, both for your own and for compiled classes. So that you can see I'm not spouting bilgewater I have also included a demonstration of the first technique. You will need an extraction program such as WinZip or 7-Zip to unzip the file, and then run the extracted project. I have commented the code so that you can (hopefully!) understand what's going on 


    Attached Files
    WithEvents for arrays, by Penagate.zip (3.8 KB, 331 views)
    展开全文
  • 今天,我们共同来学习如何把一个元素插入到数组中。 程序窗体界面程序实现功能:1、通过For循环对数组元素赋值;2、通过InputBox输入对话框输入要插入的数据;3、插入的数据与数组已有元素值比较,按照升序排列。...
  • 大家好,我们今日继续讲解VBA代码解决方案的第62讲内容:在VBA中如何使用动态数组,以及利用动态数组去除重复值的方法第二部分。在上一讲中,我们讲了知识点的利用,其一是Filter函数,其二是ReDim语句的利用价值。...
  • 大家好,我们今日继续讲解VBA代码解决方案的第62讲内容:在VBA中如何使用动态数组,以及利用动态数组去除重复值的方法第二部分。在上一讲中,我们讲了知识点的利用,其一是Filter函数,其二是ReDim语句的利用价值。...
  • 案例说明本案例实现了如何在删除数组中的任意位置的一个元素,使数组成为一个新的数组。效果如下图所示。关键技术由于对数组进行删除元素的操作,会减少数组的长度,因此数组必须定义为动态数组。如果是静态数组则...
  • VB编程中,数组占据着非常重要的地位。而数组的知识点又很多,让初学者非常容易混淆与难于掌握。比如数组定义数组的初始化、多维数组、动态数组、控件...接下来是一个VB的实际应用题目,我们共同来学习如何解...
  • VB2008】控件数组

    2012-03-15 17:33:50
    其实也很简单,因为vb.net和vb不同拉,来看看vb.net的动态控件数组如何实现。 1、在form的头部定义一个公共的ArrayList类型的变量   Dim m_items As New ArrayList 2、在程序中动态生成控件
  • vb.net动态控件数组

    千次阅读 2006-12-15 12:57:00
    vb.net里面的动态控件...其实也很简单,因为vb.net和vb不同拉,来看看vb.net的动态控件数组如何实现。 1、在form的头部定义一个公共的ArrayList类型的变量 Dim m_items As New ArrayList2、在程序中动态生成控件 
  • 要求数组在form2中定义数组,在form2没有关闭的范围内,这个数组就存在, 可以通过button的click事件等,来修改数组的内容!! __________________________________________________________________________ 我...
  • 前言 太多的例子讲如何如何实现VC编写动态链接库,VB中声明后使用,但没有一个好的文章来深入探讨这个问题,本文负责解决此类问题。 下载本文示例源代码 大小:125K 1.一般情况 VB定义为: Private Declare Function F....
  • VB编程中,数组占据着非常重要的地位。而数组的知识点又很多,让初学者非常容易混淆与难于掌握。比如数组定义数组的初始化、多维数组、动态数组、控件...接下来是一个VB的实际应用题目,我们共同来学习如何解...
  • 版权声明:本文为博主原创文章,转载...1、按照多个数组的大小重新定义一个新的数组,使用循环将旧数组的值写入新数组: Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click ...
  • Dim fileName As String ’定义了文件路径 Dim plainText() As Byte’用来存放照片二进制数据文件的数组 Open fileName For Binary As 1#’我已经写了用二进制打开文件 '现在循环就不知道怎么写了,想每次存256个...
  • 用vs2005,我在后台Default.aspx.vb文件中定义个结构体数组为public类型,定义如下:public structure struct1 dim str1 as string dim n as integerEnd structure定义数组public struct(2) as struct1付值:struct...
  • 在类中如下定义Public Sub Run(resultcir() As Single)MsgBox UBound(resultcir)resultcir(2) = 12345End Sub窗口中这样用:Private Sub Command1_Click()Dim a(9) As SingleDim aa As New Class1aa.Run a()MsgB
  • ATL或COM如何处理JS传递的数组

    千次阅读 2012-02-24 11:24:19
    最近碰到方正客户提出的一个问题,就是对于js定义数组传递给我们的接口(ATL控件),值设置不进去,一直返回false。 经过查找原因,发现控件代码只处理了vb脚本类的数组,而js数组要特殊处理vbscript传进来的是个...
  • VB定义可变长参数

    千次阅读 2008-10-21 17:03:00
    如何定义参数数量不确定的过程可以将“参数数组”声明为过程的参数列表中的最后一项。这允许过程接受参数的一组值,而不仅仅是一个值。定义过程时不必知道该组中值的数目。该组由对过程的每次调用单独确定,每次...
  • MsgBox "工艺问题定义不能为空!", vbOKOnly + vbCritical, "错误" End If End Sub![图片说明](https://img-ask.csdn.net/upload/202003/19/1584585696_195719.png)![图片说明]...
  • [原文地址: http://www.think400.dk/adhoc_4.htm#eks0015]问题:使用VB 或者C 我能够很容易的增加数组的大小而不丢失原来的数据,在RPG里面是否也可能呢?回答:当然可以,但是你得基于...*数组定义--基于指针Darra...
  • 在COM+中,如何传递一个Record,或一个Record数组? (可以传递ADO的记录集吗?好像VB可以,但VB传递的记录集,Delphi可以用吗?) 当然可以 看看一下代码 1。定义一个结构 ///////////////////自定义的欠费统计...
  • 控件数组VB提供的一个优秀的设计解决方案,它能很方便快捷的处理大批同类控件的响应和时间处理,但不知为什么在C#中这个优秀特性没有传承下来,甚为可惜,本文将要探讨就是如何在C# WinForm程序实现它.首先请看界面在...
  • 事件、接口、类---对象数组

    千次阅读 2014-02-09 21:10:10
    VB6 给对象数组如何定义事件? http://www.cnblogs.com/raylynn/archive/2007/01/29/632978.html 在编程的过程中,可以对一级有的类使用WithEvents关键字。 WithEvents,是在声明对象变量时,表明还希望使用该对象...
  • 控件数组VB提供的一个优秀的设计解决方案,它能很方便快捷的处理大批同类控件的响应和时间处理,但不知为什么在C#中这个优秀特性没有传承下来,甚为可惜,本文将要探讨就是如何在C# WinForm程序实现它.首先请看界面 在...

空空如也

空空如也

1 2 3 4 5 6
收藏数 102
精华内容 40
关键字:

vb如何定义数组