0
Dim response As MyClass = obj.ProcessRequest(strRequest)
Msgbox("This is the response message: " & response.Message)

If there is a problem in the ProcessRequest method and it returns null, then the next line will not work and a NullReference exception will be thrown.

What is the easiest way to have response.Message evaluate to an empty string if response is null?

2 Answers 2

2

You may use inline If:

Msgbox("This is the response message: " & If(response Is Nothing, "", response.Message))

but IMO it is more readable to use if .. else clause

Sign up to request clarification or add additional context in comments.

Comments

0

& Nothing equates to & "".

So the neatest way IMO is:

Dim response As MyClass = obj.ProcessRequest(strRequest)
Msgbox("This is the response message: " & response?.Message)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.