c# - I'm getting an error when I turn on string Unicode encoding -
so, i'm making program reads spanish dictionary. grabs random word. need turn on unicode string because there characters "é" , that, requires encoding string differently. when turn on unicode string[]
gives me error message:
system.windows.markup.xamlparseexception unhandled message='the invocation of constructor on type 'wordaday.mainwindow' matches specified binding constraints threw exception.' line number '4' , line position '9'. source=presentationframework linenumber=4 lineposition=9 stacktrace: @ system.windows.markup.xamlreader.rewrapexception(exception e, ixamllineinfo lineinfo, uri baseuri) @ system.windows.markup.wpfxamlloader.load(xamlreader xamlreader, ixamlobjectwriterfactory writerfactory, boolean skipjournaledproperties, object rootobject, xamlobjectwritersettings settings, uri baseuri) @ system.windows.markup.wpfxamlloader.loadbaml(xamlreader xamlreader, boolean skipjournaledproperties, object rootobject, xamlaccesslevel accesslevel, uri baseuri) @ system.windows.markup.xamlreader.loadbaml(stream stream, parsercontext parsercontext, object parent, boolean closestream) @ system.windows.application.loadbamlstreamwithsyncinfo(stream stream, parsercontext pc) @ system.windows.application.loadcomponent(uri resourcelocator, boolean bskipjournaledproperties) @ system.windows.application.dostartup() @ system.windows.application.<.ctor>b__1(object unused) @ system.windows.threading.exceptionwrapper.internalrealcall(delegate callback, object args, int32 numargs) @ ms.internal.threading.exceptionfilterhelper.trycatchwhen(object source, delegate method, object args, int32 numargs, delegate catchhandler) @ system.windows.threading.dispatcheroperation.invokeimpl() @ system.windows.threading.dispatcheroperation.invokeinsecuritycontext(object state) @ system.threading.executioncontext.runtrycode(object userdata) @ system.runtime.compilerservices.runtimehelpers.executecodewithguaranteedcleanup(trycode code, cleanupcode backoutcode, object userdata) @ system.threading.executioncontext.runinternal(executioncontext executioncontext, contextcallback callback, object state) @ system.threading.executioncontext.run(executioncontext executioncontext, contextcallback callback, object state, boolean ignoresyncctx) @ system.threading.executioncontext.run(executioncontext executioncontext, contextcallback callback, object state) @ system.windows.threading.dispatcheroperation.invoke() @ system.windows.threading.dispatcher.processqueue() @ system.windows.threading.dispatcher.wndprochook(intptr hwnd, int32 msg, intptr wparam, intptr lparam, boolean& handled) @ ms.win32.hwndwrapper.wndproc(intptr hwnd, int32 msg, intptr wparam, intptr lparam, boolean& handled) @ ms.win32.hwndsubclass.dispatchercallbackoperation(object o) @ system.windows.threading.exceptionwrapper.internalrealcall(delegate callback, object args, int32 numargs) @ ms.internal.threading.exceptionfilterhelper.trycatchwhen(object source, delegate method, object args, int32 numargs, delegate catchhandler) @ system.windows.threading.dispatcher.invokeimpl(dispatcherpriority priority, timespan timeout, delegate method, object args, int32 numargs) @ ms.win32.hwndsubclass.subclasswndproc(intptr hwnd, int32 msg, intptr wparam, intptr lparam) @ ms.win32.unsafenativemethods.dispatchmessage(msg& msg) @ system.windows.threading.dispatcher.pushframeimpl(dispatcherframe frame) @ system.windows.threading.dispatcher.pushframe(dispatcherframe frame) @ system.windows.threading.dispatcher.run() @ system.windows.application.rundispatcher(object ignore) @ system.windows.application.runinternal(window window) @ system.windows.application.run(window window) @ system.windows.application.run() @ wordaday.app.main() in c:\documents , settings\admin\my documents\wordaday\wordaday\obj\x86\debug\app.g.cs:line 0 @ system.appdomain._nexecuteassembly(runtimeassembly assembly, string[] args) @ system.appdomain.executeassembly(string assemblyfile, evidence assemblysecurity, string[] args) @ microsoft.visualstudio.hostingprocess.hostproc.runusersassembly() @ system.threading.threadhelper.threadstart_context(object state) @ system.threading.executioncontext.run(executioncontext executioncontext, contextcallback callback, object state, boolean ignoresyncctx) @ system.threading.executioncontext.run(executioncontext executioncontext, contextcallback callback, object state) @ system.threading.threadhelper.threadstart() innerexception: system.indexoutofrangeexception message=index outside bounds of array. source=wordaday stacktrace: @ wordaday.mainwindow.newword() in c:\documents , settings\admin\my documents\wordaday\wordaday\mainwindow.xaml.cs:line 132 @ wordaday.mainwindow..ctor() in c:\documents , settings\admin\my documents\wordaday\wordaday\mainwindow.xaml.cs:line 33 innerexception:
which, way, wacky me , can't understand it. asking, mean when coming code?:
#region setup random word = new random(); string[] lines = file.readalllines(dictionarypath, encoding.unicode); int randomword = word.next(1, lines.count()); string[] excludedlines; if (!file.exists(path)) { file.create(path); } excludedlines = file.readalllines(path); string chosenword = lines[randomword]; #endregion #region logic if (excludedlines.count() == 58110) { file.writealltext(path, ""); } if (excludedlines.contains(chosenword)) { while (excludedlines.contains(chosenword)) { randomword = word.next(58110); chosenword = lines[randomword]; } file.appendalltext(path, chosenword + environment.newline); excludedlines = file.readalllines(path); label1.content = chosenword; } else { file.appendalltext(path, chosenword + environment.newline); excludedlines = file.readalllines(path); label1.content = chosenword; } #endregion
the following 2 lines throw index out of range exception:
randomword = word.next(58110); chosenword = lines[randomword];
surely following make more sense:
randomword = word.next(lines.length); chosenword = lines[randomword];
also, following line:
int randomword = word.next(1, lines.count());
should be
int randomword = word.next(lines.length);
in version, word @ lines[0]
never chosen randomly, looks wrong.
Comments
Post a Comment