need help on optional arg

G

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
 
O

Oenone

GS said:
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?
 
G

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
GS said:
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?
 
G

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top