473,383 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

constraint programming

Hi,

How can I include contraints in VB.NET (Windows Forms, 2.0), using Objects
with Propertys?

I need several property's be recalculated when others change, but not in a
straightforward way: When A is changed, B has to change, but when B changes,
A has to change (or C or ...).
Untill now I added routines in the Set-Property that calculated the other
Property, but I'm loosing the overview, and I would have referred to have
all this rules centrally placed. Also, I'm kind of affraid of endless loops:
A changes B, B changes A, which changed B again etc etc...

Is there a way to do some kind of constraint programming in VB.NET? Or how
should I implement such a thing the best way?

I need constraints like this:
- When the Currency changes, the AmountNewCurrency must be automaticly
changed to AmountOldCurrency * CurrencyRate
but:
When the user changes the in the TextBox the AmountNewCurrency, the
AmountOldCurrency must be changed too (AmountNewCurrency / CurrencyRate).

- Another one: TotalBuyPrice = the total Price we pay for all the Articles
TotalSellPrice = The total Price at which we sell the Articles
Percentage: the percentage we add to the TotalBuyPrice to have a sudden
TotalSellPrice.
-Is Percentage is changed, TotalSellPrice must be changed (and the
ArticleSellPrice accordingly).
but:
-When the TotalSellPrice is changed, the percentage has to be
calculated automaticly, and the ArticleSellPrice of each article has to be
changed proportionally

- and many many more...
Any help would be really appreciated,

Thansk a lot in advance,

Pieter
Jan 25 '07 #1
3 1493
Pieter wrote:
I need several property's be recalculated when others change, but not in a
straightforward way: When A is changed, B has to change, but when B changes,
A has to change (or C or ...).
I usually use a flag to prevent recursive calls, something like

Class X
Private _bRecursionBuster As Boolean = False

Property A() As ?
Set(Value as ?)

If Not _bRecursionBuster Then
_bRecursionBuster = True

B = Value /2

_bRecursionBuster = False
End If

End Set
End Property

Property B() As ?
Set(Value as ?)

If Not _bRecursionBuster Then
_bRecursionBuster = True

A = Value * 2

_bRecursionBuster = False
End If

End Set
End Property

End Class

With multiple properties, you make update everything from each property
and, with a bit of luck, you don;t get all that nasty endless looping.

HTH,
Phill W.
Jan 25 '07 #2
Pieter wrote:
<snip>
I need several property's be recalculated when others change, but not in a
straightforward way: When A is changed, B has to change, but when B changes,
A has to change (or C or ...).
Untill now I added routines in the Set-Property that calculated the other
Property, but I'm loosing the overview, and I would have referred to have
all this rules centrally placed. Also, I'm kind of affraid of endless loops:
A changes B, B changes A, which changed B again etc etc...
I'd use events to keep each class independent of each other.

Besides, if the changing of a value in some class might create an
endless loop then it seems you must put some safeguards in the code:

<aircode>
Class A
Public Event CurrencyChanged( _
Sender As Object, _
NewCurrency As Double)

Private mCurrency As Double

Property Currency As Double
Get
Return mCurrency
End Get

Set(Value As Double)
Static Busy As Boolean

If Value <mCurrency Then

If Busy Then
Throw New InvalidOperationException( _
"Set-Currency loop!" _
)
End If

Busy = True
mCurrency = Value
Try
RaiseEvent CurrencyChanged(Me, Value)
Finally
Busy = False
End Try
End If

End Set
End Property
End Class
</aircode>
HTH.

Regards,

Branco

Jan 25 '07 #3
I agree with Phil... update everything from each input to the function.

IOW, have a 'recalculate_page' method that calculates every function, from
currency to total price. Then, when the user changes any of the inputs,
from locale to quantity, whatever, call 'recalculate_page'.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Pieter" <pi****************@hotmail.comwrote in message
news:O1**************@TK2MSFTNGP06.phx.gbl...
Hi,

How can I include contraints in VB.NET (Windows Forms, 2.0), using Objects
with Propertys?

I need several property's be recalculated when others change, but not in a
straightforward way: When A is changed, B has to change, but when B
changes, A has to change (or C or ...).
Untill now I added routines in the Set-Property that calculated the other
Property, but I'm loosing the overview, and I would have referred to have
all this rules centrally placed. Also, I'm kind of affraid of endless
loops: A changes B, B changes A, which changed B again etc etc...

Is there a way to do some kind of constraint programming in VB.NET? Or how
should I implement such a thing the best way?

I need constraints like this:
- When the Currency changes, the AmountNewCurrency must be automaticly
changed to AmountOldCurrency * CurrencyRate
but:
When the user changes the in the TextBox the AmountNewCurrency, the
AmountOldCurrency must be changed too (AmountNewCurrency / CurrencyRate).

- Another one: TotalBuyPrice = the total Price we pay for all the Articles
TotalSellPrice = The total Price at which we sell the Articles
Percentage: the percentage we add to the TotalBuyPrice to have a sudden
TotalSellPrice.
-Is Percentage is changed, TotalSellPrice must be changed (and the
ArticleSellPrice accordingly).
but:
-When the TotalSellPrice is changed, the percentage has to be
calculated automaticly, and the ArticleSellPrice of each article has to be
changed proportionally

- and many many more...
Any help would be really appreciated,

Thansk a lot in advance,

Pieter

Jan 29 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Agoston Bejo | last post by:
I want to enforce such a constraint on a column that would ensure that the values be all unique, but this wouldn't apply to NULL values. (I.e. there may be more than one NULL value in the column.)...
4
by: wireless | last post by:
I've written code that dynamically builds an sql query based on various constraint possibilities. The problem is the code would have been very complex had I not come up with a dummy constraint...
2
by: adammitchell | last post by:
How can you indicate that a FOREIGN KEY constraint references two columns in two different tables? "SQL Server Books Online" show an example of how to reference two columns in the SAME table:...
3
by: ferg | last post by:
I have a Customer table. The table has two different CHECK constraints. Then there is the Customer details dialog, which provides the user with an UI for changing users. I have some UPDATE sql,...
6
by: Dan Holmes | last post by:
I have a class that i need a constraint of int, string, float or bool. I have tried the following but can't make VS accept it. I read the docs and they showed that any value type can be used...
3
by: Jeff Kish | last post by:
Hi. I'm getting errors like this when I try to run an upgrade script I'm trying to write/test: altering labels to length 60 Server: Msg 5074, Level 16, State 4, Line 5 The object...
2
by: Pieter | last post by:
Hi, How can I include contraints in VB.NET (Windows Forms, 2.0), using Objects with Propertys? I need several property's be recalculated when others change, but not in a straightforward way:...
4
by: antpal | last post by:
I am not sure exactly what i am doing run but when I run this code in sql plus i get constraint error on most of my tables except author and books table. Please help I am new to Programming in...
15
by: Frank Swarbrick | last post by:
I have the following three tables DROP TABLE CALLTRAK.SERVICE_CODES @ CREATE TABLE CALLTRAK.SERVICE_CODES ( CODE CHAR(1) NOT NULL , CONSTRAINT SERVICE_CODES_PK PRIMARY KEY (CODE) ,...
2
by: rorajoey | last post by:
Violation of UNIQUE KEY constraint 'IX_surveyQuestions'. Cannot insert duplicate key in object 'dbo.surveyQuestions'. This might seem like a simple matter of trying to insert a row with ID=20 when...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.