[Gambas-devel] How to pass objects and access their properties

Randall Morgan rmorgan62 at ...176...
Sat Feb 4 16:03:00 CET 2012


Hi Benoit,

I didn't want to commit code that I was using to experiment and learn the
inner workings of Gambas.

Anyhow, I figured out that a pointer to the GB_OBJECT structure is what is
being passed to my class method.
>From there it had to learn that the value held a pointer to my GSLCOMPLEX
structure and using the value
stored in gb_object:value cast to a GSLCOMPLEX pointer I could access my
object members.

So I've ended up with code that looks like this:

// In gsl_complex.h

extern GB_DESC CGslComplexDesc[];

typedef
    struct __GSLCOMPLEX
    {
        GB_OBJECT ob;
        double real;
        double imagined;
    }
    GSLCOMPLEX;



// In gsl_complex.c

BEGIN_METHOD(GSLCOMPLEX_X, GB_OBJECT x;)
    GSLCOMPLEX *p;
    p = ARG(x)->value; // Get GSLCOMPLEX pointer from GB_OBJECT:value member
    // Now using our GSLCOMPLEX pointer we can access the GSLCOMPLEX
structure members
    GB.ReturnFloat(p->real);
END_METHOD


/**************************************************
  Describe Class properties and methods to Gambas
**************************************************/
GB_DESC CGslComplexDesc[] =
{
    GB_DECLARE("Complex", sizeof(GSLCOMPLEX)),

    GB_PROPERTY("real", "f", GSLCOMPLEX_REAL),
    GB_PROPERTY("imag", "f", GSLCOMPLEX_IMAGINED),

    GB_STATIC_METHOD("X", "f", GSLCOMPLEX_X, "(x)o"),

    GB_END_DECLARE
};


As I said, I always try to figure things out myself. I ask when I get
stuck. But even then I do not give up trying! Even us old dogs can learn
tricks :-)

Randall

2012/2/3 Benoît Minisini <gambas at ...1...>

> Le 02/02/2012 22:48, Randall Morgan a écrit :
> > Hi Benoit,
> >
> > I have a skeleton Complex class now but I am having difficulty locating
> > any sample code that shows how to pass an object in to a method and
> > access it's properties.
> >
> > The reason I want to do this is that complex numbers have a real and
> > imaginary component. libgsl defines these as a structure containing two
> > doubles. So my class
> > contains two properties real, and imag. Rather than having the user
> > write something like:
> >
> > Q.Add(V.real, V.imag)
> >
> > I would prefer the user be able to simply pass in the complex object and
> > let the underlying code access the properties of the object.
> >
> > So can you point me to some sample code where I can see how this might
> > be done?
> >
> > Thanks,
> >
> > Randall
> >
> >
>
> If you want some answers, I need that you commit your new source files!
> (svn add + svn commit)
>
> Regards,
>
> --
> Benoît Minisini
>
>
> ------------------------------------------------------------------------------
> Try before you buy = See our experts in action!
> The most comprehensive online learning library for Microsoft developers
> is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
> Metro Style Apps, more. Free future releases when you subscribe now!
> http://p.sf.net/sfu/learndevnow-dev2
> _______________________________________________
> Gambas-devel mailing list
> Gambas-devel at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/gambas-devel
>



-- 
If you ask me if it can be done. The answer is YES, it can always be done.
The correct questions however are... What will it cost, and how long will
it take?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/devel/attachments/20120204/184d9885/attachment.html>


More information about the Devel mailing list