CWindow RC18

Started by José Roca, August 25, 2016, 09:56:54 PM

Previous topic - Next topic

José Roca

There was a mistake in the CWinHTTPRequest class and header. New file uploaded.

José Roca

An example for the dictionary object.


#include "Afx/CDicObj.inc"
using Afx

' // Creates an instance of the CDicObj class
DIM pDic AS CDicObj

' // Adds some key, value pairs
pDic.Add "a", "Athens"
pDic.Add "b", "Belgrade"
pDic.Add "c", "Cairo"

print "Count: "; pDic.Count
print pDic.Exists("a")
print

' // Retrieve an item and display it
print pDic.Item("b")
print

' // Change key "b" to "m" and "Belgrade" to "México"
pDic.Key("b") = "m"
pDic.Item("m") = "México"
print pDic.Item("m")
print

' // Get all the items and display them
DIM cvItems AS CVAR = pDic.Items
FOR i AS LONG = cvItems.GetLBound TO cvItems.GetUBound
   print cvItems.GetVariantElem(i)
NEXT

print

' // Get all the keys and display them
DIM cvKeys AS CVAR = pDic.Keys
FOR i AS LONG = cvKeys.GetLBound TO cvKeys.GetUBound
   print cvKeys.GetVariantElem(i)
NEXT

print

' // Remove key "m"
pDic.Remove "m"
IF pDic.Exists("m") THEN PRINT "Key m exists" ELSE PRINT "Key m doesn't exists"

' // Remove all keys
pDic.RemoveAll
print "All the keys must have been deleted"
print "Count: "; pDic.Count

PRINT
PRINT "Press any key..."
SLEEP


Marc Pons

an exemple of using the implicit cast capabilies of CVar class : in input or in output

#include once "Afx/CVar.inc"
using Afx


' ========================================================================================
' you can use implicit Casting in input/output for numeric values or string
' the casted output value  is depending on the type of the variable you assign the value
'
' if case of numeric type be carefull to use a variable type accepting the range of the supposed output
' if the value exceed the range of the var , the overflow will put 0 in var or negative value


'change here   with or without double quotes to see the differences
#define TESTVALUE -10001 'or test with  "  -10001 "    or - 10001 or "  +  10001 "  or + 10001 or whatever...


DIM cv AS CVar = TESTVALUE    'implicit casting in input can accept literal string/numeric

DIM l1 AS long = cv 'implicit casting in output
dim s1 as string = cv 'implicit casting in output

if s1 <> "" THEN
print : print "Test value = """ ; TESTVALUE;"""" : print
else
print : print "Test value = " ; TESTVALUE : print
END IF


if l1> 0 THEN
print "  l1 > 0 s1 = [" & s1 & "]   l1 = " & l1
elseif l1 = 0 THEN
print "  l1 = 0 s1 = [" & s1 & "]   l1 = " & l1
else
print "  l1 < 0 s1 = [" & s1 & "]   l1 = " & l1
END IF
PRINT
PRINT "Press any key..."
SLEEP


the beauty of that : the string representation of  numeric value assigned to CVar
can give the right value when assigning that Cvar to numeric var, just carefull on overflow

on the exemple : change the value on the define to see the effect
or change  DIM l1 AS long = cv   by different numeric type

Thanks again Jose for that huge code collection!

Richard Kelly

With all this good stuff, it's time to take a look at FB. Will FF ever catch up and include all the gadgets?

José Roca

#34
We are going to have the one datatype that was missing, safe arrays...

I already have implemented a class, CSafeArray, that works with arrays of any type and dimensions.


' // Two-dimensional array of BSTR
' // 1D: elements = 5, lower bound = 1
' // 1D: elements = 3, lower bound = 1
DIM rgsabounds(0 TO 1) AS SAFEARRAYBOUND = {(5, 1), (3, 1)}
DIM csa AS CSafeArray = CSafeArray(VT_BSTR, 2, @rgsabounds(0))
DIM cbs1 AS CBSTR = "Test string 1"
' // array index: first dimension, first element
DIM rgidx(0 TO 1) AS LONG = {1, 1}
' // Put the value
csa.PutElement(@rgidx(0), *cbs1)
' // Get the value
DIM cbsOut AS CBSTR
csa.GetElement(@rgidx(0), @cbsOut)
print cbsOut
' // array index: second dimension, first element
rgidx(0) = 2 : rgidx(0) = 1
' // Put the value
DIM cbs2 AS CBSTR = "Test string 2"
csa.PutElement(@rgidx(0), *cbs2)
' // Get the value
csa.GetElement(@rgidx(0), @cbsOut)
print cbsOut


Now I'm going to add overloaded functions to ease its use with one-dimensional arrays, like


' // One-dimensional array of VT_VARIANT
DIM csa AS CSafeArray = CSafeArray(VT_VARIANT, 1, 5)
DIM cv1 AS CVAR = "Test variant 1"
csa.PutElement(1, @cv1)
DIM cvOut AS CVAR
csa.GetElement(1, @cvOut)
print cvOut
DIM cv2 AS CVAR = "Test variant 2"
csa.PutElement(1, @cv2)
csa.GetElement(1, @cvOut)
print cvOut


And add overloaded functions to GetElement and PutElement.

In COM programming, about 90% of the safe arrays are one-dimensional. I only have seen a two-dimensional safe array in the GetRows method of ADO.

Safe vectors are not-resizable one-dimensional safe arrays allocated in contiguous memory for efficiency.

José Roca

I'm not satisfied with the name of CVar for the variant class and I'm going to change it to CVariant.

José Roca

#36
I have changed CVAR to CVARIANT and have finished the SAFEARRAY class.

Tomorrow I will post a new release candidate package and an updated help file.

The safe arrays can be multidimensional and there are overloaded functions to ease the use of the one and to dimensional safe arrays.

Usage example of a one-dimensional array of variants:


' ========================================================================================
' CSafeArray test
' ========================================================================================

#include "Afx/CSafeArray.inc"
using Afx

' // One-dimensional array of VT_VARIANT
DIM csa AS CSafeArray = CSafeArray(VT_VARIANT, 1, 2)
DIM cv1 AS CVARIANT = "Test variant 1"
csa.PutElement(1, @cv1)
DIM cvOut AS CVARIANT
csa.GetElement(1, @cvOut)
print cvOut

DIM cv2 AS CVARIANT = "Test variant 2"
csa.PutElement(1, @cv2)
csa.GetElement(1, @cvOut)
print cvOut

' // Redim (preserve) the safe array
csa.Redim(1, 3)
DIM cv3 AS CVARIANT = "Test variant 3"
csa.PutElement(3, @cv3)
csa.GetElement(3, @cvOut)
print cvOut

PRINT
PRINT "Press any key..."
SLEEP


Usage example of a two-dimensional safe array of BSTRs.


' ========================================================================================
' CSafeArray test
' ========================================================================================

#include "Afx/CSafeArray.inc"
using Afx

' // Two-dimensional array of BSTR
' // 1D: elements = 5, lower bound = 1
' // 1D: elements = 3, lower bound = 1
DIM rgsabounds(0 TO 1) AS SAFEARRAYBOUND = {(5, 1), (3, 1)}
DIM csa AS CSafeArray = CSafeArray(VT_BSTR, 2, @rgsabounds(0))
' or we can use:
' DIM csa AS CSafeArray = CSafeArray(VT_BSTR, 5, 1, 3, 1)

' // array index: first element, first dimension
DIM rgidx(0 TO 1) AS LONG = {1, 1}
' // Put the value
DIM cbs1 AS CBSTR = "Test string 1.1"
csa.PutElement(@rgidx(0), *cbs1)

' // array index: second element, first dimension
rgidx(0) = 2 : rgidx(1) = 1
' // Put the value
DIM cbs2 AS CBSTR = "Test string 2.1"
csa.PutElement(@rgidx(0), *cbs2)

' // array index: first element, second dimension
rgidx(0) = 1 : rgidx(1) = 2
' // Put the value
DIM cbs3 AS CBSTR = "Test string 1.2"
csa.PutElement(@rgidx(0), *cbs3)

' // array index: second element, second dimension
rgidx(0) = 2 : rgidx(1) = 2
' // Put the value
DIM cbs4 AS CBSTR = "Test string 2.2"
csa.PutElement(@rgidx(0), *cbs4)

DIM hr AS HRESULT, cbsOut AS CBSTR
hr = csa.GetElement(1, 1, @cbsOut)
print cbsOut
hr = csa.GetElement(2, 1, @cbsOut)
print cbsOut
print

hr = csa.GetElement(1, 2, @cbsOut)
print cbsOut
hr = csa.GetElement(2, 2, @cbsOut)
print cbsOut

PRINT
PRINT "Press any key..."
SLEEP


For the two-dimensional arrays in the example above, we can use


csa.PutElement(1, 1, *cbs1)
csa.PutElement(2, 1, *cbs2)


instead of

DIM rgidx(0 TO 1) AS LONG = {1, 1}
-or-
rgidx(0) = 2 : rgidx(1) = 1