vb.net - String.Substring(start, end) sometimes throwing an exception -
my program uses backgroundworker call performaction()
method when different method, method1
returns true. using strategy pattern set correct performaction()
should performed every time event raised.
for strategy pattern created abstract class, , class inside abstract class inherits it.
public mustinherit class abstract public mustoverride sub performaction(byval str string) public class extends inherits abstract public overrides sub performaction(byval str string) str = str.substring(str.indexof(":"), str.indexof(">")) msgbox(str) end sub end class
i create class contains field of abstract, , used call performaction.
the performaction method gets called backgroundworker.reportprogress event, called when backgroundworker.dowork detects method1
returning true. , code above, causes system.reflection.targetinvocationexception
addition information exception has been thrown target of invocation.
the debugger tells me:
cannot obtain value of local or argument '<this>' not available @ instruction pointer, possibly because has been optimized away. system.delegate args cannot obtain value of local or argument 'args' not available @ instruction pointer, possibly because has been optimized away. object[]
strangely enough, when perform (what seems me) identical operation 2 substrings:
s = s.substring(s.indexof(":")) s = s.substring(0, s.indexof(">"))
it functions perfectly.
what difference between these 2 methods? inheritance set incorrectly , causing these errors? what's going on here?
let me know if need add more code explain situation. thanks.
to effect of
s = s.substring(s.indexof(":")) s = s.substring(0, s.indexof(">"))
in single statement, need calculate length of desired substring
s = s.substring(s.indexof(":"), s.indexof(">") - s.indexof(":"))
note if possible string not contain ":" followed later ">", need first verify indexof(":")
>= 0 , s.indexof(">")
returns value greater indexof(":")
.
Comments
Post a Comment