문제

내가 큰 고전 ASP 응용 프로그램을 유지해야,그리고 내가 반복적으로 자신을 찾는 좌절의 부족에 의해 단락 회로 평가 능력입니다.E.g., VBScript 못하게하고리:

if not isNull(Rs("myField")) and Rs("myField") <> 0 then
...

기는 경우는 Rs("유사")는 null,당신은에 오류가 두 번째 조건이 비교,null 은 0 입니다.그래서 내가 일반적으로 결국 이 일을 대신:

dim myField
if isNull(Rs("myField")) then 
    myField = 0
else
    myField = Rs("myField")
end if

if myField <> 0 then
...

물론,verboseness 은 매우 깨달았습니다.보고 주위에 이 코드를 기초,최선의 해결 방법을 발견했는 기능을 사용하면 원래의 프로그래머 썼다,라고 TernaryOp 는 기본적으로 접목에서는 원자 같은 기능을 하지만 나는 여전히 붙어 임시 변수를 사용하는 것이 필요하지 않에서 이상의 전체 추천 언어입니다.은 더 나은 방법이 있을까?몇 가지는 슈퍼-비밀하는 방식의 단락 정말 존재하지 않 VBScript?

도움이 되었습니까?

해결책

아마 최고의 방법이지만,그것은 확실히 작동하는지...또한,는 경우에 vb6 또는.순할 수 있는 다른 방법이 적절한 형식으로 캐스팅합니다 너무입니다.

if cint( getVal( rs("blah"), "" ) )<> 0 then
  'do something
end if


function getVal( v, replacementVal )
  if v is nothing then
    getVal = replacementVal
  else
    getVal = v
  end if
end function

다른 팁

중첩된 IFs(만 약간 덜 자세한 정보):

if not isNull(Rs("myField")) Then
   if Rs("myField") <> 0 then

난 항상 사용되는 사례를 선택합 문 짧은 논리 회로에서 VB.무언가가 다음과 같..

Select Case True

Case isNull(Rs("myField"))

    myField = 0

Case (Rs("myField") <> 0)

    myField = Rs("myField")

Case Else

    myField = -1        

End Select

내 구문을 떨어져있을 수 있습이었습니다.는 경우 경우 나타납,다른 모든 것은 무시됩니다.

또는 아마도 내의 문제입니다.았다 당신은 같은 뭔가 의미 iIf() VB?이것이 나를 위해:

myField = returnIf(isNothing(rs("myField")), 0, rs("myField"))

returnIf() 는 기능이다.

function returnIf(uExpression, uTrue, uFalse)
    if (uExpression = true) then returnIf = uTrue else returnIf = uFalse : end if
end function

당신이 쓰는 경우에 그것은 두 개의 인라인 IF 문 달성할 수 있는 단락시키기:

if not isNull(Rs("myField")) then if Rs("myField") <> 0 then ...

하지만 당신 then 작업에 표시해야 합 같은 줄 뿐입니다.필요할 경우 여러 문 후 then, 구분할 수 있습으로 그들을 : 또는 이동하는 코드 서브루틴는 당신을 호출 할 수 있습니다.예를 들어:

if not isNull(Rs("myField")) then if Rs("myField") <> 0 then x = 1 : y = 2

if not isNull(Rs("myField")) then if Rs("myField") <> 0 then DoSomething(Rs("myField"))

누구도 따라올 수 없었는 TernaryOp 이 당신의 유일한 희망입니다.

그래요 그것은 최고의 솔루션을 하지만 우리가 사용하는 이 같은 뭔가

function ReplaceNull(s)
    if IsNull(s) or s = "" then
        ReplaceNull = "&nbsp;"
    else
        ReplaceNull = s
    end if
end function

두 가지 옵션을 마음에 와서:

1)이용 len()lenb() 발견하는 데이터가있는 경우에는 변수:

if not lenb(rs("myField"))=0 then...

2)반환하는 함수의 사용 boolean:

if not isNothing(rs("myField")) then...

isNothing() 는 기능이다.

function isNothing(vInput)
    isNothing = false : vInput = trim(vInput)
    if vartype(vInput)=0 or isEmpty(vInput) or isNull(vInput) or lenb(vInput)=0 then isNothing = true : end if 
end function

할 수 있습니다 단지 사용 Else catch null,""s,etc.

If UCase(Rs("myField")) = "THING" then
  'Do Things
elseif UCase(Rs("myField")) = "STUFF" then
  'Do Other Stuff
else
  'Invalid data, such as a NULL, "", etc.
  'Throw an error, do nothing, or default action
End If

나는 테스트이에서 내 코드와 그것의 현재 작동합니다.이 될 수도 있지 오른쪽 모두의 상황이지만.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top