New topic: 

How to access a property of a sub-classed window control

<http://forums.realsoftware.com/viewtopic.php?t=47669>

         Page 1 of 1
   [ 5 posts ]                 Previous topic | Next topic          Author  
Message        superjacent          Post subject: How to access a property of a 
sub-classed window controlPosted: Mon Apr 22, 2013 6:41 am                      
           
Joined: Sat Oct 01, 2005 4:47 am
Posts: 107
Location: Melbourne, Australia                I've subclassed a TextField 
control, added a string property called ("FieldName"). I've added this 
subclassed control to a window. On a window there are many of these controls 
and they are to be matched to fields of a database table.

I'm finding it difficult writing code in another class where I can reference 
the property of the subclassed control.

The process that's in my head is this. At the class level, cycle through a 
recordset, populating each of the window controls with data from the recordset. 
The matching aspect at the window control side of things is the property I 
created, FieldName. 

Would appreciate any suggestions.      
_________________
Steve
rs2012 r2.1 Windows 7.  
                             Top                msssltd          Post subject: 
Re: How to access a property of a sub-classed window controlPosted: Mon Apr 22, 
2013 11:58 am                                 
Joined: Fri Oct 28, 2005 7:05 am
Posts: 555
Location: Emsworth, UK                There are many, many ways to do what you 
want.  So many ways it could be the subject of a chapter on OOP.  But it sounds 
like you are struggling with Scope.  

Class Methods and Properties have a scope modifier within the declaration;
Public (a blue dot in RS); the method is accessible to all other objects
Private (a red dot in RS); the method is only accessible to this object.
Protected (a yellow triangle in RS); this method is accessible to this object 
and any object which inherits the method, sub classes of this object.

Here is an example of a ListBox control I created today.  You might note that I 
am passing the non visual  record set, to the visual DataBox control, not the 
other way around.  You might also note I don't have to use the me. operator, as 
me. is inferred by the method residing within the class.  
Class DataBox Inherits ListBox
  
  Public Sub DataBox.Populate(rs as RecordSet)
  If rs Is Nil Then
  Return
  End if
  
  //use field names as headings
  ColumnCount = rs.FieldCount
  For i as integer = 0 to ColumnCount -1
  Heading(i) = rs.IdxField(i +1).StringValue
  Next
  HasHeading = true
  
  //add records to the list
  DeleteAllRows
  While not rs.EOF
  AddRow rs.IdxField(1)
  For i as Integer = 0 to ColumnCount -1
    Cell(lastIndex, i) = rs.IdxField(i +1).StringValue
  Next
  rs.MoveNext
  Wend
  End Sub


Now when I drop my DataBox on a Window, and access it from the app object, I 
might use it like this
Sub App.Foo
  dim sql as string, rs as recordset
  sql = "SELECT myfield as Product, Count(myfield) as Total FROM myTable GROUP 
BY myfield ORDER BY Total DESC;"
  
  If db.Connect Then
  rs = db.SQLSelect(sql)
  If not (rs Is Nil) then
  If not (rs.EOF and rs.BOF) then
    Window1.DataBox1Populate(rs)
  End If
  End if
  End if
End


Were I accessing the data box from the same window it was placed on
sub Window1.Foo
  dim sql as string, rs as recordset
  sql = "SELECT * FROM myTable;"
  
  if app.db.connect then
  rs = app.db.SQLExecute(rs)
  if not (rs is Nil) then
  If not (rs.EOF and rs.BOF then
    self.DataBox1.Populate(rs)
  end if
  end if
  end if
End


Class property scope works in a very similar way.
Had I made db a private or protected property of App, I would not be able to 
access it from Window1.

Is it making any more sense yet?      
_________________
Yes it's me in the avatar  
                             Top                superjacent          Post 
subject: Re: How to access a property of a sub-classed window controlPosted: 
Mon Apr 22, 2013 7:08 pm                                 
Joined: Sat Oct 01, 2005 4:47 am
Posts: 107
Location: Melbourne, Australia                Thanks for replying, much 
appreciated. I've read and re-read your example many times and can follow 
what's going on though I'm still having trouble with it.

Just to re-iterate, I've created a sub-class of the TextField control 
(db_TextField), added a property (public - blue dot) and corresponding method 
to get the property.

Plonked the sub-classed (db_TextField) control onto a Window and set data for 
the newly created property (FieldName : data is field name of SQL statement).

A separate class handles all the initial SQL creation routines for the Window, 
one of those routines is to scan my sub-classed TextField (db_TextField) 
controls, check the user-defined property FieldName and then populate that 
db_TextField with data from a Recordset.


Here's what I'm working with at the Class level but isn't quite working. Some 
extraneous stuff added for testing purposes.

Sub DisplayRecord
  
  dim intControl as Integer
  dim i as integer
  dim CurrWin as Window, CurrControl as Control
  
  dim f as Integer
  
  dim strFieldName as string
  dim strTableName as string
  
  CurrWin = Window(0)    // I believe this refers to current window.
  
  dim y as Integer
  
  for intControl = CurrWin.ControlCount - 1 DownTo 0
  CurrControl = CurrWin.Control(intControl)
  
  if CurrControl isa db_TextField then
  
  for y = 1 to c_rst_OneRecord.FieldCount
    
    strFieldName = db_TextField("CurrControl.Name").GetFieldName   
//GetFieldName is getter of property
    strTableName = db_TextField("CurrControl.Name").GetTableName  
//Autocomplete helped produce this.
    
    strFieldName = db_TextField.GetFieldName
    
    // The above 3 lines of code all produce errors upon compilation
    
    // if hardcoded, it works
    strFieldName = win_contacts.fld_Contact_ID.FieldName
    
  next
  
  end if
  next


I must be missing something. Any other hints or clues appreciated.      
_________________
Steve
rs2012 r2.1 Windows 7.  
                             Top                timhare          Post subject: 
Re: How to access a property of a sub-classed window controlPosted: Mon Apr 22, 
2013 9:16 pm                         
Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12262
Location: Portland, OR  USA                Use the control directly, not its 
name or a string.

strFieldName = db_TextField(CurrControl).GetFieldName   //GetFieldName is 
getter of property
strTableName = db_TextField(CurrControl).GetTableName  //Autocomplete helped 
produce this.   
                             Top                superjacent          Post 
subject: Re: How to access a property of a sub-classed window controlPosted: 
Mon Apr 22, 2013 11:33 pm                                 
Joined: Sat Oct 01, 2005 4:47 am
Posts: 107
Location: Melbourne, Australia                timhare wrote:Use the control 
directly, not its name or a string.

strFieldName = db_TextField(CurrControl).GetFieldName   //GetFieldName is 
getter of property
strTableName = db_TextField(CurrControl).GetTableName  //Autocomplete helped 
produce this.

Thanks Tim, that did the trick. 

Also thanks to msssltd really appreciated your example, it has given me some 
ideas.      
_________________
Steve
rs2012 r2.1 Windows 7.  
                             Top             Display posts from previous: All 
posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost 
timeSubject AscendingDescending          Page 1 of 1
   [ 5 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]

Reply via email to