|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
need help on optional argPublic 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 GS wrote:
> webctl is an usercontrol with webbrowser control named as When you use optional parameters, the compiler needs to be able to build the > webbrowser1. vb express 2005 just does not like the webBrowser1. part > in the optional arguments 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 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? > 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 > >
vb 6.0 naar vb.net 2005
Dynamic mapping string to function Overlapping controls creating pictureboxes on the fly How to determen the object ? WEB combobox won't fire event Redrawing on GDI+ NullReferenceException with shared members Referencing parameters collection member by Name bombs? crash at closue msvbvm60.dll |
|||||||||||||||||||||||