New topic: OOP. Need help
<http://forums.realsoftware.com/viewtopic.php?t=46541> Page 1 of 1 [ 7 posts ] Previous topic | Next topic Author Message Antonio Post subject: OOP. Need helpPosted: Sun Jan 13, 2013 4:45 am Joined: Sat Feb 04, 2012 1:26 pm Posts: 69 Location: Italy Hi, I am studying the exemples in the RS folder trying to really understand how programming works. I encounter two pieces of code I have not understood that much as i do not find out a practical use. 1) an about windows is opened using the following code, creating a new instance of that window. dim aw as aboutbox aw = new aboutbox So far, I launched the aboutbox window like this: aboutbox.show So what's the difference? did I make a mistake so far, launching the window using aboutbox.show? What is the practical use to launch the windows, by instanciating a new one? 2) the following pieces of code apparently return the same result: a message box showing the date dim t as new date msgbox t.Shortdate dim t as date t = new date msgbox t.Shortdate Are there benefits from using one of the code instead the other one ? thanks for helping, Antonio Top lenpartico Post subject: Re: OOP. Need helpPosted: Sun Jan 13, 2013 7:53 am Joined: Fri Sep 30, 2005 10:49 pm Posts: 476 OK this is my explanation... dim aw as aboutbox // Allocate some memory for an aboutbox window If aw = nil then msgbox "Could not perform that request" return // something went wrong... maybe no memory available to allocate for a new aw else aw = new aboutbox // Show that newly created aboutbox window end if dim t as new date // Allocate some memory for a new date and then if memory is available create t // each time t is called a new date is created msgbox t.longtime //Display t.longtime in a messagebox //This splits "dim t as new date" into two lines dim t as date // Allocate some memory for a new date t = new date // if memory is available create t msgbox t.longtime I used longtime since longtime would change every second rather than everyday as with t.shortdate Lennox Top ktekinay Post subject: Re: OOP. Need helpPosted: Sun Jan 13, 2013 11:11 am Joined: Mon Feb 05, 2007 5:21 pm Posts: 354 Location: New York, NY 1) A window is special class in that it instantiates itself. As such, either method of showing it is valid. If you only ever need one instance, like an About box, use the form MyWindow.Show. If you need to make multiple instances, like a document, use the form dim w as new MyWindow w.Show 2) There is no performance benefit in using one form over the other. You might find one form more readable than the other, or have some other preference, but that's it. _________________ Kem Tekinay MacTechnologies Consulting http://www.mactechnologies.com/ Need to develop, test, and refine regular expressions? Try RegExRX. Top npalardy Post subject: Re: OOP. Need helpPosted: Sun Jan 13, 2013 11:55 am Real Software Engineer Joined: Sat Dec 24, 2005 8:18 pm Posts: 7674 Location: Canada, Alberta, Near Red Deer Antonio wrote:Hi, I am studying the exemples in the RS folder trying to really understand how programming works. I encounter two pieces of code I have not understood that much as i do not find out a practical use. 1) an about windows is opened using the following code, creating a new instance of that window. dim aw as aboutbox aw = new aboutbox So far, I launched the aboutbox window like this: aboutbox.show So what's the difference? did I make a mistake so far, launching the window using aboutbox.show? What is the practical use to launch the windows, by instanciating a new one? Windows are kind of special in that they have a property called "implicitinstance" which means you can use the name of the window as though one already exists. MOST classes you have to create one before you can use it. You create one with the "new" operator. Because windows have this small bit of magic you dont have to do that and the line aboutbox.show works Antonio wrote:2) the following pieces of code apparently return the same result: a message box showing the date dim t as new date msgbox t.Shortdate dim t as date t = new date msgbox t.Shortdate Are there benefits from using one of the code instead the other one ? That will depend entirely on whether you need to hang on to the "date" instance & do something more with it or not. _________________ My web site Great White Software RBLibrary.com REALbasic learning Top markwalsh Post subject: Re: OOP. Need helpPosted: Mon Jan 14, 2013 9:23 am Joined: Wed May 20, 2009 4:43 pm Posts: 921 Essentially, these are the same: Dim c as myClass c = new myClass Dim c as new myClass There are times when you need to define and instantiate the variable separately, e.g. you may want to use different constructors for a class. The following won't work: If flag Then Dim c as new myClass(parameterA) else Dim c as new myClass(parameterB, parameterC) end if c.doStuff() Since 'c' is defined in within the 'If flag...' block, it's scope is limited to that block, and is no longer valid by the time you get to the 'doStuff' line. In this case, you need to define the variable in the same code block that contains the 'doStuff' line, but you instantiate the variable within your 'if' block: Dim c as myClass If flag Then c = new myClass(parameterA) else c = new myClass(parameterB, parameterC) end if c.doStuff() _________________ RB 2009r4 Windows XP Top Antonio Post subject: Re: OOP. Need helpPosted: Mon Jan 14, 2013 3:08 pm Joined: Sat Feb 04, 2012 1:26 pm Posts: 69 Location: Italy Thank you all very much for helping. I tryed to create a little excercise by myself to apply the concept. as for the date class. 1st way: dim t as Date dim begintime, endtime as Double dim counter As Integer t = new date begintime = t.TotalSeconds for counter = 1 to 10000000 next t = new date endtime = t.TotalSeconds MsgBox cstr(endtime-begintime) 2nd way: dim t as new Date dim begintime, endtime as Double dim counter As Integer begintime = t.TotalSeconds for counter = 1 to 10000000 next dim t2 as new date endtime = t2.TotalSeconds MsgBox cstr(endtime-begintime) As for the aboutbox matter, when I create more instances of a control, is there a way to track them. I mean if I put these codes inside a pushbutton dim aw as aboutbox aw = new aboutbox or dim w as new MyWindow w.Show and push it several times, can I identify someway each window so that I can close the one instead of another ? Top ktekinay Post subject: Re: OOP. Need helpPosted: Mon Jan 14, 2013 7:39 pm Joined: Mon Feb 05, 2007 5:21 pm Posts: 354 Location: New York, NY 1) Dates. You can also do: dim t as new Date .. t = new Date BTW, I understand you only posted this as an example, but to be clear, the Ticks or Microseconds functions are better suited to timing. 2) Windows. There is a WindowCount function that will give you a count of all of your open windows, and you can always cycle through them using the Window function. See the documentation for both. Or you can assign your open windows to a global array and mange it yourself. _________________ Kem Tekinay MacTechnologies Consulting http://www.mactechnologies.com/ Need to develop, test, and refine regular expressions? Try RegExRX. Top Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending Page 1 of 1 [ 7 posts ] -- Over 1500 classes with 29000 functions in one REALbasic plug-in collection. The Monkeybread Software Realbasic Plugin v9.3. http://www.monkeybreadsoftware.de/realbasic/plugins.shtml [email protected]
