Home All Groups Group Topic Archive Search About

need help on optional arg

Author
6 Oct 2006 5:57 PM
GS
I have this:
   Public Function CleanHtml(ByVal strHtml As String, Optional ByVal doc As
HtmlDocument = WebBrowser1.Document) As String
flagged by vb as
Error 5 Reference to a non-shared member requires an object reference.
c:\myproject\WebCtl.vb 73 93 myproject

webctl is an usercontrol with webbrowser control named as webbrowser1. vb
express 2005 just does not like the webBrowser1. part in the optional
arguments


for now I got around declaring two sub, one with 1 arg calling the 2nd with
two arts . the 2nd sub test if doc is nothing then set it to
webbrowser1.document.

this work around is awkward and counterproductive.


I appreciate help in getting optional arg with default working

Author
6 Oct 2006 6:33 PM
Oenone
GS wrote:
> webctl is an usercontrol with webbrowser control named as
> webbrowser1. vb express 2005 just does not like the webBrowser1. part
> in the optional arguments

When you use optional parameters, the compiler needs to be able to build the
actual values that are to be used when the parameter is omitted into the
compiled code. Here you are telling it to use a specific object from a form.
As the form doesn't exist at compile time, it is unable to use that object
as the default value.

How about:

\\\
Public Function CleanHtml(ByVal strHtml As String, Optional ByVal doc As
HtmlDocument = Nothing) As String

    If doc Is Nothing Then
        doc = webbrowser1.Document
    End If

    [...rest of code...]
///

Does that do what you need?

--

(O)enone
Author
6 Oct 2006 7:14 PM
GhostInAK
Hello Oenone,

Oenone is correct in their assessment of optional values.

I would suggest avoiding optional parameters though.  Instead use overloaded
procedures.

Public Function CleanHtml(ByVal strHtml As String) As String
Return CleanHtml(strHtml, Webbrowser1.Document)
End Function

Public Function CleanHtml(ByVal strHtml As String, ByVal doc As HtmlDocument)
As String

if nothing is doc then
throw new NullReferenceException("Eh, wassup Doc?")
end if

'  Do crap here.

End Function

-Boo

Show quoteHide quote
> GS wrote:
>
>> webctl is an usercontrol with webbrowser control named as
>> webbrowser1. vb express 2005 just does not like the webBrowser1. part
>> in the optional arguments
>>
> When you use optional parameters, the compiler needs to be able to
> build the actual values that are to be used when the parameter is
> omitted into the compiled code. Here you are telling it to use a
> specific object from a form. As the form doesn't exist at compile
> time, it is unable to use that object as the default value.
>
> How about:
>
> \\\
> Public Function CleanHtml(ByVal strHtml As String, Optional ByVal doc
> As
> HtmlDocument = Nothing) As String
> If doc Is Nothing Then
> doc = webbrowser1.Document
> End If
> [...rest of code...]
> ///
> Does that do what you need?
>
Author
6 Oct 2006 10:21 PM
GS
thank you all.

I guess optional parameter can be troublesome especially when there is more
than one.

IN this single optional parameter at the end of the parameter list, what
kind problem should one expect?


Show quoteHide quote
"GS" <gsmsnews.microsoft.co***@msnews.Nomail.com> wrote in message
news:OBZrrDX6GHA.2208@TK2MSFTNGP04.phx.gbl...
> I have this:
>    Public Function CleanHtml(ByVal strHtml As String, Optional ByVal doc
As
> HtmlDocument = WebBrowser1.Document) As String
>  flagged by vb as
> Error 5 Reference to a non-shared member requires an object reference.
> c:\myproject\WebCtl.vb 73 93 myproject
>
> webctl is an usercontrol with webbrowser control named as webbrowser1. vb
> express 2005 just does not like the webBrowser1. part in the optional
> arguments
>
>
> for now I got around declaring two sub, one with 1 arg calling the 2nd
with
> two arts . the 2nd sub test if doc is nothing then set it to
> webbrowser1.document.
>
> this work around is awkward and counterproductive.
>
>
> I appreciate help in getting optional arg with default working
>
>