Welcome To The Home Of The Visual FoxPro Experts  
home. signup. forum. archives. search. google. articles. downloads. faq. members. weblogs. file info. rss.
 From: Stefan Wuebbe
  Where is Stefan Wuebbe?
 Hamburg
 Germany
 Stefan Wuebbe
 To: Samir Ibrahim
  Where is Samir Ibrahim?
 Chekka
 Lebanon
 Samir Ibrahim
 Tags
Subject: RE: VFP Wait Window in .NET
Thread ID: 415928 Message ID: 416132 # Views: 33 # Ratings: 0
Version: Unknown Category: .NET
Date: Monday, December 22, 2014 10:55:35 AM         
   


Good morning, Samir -

> I ended up creating my class base on "Hashtable"

Sounds reasonable.
I'd say an alternate approach could perhaps be a custom "Model" -
i.e. the "M" in the names of the "MVC", "MVVM", "MVP" patterns, https://www.google.com/search?q=winforms+mvc+mvvm

Using an "ORM" tool like MS "Entity Framework" or "NHibernate" would provide a similar approach.
https://www.google.com/search?q=ef6+code+first

The "Model", or the "Entity" respectively, would be a custom class, not necessarily based on anything in the first place. It would be a kind of self-made "DataRow" object, one for each table providing read-write properties, one for each column. It can provide additional features, like for example a "FullName" r/o property calculating a string based on something like "FirstName" and "LastName" properties that actually do represent real table columns, or also "business logic" features if you do not want to separate those in a "service" class.
Or it could keep track of the original property values in its constructor in order to get some VFP-like OldVal() / GetFldState() functionality, and so forth...

So the "Model" would represent the data row, and a row set object could be something like an IList, on demand being wrapped as something like an ObservableCollection (in WPF for example)

One great benefit among others of that approach IMO is that you would not need relatively vague string-based accessors that can contain typos, but you'd rather get "strong types", your Customer/Invoice/WhateverModel properties



hth
-Stefan


> Hi Stefan
>
> The point of this class is ... humm.. you can call it copy/paste vfp code logic, make it run and gives the same result that vfp gives without the need to re-create the logic in .Net
>
> I had made a project 9 years ago, they are still using it so far, but I wanted to make it part of my ERP, and it contains a accounting logic that i remember it took me long time to understand to make in vfp, so now i want to transfer it AS IS.
>
>
> My class usage is like this
>
>
Dim dt as New Datatable = ...... (Get your datatable)
> Dim oTable as New VFPTable
> oTable.Use(dt)
> oTable.Locate("farm_num = @p1 AND flock_num = @p2",{1000,59})
> Do While oTable.Found()
>     debug.print(oTable("frm_name"))
>     oTable.Continue
> Loop

>
>
> I want 2 things from the class, A-give me correct result, B- simple accessing data in it
>
> As can see from the code above, I am accessing the data by using oTable("frm_name")
>
> I spend sometime trying to figure how to access the data, If my class is based on Datatable, I had to access the data by oTable(0)("frm_name") and i don't want use (0) brackets neither I want oTable.Rows(0)("field_name")
>
> The only object I found that enables me from using Object("Field_Name") is the datarow, but
> I did not manage to inherit my class from Datarow, It has no constructor, and I found a code to make constructor, but I could not intialize it.
>
> I ended up creating my class base on "Hashtable"
>
> With each Continue(), I had to copy the current row to be the first object in my Hashtable object and access it. Doing large operatrion it time wasting (but my currect project has maximum of 3000 row) and will not increase (I had 30 chicken Farm, each farm has 2-6 floors, each Farm..Floor which called Floc has 45 record)
>
> Class Usage
>
>
'Do While Found() Example
>         Dim oTable As New VFPTable(MyDataset.FLOCK)
>         With oTable
>             .Use(Ds_visur_test.FLOCK)
>             .Locate("frm_num = @p1",{33})
>             do While .Found()
>                 Debug.Print(oTable("flk_count"))
>                 .Continue()
>             Loop
>         End With
> 
>         ' If Found() Example
>         oTable.Locate("frm_num = @p1 and flk_num = @p2 and floor = @p3",{42,60,2})
>         If oTable.Found()
>             MsgBox(oTable("flk_count"))
>         Else
>             MsgBox("Not Found")
>         End If
>         ' Sum Example
>     
>         Dim MySum = oTable.Sum("flk_count","frm_num = @p1 AND floor = @p2",15,1)
>         MsgBox(MySum)

>
> The Class
>
>
    Public NotInheritable Class VFPTable
>         'Inherits DataRow
>         'Inherits BindingSource
>         'Inherits DataRow
>         Inherits Hashtable
>         Property Rows As DataRowCollection
>         Property Table As DataTable
>         Property Row As Object 
>         Property Found As Boolean
>         Property RecNo As Integer
>         'Property x As New Collection
>         Private _dtrow As DataRow
>         Private _dvrow As DataRowView
>         Private _defaultview As DataView
>         Private _locateview As DataView
>         Private _record_index As Integer
>         Private _locatefound As Boolean
>         Private bs As New BindingSource
>         
>         Private _primary_keys() As String
>         Private _EOF As Boolean
>         Property EOF As Boolean
>         Get
>             If RecNo = _locateview.Count
>                 Return True
>             Else
>                 Return False
>             End If
>         End Get
>         Set(value As Boolean)
>             _EOF = value
>         End Set
>         End Property
> 
> 
>   ' Necessary only if you want to use your row in the Grid Designer
>         Sub New(ByVal dt As DataTable, ParamArray PrimaryKeys() As string)
>             Table = dt
>             bs.DataSource = dt.DefaultView
>             Rows = Table.Rows
>             _defaultview = Table.DefaultView
>             _locateview = New DataView(Table)
>             _primary_keys = PrimaryKeys
>         End Sub
>         Sub New()
>             MyBase.New
>         End Sub
>         Sub Use(ByVal dt As DataTable)
>             Table = dt
>             bs.DataSource = dt.DefaultView
>             Rows = Table.Rows
>             _defaultview = Table.DefaultView
>             _locateview = New DataView(Table)
>             'MyBase.Add("key1","value1")
>         End Sub
> 
>         Function Reccount() As Integer
>             Return Table.Rows.Count
>         End Function
> 
>         Function [Goto](ByVal RecordNo As Integer)
>             bs.Position = RecordNo
>             'Row = Table.Rows(RecordNo)
>             Row = _locateview(RecordNo -1)
>             
>             _AddRowToCollection()
>             RecNo = RecordNo
>         End Function
>         Private Function _AddRowToCollection()
>             MyBase.Clear
>             For i = 0 to Table.Columns.Count -1
>                 Dim field_name = table.Columns(i).ColumnName
>                 'MyBase.Add(field_name,Table.Rows(RecNo)(field_name))
>                 'MyBase.Add(field_name,Row(field_name))
>                 MyBase.Add(field_name,bs.Current(field_name))
>             Next
>         End Function
>         Function Locate(ByVal tcFilterExpr As String,toParameters() As Object)
>             Dim filter_expr = tcFilterExpr
>             Dim param_expr = ""
>             For i = 0 to toParameters.Length - 1
>                 param_expr = "@p"+ (i+1).ToString
>                 filter_expr = filter_expr.Replace(param_expr,toParameters(i))
>             Next
>             _locateview.RowFilter = filter_expr
>             If _locateview.Count > 0
>                 Found = True
>                 Row = _locateview(0)
>                 bs.DataSource = _locateview
>                 bs.Position = 0
>                 RecNo = 1
>                 _AddRowToCollection()
>             Else
>                 Found = False
>                 RecNo = 0
>                 MyBase.Clear
>             End If
>             _record_index = 0
>             
>             
>             'MsgBox(_locateview.Count)
>         End Function
>         Function [Continue]()
>             RecNo = RecNo + 1
>             _record_index = _record_index + 1
>             bs.Position = _record_index
>             _AddRowToCollection()
>             If RecNo = _locateview.Count + 1
>                 EOF = True
>                 Found = False
>             End If
> 
>         End Function
> 
>         Sub  Skip(ByVal tnRecords As Integer)
>             Dim new_rec_no = RecNo + tnRecords
>             bs.Position = new_rec_no
>             Row = bs.Current.Row
>             _record_index = RecNo-1 + tnRecords
>             RecNo = new_rec_no
>             _AddRowToCollection()
>         End Sub
> 
>         Function Sum(tcFieldToSum As String, tcExpression As String, ParamArray tcParameters() As String) As Double
>             Dim sum_expr = ""
>             Dim expr = tcExpression
>             For i = 0 To tcParameters.Length - 1
>                 expr = expr.Replace("@p" + CStr(i + 1), tcParameters(i))
>             Next
>             sum_expr = "Sum(@p1)".Replace("@p1", tcFieldToSum)
>             Dim answer = Me.Table.Compute(sum_expr, expr)
>             Return answer
>         End Function
>     End Class

>
>
>
> Samir Ibrahim

ENTIRE THREAD

VFP Wait Window in .NET Posted by Samir Ibrahim @ 12/16/2014 6:00:28 PM
RE: VFP Wait Window in .NET Posted by Stefan Wuebbe @ 12/16/2014 8:28:03 PM
RE: VFP Wait Window in .NET Posted by Samir Ibrahim @ 12/17/2014 9:11:05 AM
RE: VFP Wait Window in .NET Posted by Stefan Wuebbe @ 12/17/2014 5:34:18 PM
RE: VFP Wait Window in .NET Posted by Samir Ibrahim @ 12/20/2014 9:46:52 AM
RE: VFP Wait Window in .NET Posted by Stefan Wuebbe @ 12/21/2014 7:38:52 AM
RE: VFP Wait Window in .NET Posted by Samir Ibrahim @ 12/21/2014 7:49:51 PM
RE: VFP Wait Window in .NET Posted by Stefan Wuebbe @ 12/22/2014 10:55:35 AM