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

johnf jfabiani at ...1109...
Sat Jan 14 21:21:51 CET 2006


On Saturday 14 January 2006 11:46, Rob Kudla wrote:
> On Sat January 14 2006 11:37, johnf wrote:
> > have been a function.  My only excuse is I program in Foxpro
> > and there is not real difference between a function and a
> > method both can return a variable.
>
> In Perl, C and most other languages there's no difference between
> the two either.... it's really a BASIC thing.  I do that all the
> time myself and have to correct it to make it compile.
>
> > All I'm attempting to do is write a class that can accept a
> > string which will create a "result" datatype with a name
> > matching the string.
>
> I think I remember doing what you're trying to do, way back in my
> dBase IV days.  (Foxpro's language was based on dBase.)
>
> The difference is, symbols (meaning names of variables, etc.) are
> discarded in Gambas when you compile the code, just like in Java
> and C, so you can't create a variable with a specific name at
> runtime.  What you can do, however, is this:
>
> ' outside of any subs...
> PUBLIC MyResults as Collection
>
> ' in your Sub Main or Form_Load or whatever...
> MyResults = NEW Collection
>
> ' and then you can write this:
> DIM s as String
> s = "anyname"
> MyResults[s] = db.Execute("select * from arcust")
> ' then you can go like this:
> print MyResults["anyname"]["Address1"]
> ' or
> print MyResults[s]["City"]
> ' or even
> dim r as Result
> r = MyResults[s]
> print r["Zip"]
> ' etc.
>
> So, to rewrite that function from your original question,
>
> public function myResult(thestr as String) as Result
>
> return MyResults[thestr]
>
> end
>
> But it'd probably be easier to just refer to MyResults[thestr]
> without writing that function.
>
> Let me know if this is what you had in mind.
>
> > Actually the sql statement is very different but I want it to
> > simple. The "realbasic" example is called "casting" I believe.
>
> I have about 5 minutes of experience with RealBasic, but
> "casting" in other languages means "forcing the
> compiler/interpreter to treat a variable as a different type
> than it was created as."  For example, suppose you have an
> integer and you want your C compiler to treat it as an unsigned
> long.... you'd go like this:
>
> int i = 1;
> printf("Look, it's an unsigned long: %d", (ulong) i);
>
> While used frequently, casting is pretty dangerous.  If for
> example I had written
>
> printf("Look, it's a string: %s", (char *) i);
>
> forcing i to be a string pointer, the program would have crashed
> and dumped core at runtime, as it would have been looking for
> the string to start at memory location 0x00000001.
>
> Anyway, Gambas (like VB and most other BASIC variants, and even
> most other high level languages like php and python) has no such
> concept as casting.... types are automatically converted when
> possible, and there are conversion functions like Val() and
> CStr() when it's not.  And there's a type called Variant which
> can basically store anything.  The keys and values in a
> Collection are always variants, which is how we can put your
> Result into one.
>
> > I'm a newbie to gambas and basic in general.  Which means I'm
> > trying to use old thinking with a new langauge.  So I'm very
> > open to suggestions on how to create a framework.  Actually I
> > open to anything with respect to Gambas.
>
> What I've described is pretty similar to how the IDE, for
> example, handles the controls we create at runtime.  The IDE is
> probably the best example of a Gambas framework there is, right
> now.
>
> Rob
On the surface this looks like the answer.  I knew there just had to be a way 
to create my class.  Thanks, let me play with this and figure out how to get 
the first steps of my framework created.  

John




More information about the User mailing list