VB의 Null 검사입니다.
내가 하고 싶은 것은 객체가 null인지 확인하는 것이지만, 내가 무엇을 하든, 만약 그것이 컴파일된다면, 그것은 그것을 던집니다.NullReferenceException
확인하려고 합니다!제가 한 일은 다음과 같습니다.
If ((Not (comp.Container Is Nothing)) And (Not (comp.Container.Components Is Nothing))) Then
For i As Integer = 0 To comp.Container.Components.Count() - 1 Step 1
fixUIIn(comp.Container.Components.Item(i), style)
Next
End If
If ((Not IsDBNull(comp.Container)) And (Not IsDBNull(comp.Container.Components))) Then
For i As Integer = 0 To comp.Container.Components.Count() - 1 Step 1
fixUIIn(comp.Container.Components.Item(i), style)
Next
End If
If ((Not IsNothing(comp.Container)) And (Not IsNothing(comp.Container.Components))) Then
For i As Integer = 0 To comp.Container.Components.Count() - 1 Step 1
fixUIIn(comp.Container.Components.Item(i), style)
Next
End If
If ((Not (comp.Container Is DBNull.Value)) And (Not (comp.Container.Components Is DBNull.Value))) Then
For i As Integer = 0 To comp.Container.Components.Count() Step 1
fixUIIn(comp.Container.Components.Item(i), style)
Next
End If
저는 VB 책을 찾아보고, 여러 포럼을 검색했는데, 작동해야 하는 모든 것이 작동하지 않습니다!그런 교정적인 질문을 해서 미안하지만, 나는 단지 알고 싶을 뿐입니다.
디버거에서 Null 객체가 다음과 같이 말합니다.comp.Container
변경 내용And
스토AndAlso
s
A 표준And
두 식을 모두 테스트합니다.한다면comp.Container
이라Nothing
그러면 두 번째 표현은 a를 높일 것입니다.NullReferenceException
Null 개체의 속성에 액세스하고 있기 때문입니다.
AndAlso
논리적 평가를 단락시킵니다.한다면comp.Container
이라Nothing
그러면 두 번째 식은 평가되지 않습니다.
코드가 필요 이상으로 복잡합니다.
교체하다(Not (X Is Nothing))
와 함께X IsNot Nothing
외부 괄호를 생략합니다.
If comp.Container IsNot Nothing AndAlso comp.Container.Components IsNot Nothing Then
For i As Integer = 0 To comp.Container.Components.Count() - 1
fixUIIn(comp.Container.Components(i), style)
Next
End If
훨씬 더 읽기 쉽습니다.또한 중복된 데이터를 제거했습니다.Step 1
그리고 아마도 중복된 것..Item
.
하지만 (댓글에서 지적했듯이) 인덱스 기반 루프는 어쨌든 유행하지 않습니다.꼭 사용해야 하는 경우가 아니면 사용하지 마십시오.사용하다For Each
대신:
If comp.Container IsNot Nothing AndAlso comp.Container.Components IsNot Nothing Then
For Each component In comp.Container.Components
fixUIIn(component, style)
Next
End If
언급URL : https://stackoverflow.com/questions/5583112/null-check-in-vb
'sourcecode' 카테고리의 다른 글
각도 CLI 오류:serve 명령을 Angular 프로젝트에서 실행해야 하지만 프로젝트 정의를 찾을 수 없습니다. (0) | 2023.05.08 |
---|---|
'로컬 시스템' 계정과 '네트워크 서비스' 계정의 차이점은 무엇입니까? (0) | 2023.05.08 |
배시 완료와 관련하여 ${array[*]} 대 ${array[@]}에 대한 혼동 (0) | 2023.05.08 |
변경 시 라디오 사용 방법 (0) | 2023.05.08 |
Postgres가 수동으로 시퀀스 (0) | 2023.05.08 |