Jump to content

Declare An Array With Out Setting Bounds?


Recommended Posts

Been doing that whole COM thing you know ....

Anyways after more investigating I found that in VBA I can declare a varible to be an array of some datatype, but I don't have to set the bounds.

This is important as the call I need returns two arrays byref. Before they can be passed to the interface they have to be set but can not limited.

So Can I declare an array like vba?

Dim Array() As String ..... Maybe not the datatype but the array declaration?

When I try that syntax in AutoIT I get an error.

Link to comment
Share on other sites

Been doing that whole COM thing you know ....

Anyways after more investigating I found that in VBA I can declare a varible to be an array of some datatype, but I don't have to set the bounds.

This is important as the call I need returns two arrays byref. Before they can be passed to the interface they have to be set but can not limited.

So Can I declare an array like vba?

Dim Array() As String ..... Maybe not the datatype but the array declaration?

When I try that syntax in AutoIT I get an error.

Hi,

I have used local $ar_Array[1]; the "1" is just a dummy, and "byref" re-defines it anyway (eg in ExcelCom.au3)

Best, Randall

Link to comment
Share on other sites

Hi,

I have used local $ar_Array[1]; the "1" is just a dummy, and "byref" re-defines it anyway (eg in ExcelCom.au3)

Best, Randall

K I don't know how U are able to define an array like that I keep getting errors saying it is too small a declaration

Also how does one Dim or ReDim the array then to not have a predefined bounds?

If I pass an array that has bounds set I get Type Mismatch errors

In Vba if I don't set the bounds it works ....

Link to comment
Share on other sites

  • Developers

The Helpfile explains how Dim/Redim arrays work..

Maybe you need to show a scriptlet that demos what you want and shows the error ...

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

#Include <Array.au3>
Dim $Errors
;ReDim $Errors[1]
Dim $ItemIDs[2]
Dim $ClientHandles[2]
Dim $ServerHandles
Dim $Values[20]


$debug = 0

$string =""


$oMyError = ObjEvent("AutoIt.Error","MyErrFunc"); Install a custom error handler 
$strServerName = ("Matrikon.OPC.DesktopHistorian.1")


;ByRefTest($Values,$Errors)
For $Value In $Values
    $string=$string & $Value
Next
MsgBox(4096,"ByRef Test Values", $string)
$string=""
For $Error In $Errors
    $string=$string & $Error
Next
MsgBox(4096,"ByRef Test Errors", $string)




$oHDA=ObjCreate("Matrikon.OPCHDA.Automation.1")

Dim $OPCHistory = $oHDA.OPCHDAHistory

If ($debug) Then
$string =""
$Props = ObjectGetProperties($oHDA)
For $Prop In $Props
    $string = $string & $prop & @CR
Next
MsgBox (4096,"Test",$string)
EndIf


$testID=$oHDA.Connect($strServerName,"localhost")
$ServerName = $oHDA.ServerName
MsgBox(4096,"Connected to ...",$ServerName)  


$OPCItems = $oHDA.OPCHDAItems

If ($debug) Then
$Props = ObjectGetProperties($OPCItems)
$string = ""
For $Prop In $Props
    $string = $string & $prop & @CR
Next
MsgBox (4096,"OPCItems",$string)
EndIf

$OPCBrowser = $oHDA.CreateBrowser()

If ($debug) Then
$Props = ObjectGetProperties($OPCBrowser)
$string = ""
For $Prop In $Props
    $string = $string & $prop & @CR
Next
MsgBox (4096,"OPCBrowser",$string)
EndIf

$OPCItems.AddItem('Random.Int1',0)
$OPCItem1=$OPCItems.Item ('Random.Int1')

If ($debug) Then
$Props = ObjectGetProperties($OPCItem1)
$string = ""
For $Prop In $Props
    $string = $string & $prop & @CR
Next
MsgBox (4096,"OPCItem1",$string)
EndIf

$ItemIDs[0] = 'Random.Int2'
$ItemIDs[1] = 'Random.Int4'
;Hmmmm
$ItemID = 'Random.Int2'
Dim $Error
$ClientHandles[0] = 1
$ClientHandles[1] = 2

$NumItems=2

MsgBox(4096,"Validating...", "Validation")
;++++++++++++++Type Mismatch Here
$OPCItems.Validate($NumItems,$ItemIDs,$Errors)

;++++++++++++++Type Mismatch Here
MsgBox(4096, "AddItems", "Adding");================I==========I
$OPCItems.AddItems($NumItems,$ItemIDs,$ClientHandles,$ServerHandles,$Errors)

$Count=$OPCItems.Count
MsgBox(4096,"OPCItem1",$OPCItem1)
MsgBox(4096,"Item Count",$Count)
$NumItems = 1


For $OPCItem In $OPCItems
    $ServerHandles=$OPCItem.ServerHandle
    $ClientHandle =$OPCItem1.ClientHandle
    MsgBox(4096,"OBJName",ObjName($OPCItem))
    MsgBox(4096,"Server Handle",$ServerHandles)
    MsgBox(4096,"Client Handle",$ClientHandle)
Next
$OPCItem1 = $OPCItems.GetOPCHDAItem($ServerHandles)
$Values=$OPCItem1.ReadRaw("NOW-50D","NOW")

If ($debug) Then
$Props = ObjectGetProperties($Values)
$string = ""
For $Prop In $Props
    $string = $string & $prop & @CR
Next
MsgBox (4096,"Test",$string)
EndIf

For $Value In $Values
    MsgBox(4096,"Value",$Value)
Next
MsgBox(4096,"Value",$Values)

;ByRefTest($Values,$Errors)
MsgBox(4096,"Testing",$Values & " " & $Errors)

;++++++++++This a call with the problem+++++++++++++++++
$OPCItems.SyncReadRaw ('NOW-5D', 'NOW', 3, true, 1, $ServerHandles,$Values,$Errors)


; This is my custom error handler 
Func MyErrFunc() 
   $HexNumber=hex($oMyError.number,8) 
   Msgbox(0,"","We intercepted a COM Error !" & @CRLF & _
                "Number is: " & $HexNumber & @CRLF & _
                "Windescription is: " & $oMyError.windescription ) 

   SetError(1); something to check for when this function returns 
EndFunc




Func ObjectGetProperties($oObj)
;DESCRIPTION:   Retrieves the description of all writable properties of the given Object.
;REQUIRES:      tlbinf32.dll  (distributed with Visual Studio)
;PARAMETERS:    $oApp - Pointer to an active object
;DATE:          2006-01-29
;AUTHOR:        SvenP
;CREDITS:       To Dale Holm
 Dim $oTLA, $oTLI, $TLIMember, $aProperties[1]
 Const $cINVOKE_FUNC=1          
; A method (=function)
 Const $cINVOKE_PROPERTYGET=2   
; Read-only property
 Const $cINVOKE_PROPERTYPUT=4   
; Read/write property
 Const $cINVOKE_PROPERTYPUTREF=8    
; Reference property
 if not IsObj($oObj) then Return
 $oTLA   = ObjCreate("TLI.TLIApplication")
; oTLA is a _TLIApplication interface
 $oTLI   = $oTLA.InterfaceInfoFromObject($oObj) 
; oTLI is a InterfaceInfo interface
 For $TLIMember in $oTLI.Members              
; This interface contains both methods and properties   
;if $TLIMember.InvokeKind=$cINVOKE_PROPERTYGET then
; Get only WRITABLE prperties       
 Redim $aProperties[UBound($aProperties)+1] 
; We don't know the number of properties in advance     
 $aProperties[UBound($aProperties)-1]=$TLIMember.Name
; Add the name of the property to array.    
;EndIf
 Next
 return $aProperties
 EndFunc

Link to comment
Share on other sites

Link to comment
Share on other sites

You put it in your loop, and whenever you are about to add an element to the array, you redim the array to ($whatever+1), or as big as you need. The only downside is that it's slower...

In a recent app of mine I needed to hold a large array of values which changed dynamically with each run. I tried a) creating an array of 500000 elements, filling a few thousand of them, then flipping the array to a string and back to an array (to remove the extra elements) for the return array, which seems like a lot of work, but turns out to be waaay faster than B) dynamically creating the array as the routine went along using redim.

For a small array, dynamic creation is good, but for large arrays, simply define a HUGE array, and when you're done, lop off any bits you don't need.

-mu

Link to comment
Share on other sites

I think you are missunderstanding the problem.

I don't need to redim the array .... the COM call with set the bounds and return it byref

So In VBA, I declare an Array without setting bounds. I then pass if byref into the COM call.

The COM Object wants a undefined array.

Link to comment
Share on other sites

Are you saying that your trouble is that the SyncReadRaw method is expecting the $Values and $Errors parameters to be arrays passed byRef?

Are the manipulated values of those variables important to you after the method returns? If not you might experiment with passing string variables instead of arrays to see if it will eat them or not. There are some nuances with byRef values in the COM implementation (for example, it is not allowed to use byRef in an ObjEvent function).

Dale

Edit: typo

Edited by DaleHohm

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y

Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Link to comment
Share on other sites

Are you saying that your trouble is that the SyncReadRaw method is expecting the $Values and $Errors parameters to be arrays passed byRef?

Are the manipulated values of those variables important to you after the method returns? If not you might experiment with passing string variables instead of arrays to see if it will eat them or not. There are some nuances with byRef values in the COM implementation (for example, it is not allowed to use byRef in an ObjEvent function).

Dale

Edit: typo

That is correct. And I do need access to the $Values array the $Errors is not that important. I have attempted damn near every combination in VBA and AutoIT and the only method that worked was in VBA where I do not set the bounds of the array but let the call ReDim the Array.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...