Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculation in a class embedded in a form

Status
Not open for further replies.

SitesMasstec

Programmer
Sep 26, 2010
508
Brasil
Hello colleagues!

I have a class which I use in a form:

SomaClassVFP_fc1p0z.jpg


In the class, in the field showed here in a red retangle, I want to put the sum of the four values above (shown here in the purple box). To achieve the Total I had this command in the Object txtTotalMarBR, Procedure Refresh:

Code:
this.Value = txtTarifaMarBR.Value + txtTaxaPortBR.Value + txtTransferBR.Value + txtTaxaServBR.Value

When I execute the program, I got this error:
ErroClassVFP_qsopfy.jpg


I tried this commmand but another error appeared:
Code:
this.Parent.Value = Parent.txtTarifaMarBR.Value + Parent.txtTaxaPortBR.Value + Parent.txtTransferBR.Value + Parent.txtTaxaServBR.Value
ErroClass2VFP_xwpnn7.jpg



What is wrong?


Thank you,
SitesMasstec
 
SitesMasstec,

why do you even fiddle with the objects?
Those other 4 controls have a controlsource, don't they? If I assume you always do the same as in your other recent thread, one record holds all the fields, all you need to sum is those four fields:

Code:
This.value = tablename.fieldfortxtTarifaMarBR+tablename.fieldfor_txtTaxaPortBR+...

Where fieldfortxtTarifaMarBR is the field you display in txtTarifaMarBR of the table named tablename.

Chriss
 
Chris:
I am not using any table in this application yet. I am just developing the interface.

For now, I am using:
In the Class:
In the txtTotalMarBR (Total) field, Procedure Refresh:
Code:
this.Parent.txtTotalMarBR.Value = this.Parent.txtTarifaMarBR.Value + this.Parent.txtTaxaPortBR.Value + this.Parent.txtTransferBR.Value + this.Parent.txtTaxaServBR.Value

Then in every field value to be added (txtTarifaMarBR, txtTaxaPortBR, txtTransferBR, txtTaxaServBR) to total field, I put in Procedure LostFocus:
Code:
this.Parent.Refresh

It is working fine.

In a later phase of development, I'll insert method to save the values in a table.


Thank you,
SitesMasstec
 
Yes, it works, but this is backwards. It explains what you had in thread184-1829216 and I can just copy&paste what I said about that, there:

myself said:
Your code is able to act on the data directly without using the controls. The controls are the interface a user needs to interact, not your code. Anything you want to show to a user usually comes from a record of a table and you can always use that, there are cases where storing a value to a field would be indirection, for example if you want to show a message in a messagebox, a messagebox is not a control with a controlsource, but usually it is more direct to act on a DBF or cursor or view with code than acting on controls. If you make that your rule of thumb, you'll less often address controls at all and that also frees you from moving controls, arranging them differently, without the need to adapt code to those layout changes.

Now I (we) know this all stems from your approach to do UI first. The way you do this you don't really prototoype, you burn in business logic (albeit simple summing here) into the form and later depend on that, the calculation you do with data are business logic and that deserves separation from the user interface, you hardwire something that shouldn't be hardwired into the UI, not even for prototyping purposes.

Chriss
 
Chris is right on this. What he (we) are trying to do is help you become strong, more proficient, because in the longer term, as you maintain your application, it will have significant effects on how quickly and easily you can make adjustments, or even new capabilities. It's the old "Pay me now, or pay me later" scenario and we're just trying to help you avoid pitfalls.
But like was stated earlier, it's your learning curve, and only you can climb it. We're just here to help along the way. Now that said, what I see is a fairly new developer (SitesMasstec) working with the experiences you have with the real world as the basis for your application architecture. To put another way, it's the difference between basic math and algebra.
You have to think differently as you increase your capability in mathematics. You have to make yet another leap to get to calculus, geometry and trigonometry. This is the hill you are climbing. You're trying to solve algebraic functions with only basic math, and to some degree that will work, but it has a limitation (several). I highly encourage you to "slow down" right now, and absorb the differences and the new ideas, and how to achieve what you want in the solutions.
In my own experience, I remember making the "leap" from FoxPro to Visual FoxPro back in 1995. I had been a procedural based programmer for years, and suddenly this idea of "object oriented" came along. It would be another 6 years before I finally wrapped my head around it, and moved from FPW 2.6 to VFP (I think we were up to 6 by then). And I realized how much I had to learn that was different, but also realized how much of what I knew was still useful. It builds upon the ideas. I think in this analogy, you are still at the FPD 2 stage, where your approach isn't based on relational data bases (not that FPD 2 wasn't, just using this as an analogy) there is a very steep slope to climb. So slow down, take a bit of time to learn the capabilities, and the reasons WHY some things are done the way they are, and WHY some things are avoided (like hard coding) as it's better for the long run.

Ok, preaching is over. Just wanted to convey that you will have a much easier time ultimately if you embrace taking the time to understand why things work in VFP, not just how they work.

Best Regards,
Scott
MSc ISM, MIET, MASHRAE, CDCAP, CDCP, CDCS, CDCE, CTDC, CTIA, ATS, ATD

"I try to be nice, but sometimes my mouth doesn't cooperate.
 
Chris and Scott:
I began with FP 2.0 DOS in the late nineties and then migrated to VFP 6 (Windows) in 2002 and since 2011 I have been using VFP 9. I have many Hentezenwerke books about VFP and, for many years I have been studying VFP language.

I have learned that this language is as flexible as powerful. It has many ways to do a task that I get confused about the best way to achieve a solution.

As I am not sure about which data I will use in database or free tables (as I have posted in another thread) I took the way just using a form. After a careful decision is made, I will save the data in a database/tables.

But I keep climbing. Thanks for your patience.

Thank you,
SitesMasstec
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top