Jag har gjort en liten funktion med ett antal villkor.
Såvitt jag kan se, så fungerar det som det skall:
function GetExportPrice(fPrice, bInvoice)
if cDbl(fPrice) = 0 and bInvoice = 1 then GetExportPrice = ""
if cDbl(fPrice) = 0 and bInvoice = 0 then GetExportPrice = 0
if cDbl(fPrice) > 0 and bInvoice = 1 then GetExportPrice = fPrice
if cDbl(fPrice) > 0 and bInvoice = 0 then GetExportPrice = 0
End function
function GetExportPrice(fPrice, bInvoice)
if cDbl(fPrice) = 0 and bInvoice = 1 then GetExportPrice = ""
if cDbl(fPrice) >= 0 and bInvoice = 0 then GetExportPrice = 0
if cDbl(fPrice) > 0 and bInvoice = 1 then GetExportPrice = fPrice
End function
function GetExportPrice(fPrice, bInvoice)
Dim f_Price
f_Price = cDbl(fPrice)
If f_Price = 0 And bInvoice Then
GetExportPrice = ""
Else If f_Price >= 0 And Not bInvoice Then
GetExportPrice = 0
Else If f_Price > 0 and bInvoice Then
GetExportPrice = fPrice ' Om det nu inte skall retuneras som en Dbl, så behöver du inte köra en f_Price
End If
End function
...samt kanske lägga till 'Else' med ett returvärde ifall ingen träff fås. Om det valda värdet är något av de andra så kan logiken förenklas ytterligare.
Om du vill göra det hela lite mer läsbart så bidrar jag med en variant! :)
function GetExportPrice(fPrice, bInvoice)
Dim f_Price
f_Price = cDbl(fPrice)
If f_Price <= 0 Then
If bInvoice Then
GetExportPrice = ""
Else
GetExportPrice = 0
End If
Else
If bInvoice Then
GetExportPrice = fPrice
Else
GetExportPrice = 0
End If
End If
End function
Nämen! Ni gör ju alla "fel". ;)
Om bInvoice = 0 ska exportpriset bli 0 oavsett fPrice. Alltså ska man (för att göra koden mindre och mer läsbar) ha bInvoice i en if ytterst. Alltså:
function GetExportPrice(fPrice, bInvoice)
If bInvoice then
if cDbl(fPrice) > 0 then GetExportPrice = fPrice else GetExportPrice = ""
Else
GetExportPrice = 0
End If
End function
Dessutom ser jag ingen anledning att ha en omvandlig till Double där. Om ingen kan ge en bra anledning gissar jag att det är säkert att bort cDbl.
...samt kanske lägga till 'Else' med ett returvärde ifall ingen träff fås. Om det valda värdet är något av de andra så kan logiken förenklas ytterligare.
Kan argumenten vara null?
I mycket sällsynta fall, kanske... Det 'ska' inte kunna det, men man vet ju aldrig....