webForumDet fria alternativet

if-satser i funktion

ASP

9 svar · 778 visningar · startad av devotion

Medlem sedan jan. 20013 582 inlägg
Frågan#1

Hepp!! :)

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

Men kan man göra det hela snyggare?

Mvh
Henrik :bire

Medlem sedan jan. 20013 582 inlägg
#2

Hmm...

såg en sak direkt...

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

Borde väl vara samma sak?

Mvh
Henrik

Medlem sedan dec. 20025 483 inlägg
#3

Tja!

Några saker att reflektera över:

  1. Inte alla vägar i funktionen returnerar ett deterministiskt värde.
  2. Varje if-sats evalueras även om en träff fås tidigt.
  3. Tre CDbl()-konverteringar görs i stället för en.
Medlem sedan jan. 20013 582 inlägg
#4

Jag tänkte väl att det var saker jag inte tänkt på....

Hur hade du gjort det då?

:)

Mvh
Henrik

Medlem sedan juni 20019 519 inlägg
#5

Nåt i denna stil:

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
Medlem sedan dec. 20025 483 inlägg
#6

...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?

Medlem sedan juni 20003 076 inlägg
#7

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
Medlem sedan aug. 20039 340 inlägg
#8

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.

Medlem sedan jan. 20013 582 inlägg
#9

Peter S skrev:

...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....

:-)

Mvh
Henrik

Medlem sedan jan. 20013 582 inlägg
#10

Funkish!

Mvh
Henrik

262 ms totalt · 4 externa anrop · v20260731065814-full.86ec41c2
130 ms — deklarationer (db)
0 ms — hämta statistik (cache)
129 ms — hämta tråd, inlägg och bilagor (db)
125 ms — ändringar (db)