-
Jul 29th, 2021, 01:25 AM
#1
Thread Starter
Junior Member
Passing PointF as Optional Parameter
I want to pass PointF as optional Parameter. But I am finding it difficult to assign default value for PointF.
I can't assign Nothing. If I do it becomes (0,0) which can be working value hence can't be used as a unique default value.
I am not able to assign any other value. I tried (Optional P1 As PointF = New PointF(-100, -100) but it does not work.
Public MyFunc(Optional P1 As PointF = ????)
Please help.
-
Jul 29th, 2021, 02:55 AM
#2
Re: Passing PointF as Optional Parameter
PointF is a value type so, just like any other value type, a variable of that type will always have a value. For all value types, Nothing maps to the default value for that type, e.g. 0 for Integer, False for Boolean and #1/01/0001# for Date. If you want to be able to represent no value at all then you need to use a nullable value type, i.e. Nullbale(Of T) or T? for short, e.g.
vb.net Code:
Public Sub DoSomething(Optional location As PointF? = Nothing)
If location.HasValue Then
Dim locationValue As PointF = location.Value
'Use locationValue here.
End If
End Sub
Of course, you don't necessarily have to use an optional parameter at all. Depending on the body of the method, it may be better to overload than use an optional parameter, e.g.
vb.net Code:
Public Sub DoSomething()
'...
End Sub
Public Sub DoSomething(location As PointF)
'...
End Sub
-
Jul 30th, 2021, 05:29 AM
#3
Thread Starter
Junior Member
Re: Passing PointF as Optional Parameter
Thanks jmcilhinney for the response.
I already had overloaded functions and I wanted to combine them. I used nullable concept successfully and its working now.
If there are many optional parameters then, I guess, it would be difficult to manage many overloaded functions.
-
Jul 30th, 2021, 07:22 AM
#4
Re: Passing PointF as Optional Parameter
It's actually easier to manage a couple of overloaded functions than one function with a bunch of optional parameters. In an overloaded function, you know you're going to have ABC, or ABD or ACT or ABE... but in a combined method you have to check for C then T, then D, then E, and B .... And then what if you get an invalid combinatiion... ABC and ABD and ABE are valid, but ABT isn't ... now you've got a problem, and more checks to run... with overloaded methods, the correct version is selected automatically based on the overloaded types, eliminating that problem.
-tg
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|