home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / sys / mac / programm / 20684 < prev    next >
Encoding:
Text File  |  1993-01-05  |  2.8 KB  |  83 lines

  1. Newsgroups: comp.sys.mac.programmer
  2. Path: sparky!uunet!spool.mu.edu!uwm.edu!cs.utexas.edu!bcm!aio!mark@cheers.jsc.nasa.gov
  3. From: mark@cheers.jsc.nasa.gov (Mark Manning)
  4. Subject: Re: A Difference of Pointers
  5. Message-ID: <1993Jan5.151535.12592@aio.jsc.nasa.gov>
  6. Sender: news@aio.jsc.nasa.gov (USENET News System)
  7. Organization: NASA
  8. References: <1993Jan5.005151.4558@aio.jsc.nasa.gov>
  9. Date: Tue, 5 Jan 1993 15:15:35 GMT
  10. Lines: 71
  11.  
  12. I would like to thank everyone who wrote to me with their solutions
  13. to this problem.  Thanks! :)
  14.  
  15. The solution to the problem was sent by Sean J. Crist
  16. <kurisuto@chopin.udel.edu>.  To whom I will be eternally grateful
  17. for clearing up my confusion on this.  Thanks Sean!
  18.  
  19. Solution:
  20.  
  21. What I didn't know MPW Pascal could do was to perform typecasting
  22. (like you can do in C).  This is what needed to be done in order
  23. to correctly extract the needed information.  Sean wrote:
  24.  
  25. Mac Pascal supports a very important feature called typecasting.  You can
  26. typecast any type to any other type (within certain restrictions- you
  27. can't typecast an integer to a handle, for example).  I'll illustrate with
  28. code samples like yours:
  29.  
  30. >   SetPort( theDialog );
  31. >   GetDItem (theDialog, theItem, theType, theHandle, theRect);
  32. >   theControl := @theHandle;
  33. >   theValue := GetCtlValue( theControl );
  34.  
  35. (Just as an aside- I'm pretty sure you don't have to do this SetPort
  36. before you do GetDItem, but this isn't real important.)
  37.  
  38. Your code should read:
  39.  
  40.    SetPort( theDialog );
  41.    GetDItem (theDialog, theItem, theType, theHandle, theRect);
  42.    theValue := GetCtlValue(ControlHandle(theHandle));
  43.  
  44. The important line is the third one.  theHandle is type Handle, but you
  45. can make it into a ControlHandle by referring to it as
  46. ControlHandle(theHandle).
  47.  
  48. This is also how you allocate memory for your own kinds of handles.  For
  49. example, if you have the following declarations...
  50.  
  51.    type
  52.       MyStructure = record
  53.           AString : Str255;
  54.           AnInteger : Integer;
  55.           ABoolean : Boolean;
  56.         end;
  57.       MyPointer = MyStructure^;
  58.       MyHandle = MyPointer^;
  59.  
  60. ..then you would allocate a new MyHandle as follows:
  61.  
  62.    var
  63.       ExampleMyHandle : MyHandle;
  64.       ExampleHandle : Handle;
  65.    begin
  66.       ExampleHandle := NewHandle(SizeOf(MyStructure));
  67.       ExampleMyHandle := MyHandle(ExampleHandle);
  68.       ExampleMyHandle^^.AString := 'This is a string!'
  69.       etc.
  70.  
  71. If you like, you can leave out the ExampleHandle and collapse the first
  72. two lines after 'begin' into one:
  73.  
  74.      ExampleMyHandle := MyHandle(NewHandle(SizeOf(MyStructure)));
  75.  
  76. ---------------------->
  77.  
  78. And it works in both cases, without glitches, and without causing
  79. other problems.  I hope this helps others who are struggling with
  80. their own problems of learning to write programs.  Heaven knows - 
  81. I need all the help >I< can get! ;)
  82.  
  83.