List Info

Thread: Variant vs other types...




Variant vs other types...
user name
2007-01-27 13:04:21
Hi!

As usual I don't have a clue about anything when it comes to
OpenOffice
Basic "programming".

I get an error message that I don't understand. Well,
actually I understand
it, but I don't understand exactly why I get it.

Here is approximately what I do:

Sub SkrivUtstamp
    Dim Dag As Date, Veckodag As Integer
    Dim Instamp As Double, Utstamp As Double
    Dim OBDef As Variant, OB(5) As Double
    Dim r As Integer, k As Integer

   {A few lines not relevant to my problem}

' Sorry for that some variable names are in Swedish...
    Dag=Sheet.getCellByPosition(0,Rad).getValue()
    Veckodag=Weekday(Dag)-1
    If Veckodag=0 Then Veckodag=7

'    The line below will make OBDef an array - OBDef(2,5).
In this case
OBDef(2,x) will be String and the rest will be Double.
   
OBDef=DataSheet.getCellRangeByPosition(4,1,9,3).getDataArray
()

    For k=0 To 5
        OB(k)=OverTime(OBDef(2,k), Veckodag, Instamp,
Utstamp, OBDef(0,k),
OBDef(1,k)) 'This line doesn't work.
Rem OB(k)=OverTime("OB11",3,0.25,1,0.5,0.8) ' This
example-line would work
if not "Rem" was there.
    Next k
  {Yet another couple of unimportant lines, when it comes to
describing my
problem}
Sub

' There is nothing wrong with the function below, it works
when I just feed
it with simple variables... When I do like I did above, I
get an error
message at the very first line of this function below. It
says something
like (free translation from Swedish, sorry) "The object
variable is not
defined":

Function OverTime(TypeOfOB As String, DayOfWeek As Integer,
StartTime As
Double, EndTime As Double, OTStart As Double, OTEnd As
Double) As Double
    Dim Factor As Double

    Factor=1
    If TypeOfOB="ÖT75" Or
TypeOfOB="ÖT100" Then
        If DayOfWeek<6 Then
            Factor=0
        EndIf
    Else
        If DayOfWeek>5 Then
            Factor=0
        EndIf
    EndIf
    If StartTime<OTStart Then StartTime=OTStart
    If EndTime>OTEnd Then EndTime=OTEnd
    If StartTime>EndTime Then StartTime=EndTime

    OverTime=Factor*(EndTime-StartTime)
End Function

So what does all this mean? What should I do instead?

Kind regards

Johnny
Re: Variant vs other types...
user name
2007-01-28 09:46:00
Maybe I should put it another way:

Let's say that I have these lines, among others:


    Dim Ctl As Object
    Dim Doc As Object, Sheet As Object

    Dim A As String
    Dim  OBDef As Double ' Or whatever, it doesn't seem to
matter in this
case, see further below...

    Doc=ThisComponent
    Sheet=Doc.Sheets.getByName("Blah")

   
OBDef=DataSheet.getCellRangeByPosition(4,1,9,3).getDataArray
() ' Now
OBDef seems to change to Variant.
    ' By the way, (4,1,9,3) is "E2:J4". Row 2 and
3 in that spreadsheet
contains numbers, row 4 contains text.

    A=OBDef(2,0) ' This doesn't work.

Okay, so far, no good.
When I look at the variables with "Övervakaren"
(in Swedish, I don't have a
clue what it might be called in English, but I am talking
about the tool
that helps me to see the content of variables while I step
through the
macro, the button that allows me to select variables to
watch in the Basic
API thing, has a pair of glasses on it and F7 does the same
thing), it seems
like OBDef(2)(0) is a Variant/String. What's the difference
between
Variant/String and just String?


So my question is really something like this:

How do I, in this meaningless example, make the content of
OBDef(2,0), which
is a String variable, originally some text in cell E4 in
spreadsheet "Blah",
to be copied to A, which is an ordinary String?

What I would like to achieve is to copy a CellRange to a
2-dimensional
variable. Since some rows contains numbers and other
contains text I though
that the type "Variant" could be useable, but
obviously not...

No I really have to copy the CellRange cell by cell?

Another solution would perharps be to create a struct (as
it's called in C),
but is that possible in OpenOffice.org Basic?

Johnny
Re: Re: Variant vs other types...
user name
2007-01-28 19:26:14
Johnny Andersson wrote:
> Maybe I should put it another way:
>
> Let's say that I have these lines, among others:
>
>
>    Dim Ctl As Object
>    Dim Doc As Object, Sheet As Object
>
>    Dim A As String
>    Dim  OBDef As Double ' Or whatever, it doesn't seem
to matter in this
> case, see further below...
>
>    Doc=ThisComponent
>    Sheet=Doc.Sheets.getByName("Blah")
>
>   
OBDef=DataSheet.getCellRangeByPosition(4,1,9,3).getDataArray
() ' Now
> OBDef seems to change to Variant.

You have a Calc sheet and you return a cell range based on
its position.
Next, you call getDataArray() on the cell range.

The "official" return type is sequence<
sequence< any > >, which means 
an array of arrays of variants. My opinion is that this
should cause an 
error because an array of arrays of any should not be
assigned to a 
double unless the type can be "coerced" or
"cast" into the double type. 
so, you found a but (that has been around a long time) in
StarBasic. I 
rarely think about it because it has never caused me any
problems. There 
might be some reason for the behavior but I am not aware of
the reasons.

If you wanted the specific values, i expect you to write
something like 
this:

Dim oData()
Dim oRow()
Dim i%, j%
Dim s$

s = ""
oData() =
DataSheet.getCellRangeByPosition(4,1,9,3).getDataArray()
For i=LBound(oData()) To UBound(oData())
  oRow() = oData(i)
  For j = LBound(oRow()) To UBound(oRow())
    s = s & " " & CStr(oRow(j))
  Next
  s = s & CHR$(10)
Next
MsgBox s

>    ' By the way, (4,1,9,3) is "E2:J4". Row 2
and 3 in that spreadsheet
> contains numbers, row 4 contains text.
>
>    A=OBDef(2,0) ' This doesn't work.
See above, it is an array of arrays, this is not a two
dimensional array.
>
> Okay, so far, no good.
> When I look at the variables with
"Övervakaren" (in Swedish, I don't 
> have a
> clue what it might be called in English, but I am
talking about the tool
> that helps me to see the content of variables while I
step through the
> macro, the button that allows me to select variables to
watch in the 
> Basic
> API thing, has a pair of glasses on it and F7 does the
same thing), it 
> seems
> like OBDef(2)(0) is a Variant/String. What's the
difference between
> Variant/String and just String?
A variant can hold any type. So, you have a variant that
references a 
string.
> So my question is really something like this:
>
> How do I, in this meaningless example, make the content
of OBDef(2,0), 
> which
> is a String variable, originally some text in cell E4
in spreadsheet 
> "Blah",
> to be copied to A, which is an ordinary String?
Does th example shown above help? If not...
oRow() = oBDef(2)
A = oRow(0)
> What I would like to achieve is to copy a CellRange to
a 2-dimensional
> variable. Since some rows contains numbers and other
contains text I 
> though
> that the type "Variant" could be useable, but
obviously not...
>
> No I really have to copy the CellRange cell by cell?
What do you really want to accomplish? Do you want to copy a
range of 
data from one location to another? If so, you can use
methods to 
directly copy the data. I have used "cheat"
methods such as

oCellRange1.setData(oCellRange2.getData())

>
> Another solution would perharps be to create a struct
(as it's called 
> in C),
> but is that possible in OpenOffice.org Basic?
Yes, you can create a user defined structure/type. To quote
from my 
AndrewMacro.odt document.


 1.1. User Defined Data Types
As of OOo 1.1.1, you can define your own data types.
Listing 5.51: You can define your own data types.
Type PersonType
  FirstName As String
  LastName As String
End Type

Sub ExampleCreateNewType
  Dim Person As PersonType
  Person.FirstName = "Andrew"
  Person.LastName  = "Pitonyak"
  PrintPerson(Person)
End Sub

Sub PrintPerson(x)
  Print "Person = " & x.FirstName & "
" & x.LastName
End Sub
I gave a presentation at the 2004 OOo Conference in Berlin
concerning 
creating advanced data types using structures. The examples
are in the 
presentation available on my web site.

>
> Johnny
>

-- 
Andrew Pitonyak
My Macro Document: http://www.pi
tonyak.org/AndrewMacro.odt
My Book: http://w
ww.hentzenwerke.com/catalog/oome.htm
Info:  http://www.pitonyak.or
g/oo.php
See Also: http://documentation.openoffice.org/HOW_TO/index.html

------------------------------------------------------------
---------
To unsubscribe, e-mail: dev-unsubscribeapi.openoffice.org
For additional commands, e-mail: dev-helpapi.openoffice.org


Re: Re: Variant vs other types...
user name
2007-01-29 05:33:17
Thanks for your interesting reply!

Parts of original messages and comments and maybe more
questions below:

2007/1/29, Andrew Douglas Pitonyak <andrewpitonyak.org>:
>
> Johnny Andersson wrote:
> > Maybe I should put it another way:
> >
> > Let's say that I have these lines, among others:
> >
> >
> >    Dim Ctl As Object
> >    Dim Doc As Object, Sheet As Object
> >
> >    Dim A As String
> >    Dim  OBDef As Double ' Or whatever, it doesn't
seem to matter in this
> > case, see further below...
> >
> >    Doc=ThisComponent
> >    Sheet=Doc.Sheets.getByName("Blah")
> >
> >   
OBDef=DataSheet.getCellRangeByPosition(4,1,9,3).getDataArray
() ' Now
> > OBDef seems to change to Variant.
>
> You have a Calc sheet and you return a cell range based
on its position.
> Next, you call getDataArray() on the cell range.
>
> The "official" return type is sequence<
sequence< any > >, which means
> an array of arrays of variants. My opinion is that this
should cause an
> error because an array of arrays of any should not be
assigned to a
> double unless the type can be "coerced" or
"cast" into the double type.
> so, you found a but (that has been around a long time)
in StarBasic. I
> rarely think about it because it has never caused me
any problems. There
> might be some reason for the behavior but I am not
aware of the reasons.


So it is an array of arrays? That explains what happens when
I copy it to a
variable: The variable is shown as MyVariable(x)(y) rather
than
MyVariable(x,y) in the "Basic debugger" (I called
it "Övervakaren"
before, I just found out that it's actually
"Bevakaren", but in this case
it's just about the same thing).

If you wanted the specific values, i expect you to write
something like
> this:
>
> Dim oData()
> Dim oRow()
> Dim i%, j%
> Dim s$
>
> s = ""
> oData() =
DataSheet.getCellRangeByPosition(4,1,9,3).getDataArray()
> For i=LBound(oData()) To UBound(oData())
>   oRow() = oData(i)
>   For j = LBound(oRow()) To UBound(oRow())
>     s = s & " " & CStr(oRow(j))
>   Next
>   s = s & CHR$(10)
> Next
> MsgBox s
>

> What do you really want to accomplish? Do you want to
copy a range of
> data from one location to another? If so, you can use
methods to
> directly copy the data. I have used "cheat"
methods such as
>
> oCellRange1.setData(oCellRange2.getData())


No, a function will use the data (and other data) for
calculation. The
function will return a value that will be written to another
cell. The
function will be in a loop and use three of the values (one
column), different every time.

>
> > Another solution would perharps be to create a
struct (as it's called
> > in C),
> > but is that possible in OpenOffice.org Basic?
> Yes, you can create a user defined structure/type. To
quote from my
> AndrewMacro.odt document.
>
>
> 1.1. User Defined Data Types
> As of OOo 1.1.1, you can define your own data types.
> Listing 5.51: You can define your own data types.
> Type PersonType
>   FirstName As String
>   LastName As String
> End Type
>
> Sub ExampleCreateNewType
>   Dim Person As PersonType
>   Person.FirstName = "Andrew"
>   Person.LastName  = "Pitonyak"
>   PrintPerson(Person)
> End Sub
>
> Sub PrintPerson(x)
>   Print "Person = " & x.FirstName &
" " & x.LastName
> End Sub


Wow, I didn't know that! Now I have to rewrite everything
macro and
every function I've made so far..!
They are not that many, on the other hand...

In your example above, is the following possible?

Sub ExampleCreateNewType
  Dim Person(100) As PersonType
  Person(0).FirstName = "Andrew"
  Person(0).LastName  = "Pitonyak"
  PrintPerson(Person(0))
End Sub


And is the following possible?

Type PersonType
  FirstName As String
  LastName As String
  ThingsToDo(9) As String
End Type

And finally, is the following possible?

Type PersonType
  FirstName As String
  LastName As String
End Type

Type Human
  Properties As PersonType
  Friends(9) As PersonType
End Type


And even more finally, is there something similar to
pointers in
OpenOffice.org Basic?

I gave a presentation at the 2004 OOo Conference in Berlin
concerning
> creating advanced data types using structures. The
examples are in the
> presentation available on my web site.


Interesting! I'd better take a look there then!

Thanks for your reply!

Johnny
Re: Re: Variant vs other types...
user name
2007-01-29 18:00:18

Johnny Andersson wrote:
>
> 2007/1/29, Andrew Douglas Pitonyak <andrewpitonyak.org>:
>> What do you really want to accomplish? Do you want
to copy a range of
>> data from one location to another? If so, you can
use methods to
>> directly copy the data. I have used
"cheat" methods such as
>>
>> oCellRange1.setData(oCellRange2.getData())
> No, a function will use the data (and other data) for
calculation. The
> function will return a value that will be written to
another cell. The
> function will be in a loop and use three of the values
(one
> column), different every time.
Very good. I assume that you now know how to do this.
>>
>> > Another solution would perharps be to create a
struct (as it's called
>> > in C),
>> > but is that possible in OpenOffice.org Basic?
>> Yes, you can create a user defined structure/type.
To quote from my
>> AndrewMacro.odt document.
>>
>>
>> 1.1. User Defined Data Types
>> As of OOo 1.1.1, you can define your own data
types.
>> Listing 5.51: You can define your own data types.
>> Type PersonType
>>   FirstName As String
>>   LastName As String
>> End Type
>>
>> Sub ExampleCreateNewType
>>   Dim Person As PersonType
>>   Person.FirstName = "Andrew"
>>   Person.LastName  = "Pitonyak"
>>   PrintPerson(Person)
>> End Sub
>>
>> Sub PrintPerson(x)
>>   Print "Person = " & x.FirstName
& " " & x.LastName
>> End Sub
>
>
> Wow, I didn't know that! Now I have to rewrite
everything macro and
> every function I've made so far..!
> They are not that many, on the other hand...
>
> In your example above, is the following possible?
>
> Sub ExampleCreateNewType
>  Dim Person(100) As PersonType
>  Person(0).FirstName = "Andrew"
>  Person(0).LastName  = "Pitonyak"
>  PrintPerson(Person(0))
> End Sub
Try it, yes, it works
> And is the following possible?
>
> Type PersonType
>  FirstName As String
>  LastName As String
>  ThingsToDo(9) As String
> End Type
No, you can NOT have an array inside of a struct. You need
to fake it:

Type PersonType
  FirstName As String
  LastName As String
 
  REM You can NOT have an array inside a structure
  REM ThingsToDo(9) As String
  REM But you can fake it...
  ThingsToDo As Variant
End Type

Sub ExampleCreateNewType
  Dim Person As PersonType
  Dim aa(9) As String
 
  Person.FirstName = "Andrew"
  Person.LastName  = "Pitonyak"
  Person.ThingsToDo = aa()
  Print UBound(Person.ThingsToDo)  REM Prints 9
  Person.ThingsToDo = DimArray(9)
  Print UBound(Person.ThingsToDo)  REM Prints 9
  'PrintPerson(Person)
End Sub
>
> And finally, is the following possible?
>
> Type PersonType
>  FirstName As String
>  LastName As String
> End Type
>
> Type Human
>  Properties As PersonType
>  Friends(9) As PersonType
> End Type
You can not have an array inside of a struct. A struct in a
struct, 
however, is fine.
> And even more finally, is there something similar to
pointers in
> OpenOffice.org Basic?
I demonstrate this in the presentation I referenced.
> I gave a presentation at the 2004 OOo Conference in
Berlin concerning
>> creating advanced data types using structures. The
examples are in the
>> presentation available on my web site.
>
>
> Interesting! I'd better take a look there then!
>
> Thanks for your reply!
>
> Johnny
>

-- 
Andrew Pitonyak
My Macro Document: http://www.pi
tonyak.org/AndrewMacro.odt
My Book: http://w
ww.hentzenwerke.com/catalog/oome.htm
Info:  http://www.pitonyak.or
g/oo.php
See Also: http://documentation.openoffice.org/HOW_TO/index.html

------------------------------------------------------------
---------
To unsubscribe, e-mail: dev-unsubscribeapi.openoffice.org
For additional commands, e-mail: dev-helpapi.openoffice.org


Re: Re: Variant vs other types...
user name
2007-01-30 01:34:24
Thanks for your reply again! I will definitely take a look
on that
presentation.

Have a great day!

Johnny Andersson

2007/1/30, Andrew Douglas Pitonyak <andrewpitonyak.org>:
>
>
>
> Johnny Andersson wrote:
> >
> > 2007/1/29, Andrew Douglas Pitonyak <andrewpitonyak.org>:
> >> What do you really want to accomplish? Do you
want to copy a range of
> >> data from one location to another? If so, you
can use methods to
> >> directly copy the data. I have used
"cheat" methods such as
> >>
> >> oCellRange1.setData(oCellRange2.getData())
> > No, a function will use the data (and other data)
for calculation. The
> > function will return a value that will be written
to another cell. The
> > function will be in a loop and use three of the
values (one
> > column), different every time.
> Very good. I assume that you now know how to do this.
> >>
> >> > Another solution would perharps be to
create a struct (as it's called
> >> > in C),
> >> > but is that possible in OpenOffice.org
Basic?
> >> Yes, you can create a user defined
structure/type. To quote from my
> >> AndrewMacro.odt document.
> >>
> >>
> >> 1.1. User Defined Data Types
> >> As of OOo 1.1.1, you can define your own data
types.
> >> Listing 5.51: You can define your own data
types.
> >> Type PersonType
> >>   FirstName As String
> >>   LastName As String
> >> End Type
> >>
> >> Sub ExampleCreateNewType
> >>   Dim Person As PersonType
> >>   Person.FirstName = "Andrew"
> >>   Person.LastName  = "Pitonyak"
> >>   PrintPerson(Person)
> >> End Sub
> >>
> >> Sub PrintPerson(x)
> >>   Print "Person = " &
x.FirstName & " " & x.LastName
> >> End Sub
> >
> >
> > Wow, I didn't know that! Now I have to rewrite
everything macro and
> > every function I've made so far..!
> > They are not that many, on the other hand...
> >
> > In your example above, is the following possible?
> >
> > Sub ExampleCreateNewType
> >  Dim Person(100) As PersonType
> >  Person(0).FirstName = "Andrew"
> >  Person(0).LastName  = "Pitonyak"
> >  PrintPerson(Person(0))
> > End Sub
> Try it, yes, it works
> > And is the following possible?
> >
> > Type PersonType
> >  FirstName As String
> >  LastName As String
> >  ThingsToDo(9) As String
> > End Type
> No, you can NOT have an array inside of a struct. You
need to fake it:
>
> Type PersonType
>   FirstName As String
>   LastName As String
>
>   REM You can NOT have an array inside a structure
>   REM ThingsToDo(9) As String
>   REM But you can fake it...
>   ThingsToDo As Variant
> End Type
>
> Sub ExampleCreateNewType
>   Dim Person As PersonType
>   Dim aa(9) As String
>
>   Person.FirstName = "Andrew"
>   Person.LastName  = "Pitonyak"
>   Person.ThingsToDo = aa()
>   Print UBound(Person.ThingsToDo)  REM Prints 9
>   Person.ThingsToDo = DimArray(9)
>   Print UBound(Person.ThingsToDo)  REM Prints 9
>   'PrintPerson(Person)
> End Sub
> >
> > And finally, is the following possible?
> >
> > Type PersonType
> >  FirstName As String
> >  LastName As String
> > End Type
> >
> > Type Human
> >  Properties As PersonType
> >  Friends(9) As PersonType
> > End Type
> You can not have an array inside of a struct. A struct
in a struct,
> however, is fine.
> > And even more finally, is there something similar
to pointers in
> > OpenOffice.org Basic?
> I demonstrate this in the presentation I referenced.
> > I gave a presentation at the 2004 OOo Conference
in Berlin concerning
> >> creating advanced data types using structures.
The examples are in the
> >> presentation available on my web site.
> >
> >
> > Interesting! I'd better take a look there then!
> >
> > Thanks for your reply!
> >
> > Johnny
> >
>
> --
> Andrew Pitonyak
> My Macro Document: http://www.pi
tonyak.org/AndrewMacro.odt
> My Book: http://w
ww.hentzenwerke.com/catalog/oome.htm
> Info:  http://www.pitonyak.or
g/oo.php
> See Also: http://documentation.openoffice.org/HOW_TO/index.html
>
>
------------------------------------------------------------
---------
> To unsubscribe, e-mail: dev-unsubscribeapi.openoffice.org
> For additional commands, e-mail: dev-helpapi.openoffice.org
>
>
[1-6]

about | contact  Other archives ( Real Estate discussion Medical topics )