OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

How to Convert String to Object?

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
adamzad
General User
General User


Joined: 08 Mar 2011
Posts: 16
Location: Buena Park, CA, USA

PostPosted: Fri Apr 01, 2011 4:39 pm    Post subject: How to Convert String to Object? Reply with quote

Greetings Fellow OO Mavens,
Here's an interesting dilema:
Is it possible to convert a string variable into an object variable?

I know it's possible to convert, thanks to the OpenOffice Basic Guide's list of "C" Functions:
CStr(Variable) - converts any data type into a string.
CInt(Variable) - converts any data type into an integer value.
CLng(Variable) - converts any data type into a long integer value.
CSng(Variable) - converts any data types into a single precision value.
CDbl(Variable) - converts any data types into a double precision value.
CBool(Variable) - converts any data type into a Boolean value.
CDate(Variable) - converts any data types into a date value.

But there isn't one to convert to an Object. CObj(Variable) doesn't work. Sad

Fair day and be well,
Adam-Zad, The Bear that Looks Like a Man
Back to top
View user's profile Send private message
probe1
Moderator
Moderator


Joined: 18 Aug 2004
Posts: 2560
Location: Chonburi Thailand Asia

PostPosted: Fri Apr 01, 2011 5:33 pm    Post subject: Re: How to Convert String to Object? Reply with quote

adamzad wrote:
Is it possible to convert a string variable into an object variable?


What for?
_________________
Cheers
Winfried
My Macros
DateTime2 extension: insert date, time or timestamp, formatted to your needs
Back to top
View user's profile Send private message Visit poster's website
adamzad
General User
General User


Joined: 08 Mar 2011
Posts: 16
Location: Buena Park, CA, USA

PostPosted: Fri Apr 01, 2011 6:51 pm    Post subject: Reply with quote

I almost said, "My own education" but the real reason is that I want to click a button, get an input box, type in a path (e.g., oEv.Source.Model.Parent), and XRay that component. I've made the script loop, so I can enter multiple paths and XRay them each in turn. Problem is, InputBox() returns a string, so you get an "Invalid Use of Object" error if you try to Dim the variable as an object, and I haven't been able to "copy" the string (or variant) variable path to an object variable. Ultimately, in a way, it really IS for my own education.
Back to top
View user's profile Send private message
probe1
Moderator
Moderator


Joined: 18 Aug 2004
Posts: 2560
Location: Chonburi Thailand Asia

PostPosted: Fri Apr 01, 2011 7:13 pm    Post subject: Reply with quote

First: you dont have to declare variables

Second: if you wish to do so: declare a STRING variable for correct use. Alternatively you can use a VARIANT variable, which suits all types of variables.


Third: there is an FilePicker control element available for FORMs and DIALOGs - use that (to ease selection and avoid typos).

If you want to choose directories only: pick a file in the folder and strip the filename from the full string, using FileNameOutOfPath function from TOOLS library, STRINGS module



Does this help?
_________________
Cheers
Winfried
My Macros
DateTime2 extension: insert date, time or timestamp, formatted to your needs
Back to top
View user's profile Send private message Visit poster's website
adamzad
General User
General User


Joined: 08 Mar 2011
Posts: 16
Location: Buena Park, CA, USA

PostPosted: Fri Apr 01, 2011 7:49 pm    Post subject: Reply with quote

Maybe it would help if I posted the code I've tried or am using...

Code:
Option Explicit                     'I got tired of my typos creating unknown variables.
Sub XRayStuff (oEv as Object)
  Dim Stuff as Variant                  'Result from InputBox(). Tried as both Object and String, too.
  Dim LoopCheck as Integer : LoopCheck = 7         'To control the looping of the subroutine.
  Dim XRayTarget as Object               'The path to the Form Element I'm trying to XRay.
 
  While LoopCheck = 7
    Stuff = InputBox("What would you like to XRay?", "XRay Query", "oEv.Source")   'Getting my input on what to XRay.
'    XRay CObj(Stuff)                  'Failed attempt to convert string output from InputBox to object.
'    XRay Stuff                     'Pitched an "Invalid use of Object" error.

'*****  Begin backup, kludgey, method of conversion  *****
'    Select Case Stuff
'      Case "oEv.Source"
'        XRayTarget = oEv.Source
'      Case "oEv.Source.Model"
'        XRayTarget = oEv.Source.Model
'      Case "oEv.Source.Model.Parent"
'        XRayTarget = oEv.Source.Model.Parent
'      Case "ThisComponent"
'        XRayTarget = ThisComponent
'    End Select
'*****  End backup, kludgey, method of conversion  *****

    XRay XRayTarget
    LoopCheck = MsgBox("Are you done?  ", 36, "XRay Query.")   'Kick the loop back to the beginning or break out of it.
  Wend
End Sub


Incidentally, all of this is in Base. I'm trying to get a good picture, in my head, of how the database is structured so I can predict, ahead of time, what I need to XRay. Then I needn't bounce back and forth between the code window and the form window. Eventually, I won't need that wonderful godsend tool, XRay (peace, praise, blessings, prosperity, happiness, women - or men, as preferred - and granola to its creator), anymore. I'll just know what to code to get the desired result.

I should also mention, at no point am I going out of the database into the file system. And, yes, I know I'm flagrantly violating variable naming conventions, but I'm the only one who will ever look at this code and it all makes sense to me.

Fair day and be well,
Adam-Zad
Tagline: C Code. C Code run. Run, DAMNIT, run!
Back to top
View user's profile Send private message
B Marcelly
Super User
Super User


Joined: 12 May 2004
Posts: 1453
Location: France

PostPosted: Fri Apr 01, 2011 11:34 pm    Post subject: Reply with quote

Hi,
You cannot do that.
Suppose the user types this, for example : oEv.Source
Then you get a String variable containing oEv.Source

Suppose it exists a CObject() function. How could it work ? It should analyse what is the meaning of oEv, then the meaning of oEv.Source, related to the existing (and running) code. It can only be done if the language can insert this string into an existing code and interpret it "on the fly". This is not the case for OOoBasic.

Don't try to use the language in a not-designed-for way.
It is much more simple, clear, and quick to add a line in your code and run it.
Write, modify, delete code lines is the normal task of a programmer.
_________________
Bernard

OpenOffice.org 1.1.5 fr / Apache OpenOffice 4.0.1 / LibreOffice 4.1.0
MS-Windows 7 Home SP1
This forum is spammed, use instead Apache OpenOffice forums
Back to top
View user's profile Send private message Visit poster's website
adamzad
General User
General User


Joined: 08 Mar 2011
Posts: 16
Location: Buena Park, CA, USA

PostPosted: Fri Apr 01, 2011 11:53 pm    Post subject: Reply with quote

Bernard,
Thanks for the reply. Of course, the language already knows what "oEv" is, because that was passed to the subroutine as an argument. Would it not be a simple matter to look in that object event for a property or method with the name "Source"? Then look in the Source for a property or method named "Model"? And so on, down the line... There is already the capability to put strings into these "paths" when one calls, for example, GetByName("String"), with "String" being the name of an element (e.g, a subform or control). Is it really that difficult to use string variables for other parts of the path? Perhaps I do not have a sufficient understanding of the language, itself, but it seems to me that it shouldn't be that big an issue. If I can use the Select... Case statement to convert the string "oEv.Source.Model.Parent" into the object oEv.Source.Model.Parent, why can't I do it directly? Please correct me if I misunderstand.

Fair day and be well,
Adam-Zad
Tagline: There are 10 kinds of people: Those who understand binary and those who don't.
Back to top
View user's profile Send private message
B Marcelly
Super User
Super User


Joined: 12 May 2004
Posts: 1453
Location: France

PostPosted: Sat Apr 02, 2011 2:31 am    Post subject: Reply with quote

adamzad wrote:
1 - There is already the capability to put strings into these "paths" when one calls, for example, GetByName("String"), with "String" being the name of an element (e.g, a subform or control).
2 - [size=14]Is it really that difficult to use string variables for other parts of the path?
3 - Perhaps I do not have a sufficient understanding of the language, itself,
3 - but it seems to me that it shouldn't be that big an issue.

Answers:
1 - This is completely different. These are "simply" string arguments to a routine provided by an API object. The API object is designed to find the named object inside a container. You should know that the API is not OOoBasic.
2 - Yes, because the language should interpret an instruction in real time. OOoBasic does not do this.
3 - certainly.
4 - it is impossible because of the design of OOoBasic.
_________________
Bernard

OpenOffice.org 1.1.5 fr / Apache OpenOffice 4.0.1 / LibreOffice 4.1.0
MS-Windows 7 Home SP1
This forum is spammed, use instead Apache OpenOffice forums
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group