home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.sys.mac.programmer
- Path: sparky!uunet!spool.mu.edu!uwm.edu!cs.utexas.edu!bcm!aio!mark@cheers.jsc.nasa.gov
- From: mark@cheers.jsc.nasa.gov (Mark Manning)
- Subject: Re: A Difference of Pointers
- Message-ID: <1993Jan5.151535.12592@aio.jsc.nasa.gov>
- Sender: news@aio.jsc.nasa.gov (USENET News System)
- Organization: NASA
- References: <1993Jan5.005151.4558@aio.jsc.nasa.gov>
- Date: Tue, 5 Jan 1993 15:15:35 GMT
- Lines: 71
-
- I would like to thank everyone who wrote to me with their solutions
- to this problem. Thanks! :)
-
- The solution to the problem was sent by Sean J. Crist
- <kurisuto@chopin.udel.edu>. To whom I will be eternally grateful
- for clearing up my confusion on this. Thanks Sean!
-
- Solution:
-
- What I didn't know MPW Pascal could do was to perform typecasting
- (like you can do in C). This is what needed to be done in order
- to correctly extract the needed information. Sean wrote:
-
- Mac Pascal supports a very important feature called typecasting. You can
- typecast any type to any other type (within certain restrictions- you
- can't typecast an integer to a handle, for example). I'll illustrate with
- code samples like yours:
-
- > SetPort( theDialog );
- > GetDItem (theDialog, theItem, theType, theHandle, theRect);
- > theControl := @theHandle;
- > theValue := GetCtlValue( theControl );
-
- (Just as an aside- I'm pretty sure you don't have to do this SetPort
- before you do GetDItem, but this isn't real important.)
-
- Your code should read:
-
- SetPort( theDialog );
- GetDItem (theDialog, theItem, theType, theHandle, theRect);
- theValue := GetCtlValue(ControlHandle(theHandle));
-
- The important line is the third one. theHandle is type Handle, but you
- can make it into a ControlHandle by referring to it as
- ControlHandle(theHandle).
-
- This is also how you allocate memory for your own kinds of handles. For
- example, if you have the following declarations...
-
- type
- MyStructure = record
- AString : Str255;
- AnInteger : Integer;
- ABoolean : Boolean;
- end;
- MyPointer = MyStructure^;
- MyHandle = MyPointer^;
-
- ..then you would allocate a new MyHandle as follows:
-
- var
- ExampleMyHandle : MyHandle;
- ExampleHandle : Handle;
- begin
- ExampleHandle := NewHandle(SizeOf(MyStructure));
- ExampleMyHandle := MyHandle(ExampleHandle);
- ExampleMyHandle^^.AString := 'This is a string!'
- etc.
-
- If you like, you can leave out the ExampleHandle and collapse the first
- two lines after 'begin' into one:
-
- ExampleMyHandle := MyHandle(NewHandle(SizeOf(MyStructure)));
-
- ---------------------->
-
- And it works in both cases, without glitches, and without causing
- other problems. I hope this helps others who are struggling with
- their own problems of learning to write programs. Heaven knows -
- I need all the help >I< can get! ;)
-
-