[Gambas-user] Re: casting or changing datatype.

ron ronstk at ...239...
Sat Jan 14 15:57:43 CET 2006


On Saturday 14 January 2006 11:36, Rohnny Stormo wrote:
> Hi.
> 
> Think scripting or javascript has a variant of this if 'm not wrong.  
> Looks like he is thinking of something like this.
> 
> Public Sub SomeSub(ItemsToUse as string)
>     [ItemsToUse].Text ="Hello"  
> end sub
> 
> SomeSub("TextBox1")
> SomeSub("TextBox2")
> 
> This is a small example, but in larger scale it could be usefull, 
> special when makeing classes.
> 
> Would be nice thow if it could be done.
> 
> Regards Rohnny
> 

You are simply setting a global variable named by content of ItemsToUse in the Sub.
SomeSub creates here 2 variables "Textbox#" and set value them to "Hello"

Global variables is not posible in gambas with good reason by Benoit, but can be
emulated to java/vb behaviour by prefix them with a module name.
Use i.e. a module named 'Global' for Global.MyGlobalVBvariable :)

But that is not the real question.

What he is doing in his example looks more as he want to 
Dim variables with a name passed by string.

He is doing with 'dim thestr as Result' is setting a dynamic name for a variable 
in a Dim, what makes nosense because that variable lives only in runtime inside the
Sub code.

Or he wants to give a name to the Result, and can not be done for Result here.
Or he want a variable with the name passed as string and returning that variable
so he can refer to that variable??

myResult("TheNameForTheVariable")
print TheNameForTheVariable

The Result is a wrong datatype/object here as example.
Result is more acting as a Object with a contruction of a DataType.

I belive he used the 'Result' as an textual example and not as the real thing
and must be seen as <datatype>, take the one you want.


Public Sub myResult(thestr as string) as <xxxx> ' i.e. <object>
  dim MyXXXX as <xxxx>
  MyXXXX = new <xxxx>
  MyXXXX.Name = thestr 'it should have the property Name of course :)
  Return MyCtrl
End

This could be more the way, but the 'Name' propertie in gambas is not available
as in VB for controls or most other objects.


Or returning the datatype of a variable where the name is passed by a string
  TheDataType = VarTypeOf("TheVarName")
instaed of
  TheDataType = VarTypeOf(TheVarName)

BTW
public sub myResult( thestr as String) as Result
is not posible, only functions return somthing.

Subs can only set global available variables for a return value and not
as in VB, change the the content of the variable passed By Ref(erence)

Gambas passes for 'not object' variable always by (copy of the) value.
Code can can change the passed value but orginal stay the same.


public sub myResult( thestr as String) as Result
dim thestr as Result

See for _Dim_ the help again. 
You should give a name (without quotes) for the variable and <type>/<object>
Here _thestr_ IS the name of the variable.
How do you want use the content of a string as i.e. "myName" as unqouted name?

Dim "thestr" as Result
Dim "myName" as String

Print thestr
Print myName
 
In fact _this_ can be done, no problem if there are quotes around the variable name
to interpret it as the variable name without quotes in the rest of the code.
But what should do _Print thestr_ if you call the sub with a different name by sting
i.e. "NotMyBike" ? 
Of course then we should/must use: Print "thestr" :)

So you did give at least two point you should analize more.
Sub returning a value and the way you want to implement the Dim you suggest.


You should give a more complete example what you wants with a piece of code that uses it.
Special the _Result_ word you use make it more complex, 
the real _Result_ or a _word-for-something_ and make here more sence.

---
My 2 cents

Ron





More information about the User mailing list