Results 1 to 4 of 4

Thread: Passing PointF as Optional Parameter

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2018
    Posts
    16

    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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    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:
    1. Public Sub DoSomething(Optional location As PointF? = Nothing)
    2.     If location.HasValue Then
    3.         Dim locationValue As PointF = location.Value
    4.  
    5.         'Use locationValue here.
    6.     End If
    7. 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:
    1. Public Sub DoSomething()
    2.     '...
    3. End Sub
    4.  
    5. Public Sub DoSomething(location As PointF)
    6.     '...
    7. End Sub

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2018
    Posts
    16

    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.

  4. #4
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width