| View previous topic :: View next topic |
| Author |
Message |
adamzad General User


Joined: 08 Mar 2011 Posts: 16 Location: Buena Park, CA, USA
|
Posted: Fri Apr 01, 2011 4:39 pm Post subject: How to Convert String to Object? |
|
|
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.
Fair day and be well,
Adam-Zad, The Bear that Looks Like a Man |
|
| Back to top |
|
 |
probe1 Moderator


Joined: 18 Aug 2004 Posts: 2465 Location: Chonburi Thailand Asia
|
Posted: Fri Apr 01, 2011 5:33 pm Post subject: Re: How to Convert String to Object? |
|
|
| 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 |
|
 |
adamzad General User


Joined: 08 Mar 2011 Posts: 16 Location: Buena Park, CA, USA
|
Posted: Fri Apr 01, 2011 6:51 pm Post subject: |
|
|
| 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 |
|
 |
probe1 Moderator


Joined: 18 Aug 2004 Posts: 2465 Location: Chonburi Thailand Asia
|
Posted: Fri Apr 01, 2011 7:13 pm Post subject: |
|
|
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 |
|
 |
adamzad General User


Joined: 08 Mar 2011 Posts: 16 Location: Buena Park, CA, USA
|
Posted: Fri Apr 01, 2011 7:49 pm Post subject: |
|
|
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 |
|
 |
B Marcelly Super User

Joined: 12 May 2004 Posts: 1414 Location: France
|
Posted: Fri Apr 01, 2011 11:34 pm Post subject: |
|
|
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 / OpenOffice.org 3.4.1 en-US + langpacks, MS-Windows XP Home SP3
This forum is unusable, use instead Apache OpenOffice forums |
|
| Back to top |
|
 |
adamzad General User


Joined: 08 Mar 2011 Posts: 16 Location: Buena Park, CA, USA
|
Posted: Fri Apr 01, 2011 11:53 pm Post subject: |
|
|
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 |
|
 |
B Marcelly Super User

Joined: 12 May 2004 Posts: 1414 Location: France
|
Posted: Sat Apr 02, 2011 2:31 am Post subject: |
|
|
| 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 / OpenOffice.org 3.4.1 en-US + langpacks, MS-Windows XP Home SP3
This forum is unusable, use instead Apache OpenOffice forums |
|
| Back to top |
|
 |
|
|
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
|