1

I thought I can chain methods/properties on Nullable Types using the ?-Operator like so

Dim foo As SomeObject? = New SomeObject()
Dim bar As SomeObject? = Nothing
foo?.SomeProperty 'Returns a value
bar?.someProperty 'Returns Nothing

However, given the following special example

Structure Foo
  Public Sub New(bar As Integer)
    Me.Bar = bar
  End Sub
  Property Bar() As Integer
  Public Shared Operator +(f1 As Foo, f2 As Foo) As Foo
    Return New Foo(f1.Bar + f2.Bar)
  End Operator
End Structure

I get an error after performing the following Operation

Dim baz1 As Foo? = New Foo(13)
Dim baz2 As Foo? = New Foo(37)
Dim baz3 = baz1 + baz2
baz3?.Bar 'error BC36637: The '?' character cannot be used here

Why is that so and more important how can I achieve the behaviour shown in my first example at the top?

Tobi Holy
  • 85
  • 6

1 Answers1

0

Main issue after testing:

I copied your exact code into VS2015 and tested it. The issue is because you are attempting to call a property, but are not doing anything with it.

You cannot just call a property. You either need to write to it or read from it. The below works just fine.

Dim baz1 As Foo? = New Foo(13)
Dim baz2 As Foo? = New Foo(37)
Dim baz3 = baz1 + baz2

Dim fooBar = baz3?.Bar

If I actually add a method, it too will work:

Dim baz1 As Foo? = New Foo(13)
Dim baz2 As Foo? = New Foo(37)
Dim baz3 = baz1 + baz2
baz3?.FooBar()

The actual error I got with your code was:

BC30545: Property access must assign to the property or use its value.

Old thought:

The issue is with your Operator + overload. You returned a standard struct instance. This is not nullable. Therefore, you cannot use the ? operator as it will never be null and isn't nullable.

Public Shared Operator +(f1 As Foo, f2 As Foo) As Foo
    Return New Foo(f1.Bar + f2.Bar)
End Operator

You may be able to get it to work (untested) by changing your declaration to:

Dim baz3 As Foo? = baz1 + baz2

But even then that may not work because baz1+baz2 currently will also never be null.

TyCobb
  • 8,909
  • 1
  • 33
  • 53
  • a quick type check using `TypeOf(baz3) ...` reveals `Foo?` as the actual type and as such the chaining with the ?-character should work, shouldn't it? – Tobi Holy Feb 28 '17 at 23:23
  • @no_mindset I don't see that because the code you showed listed as the return for your operator overload was `As Foo`. It would need to be `As Foo?` if you wanted it to return a nullable type. It may work at runtime, but you are getting an error at compile time. – TyCobb Feb 28 '17 at 23:25
  • even if i declare the return type of the Operator overload `As Foo?` the same error occurs; type of `bar3` is still `Foo?` – Tobi Holy Feb 28 '17 at 23:28
  • @no_mindset updated my answer after actually testing your code. – TyCobb Feb 28 '17 at 23:35
  • +1 ty, i accepted the answer because i believe you're right - but even assigning the property or calling an equivalent method does result in the error for me, but i heavily assume i have to blame my compiler for not being the latest version to support the ?-character in that place. could you please confirm your vbc.exe version for me? mine is 14.6.1586 – Tobi Holy Feb 28 '17 at 23:48
  • @no_mindset That's the version I have in my .NET folder, but I don't believe that is what Visual Studio is running as you still need newer version of VS in order to use newer syntactic sugar. For instance VS2013 doesn't support C#6 even though I also have VS2015 installed. – TyCobb Mar 01 '17 at 15:42
  • That must be the problem as I have no VS2015 installed currently. Thank you very much! – Tobi Holy Mar 01 '17 at 15:46