0

I need to have a function that if a string is = "" return "" else return the cint of the string. I wrote the function below:

Function returnIntWithNull(intString As String )    
        Return If(intString="","",CInt(intString )) 
    End Function

But I get the below warning

Function without an 'As' clause; return type of Object assumed

and

Cannot infer a common type because more than one type is possible; 'Object' assumed.

In my code I need to call the function as :

New JArray({returnIntWithNull(getRow.getDrawOffsets)})),

i need that the Jproperty if the GetDrawOffsets return "" will be equal to "advancedraws":[] else if return a string that contain a number must be in the format "advancedraws":[65]

2
  • Every integer can be represented as string. Are there any technical reasons that prevent you from .ToString() on the input parameter, so that you can use your function as is? (it requires string). Then I'd use Int.TryParse(). It may be well possible that TryCast accepts Object, but I can't easily test the scenario, especially with your conditions. Commented Oct 10, 2019 at 13:52
  • The function is used in this call New JProperty("advancedraws",New JArray({returnIntWithNull(getRow.getDrawOffsets)})), i need that the Jproperty if the GetDrawOffsets return "" will be equal to "advancedraws":[] else if return a string that contain a number must be in the format "advancedraws":[65] Commented Oct 10, 2019 at 13:54

1 Answer 1

2

Specify the return type as Object and specify one of the results of the If operator to be Object also.

Function returnIntWithNull(intString As String) As Object
    Return If(intString = String.Empty, String.Empty, CObj(CInt(intString))) 
End Function

If requires that the two results be the same type or one to inherit the other. String and Integer don't satisfy either of those options. By casting one result as type Object, you satisfy the second option.

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

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.