c# - Issues with Generics using Silverlight -
i creating c# web application using silverlight 5 (vs 2010).
i created console application works fine , adapting web app.
even in web application working fine particularly set data type (say example int
instead of <t>
working fine) when use generic doesn't work. it compiles error free doesn't debug area set "toggle break point". gui this:
but control passes error prone part, gui disappears
and place kept break points replaced
(see in left most) as result not able debug find problem .
some explanation trying do: in given code below have binary file , stored inside "filecontents" of data type byte[]
(i not disclosing method read file, can consider filecontents contains contents of binary file inside mainpage
class). store symbols (of form 0 , 1 in binary file) , find frequency (by counting number of time repeats in file, has no problem not writing method it). processingvalue
variable in code of generic type(<t>
) store in "symbol"
(which of <t>
type (this symbol read binary file 1 of these short
/int
/long
/uint32
/uint64
etc.) not showing in code).
i have scenario this:
using system; using system.collections.generic; using system.linq; using system.net; using system.windows; using system.windows.controls; using system.windows.documents; using system.windows.input; using system.windows.media; using system.windows.media.animation; using system.windows.shapes; using system.io; using system.text; using system.runtime.interopservices; using system.diagnostics; namespace { public partial class mainpage: usercontrol { byte[] filecontent; public mainpage() { initializecomponent(); filecontent = null; //suppose contains binary filecontents } //i press button in order call huffman class because it's object declared in public void buttonclickevent(object sender, routedeventargs e) { messagebox.show("check0"); //suppose had stored contents of file inside "filecontent" , filecontent below contains //contents of binary file //**the line below error prone** huffman < uint > obj1 = new huffman < uint > (this, filecontent, bitconverter.touint32); //this object creation creates problem, whereas if remove generic type (<t>), works fine. //if don't use genrics : huffman obj1 = new huffman(this, filecontent); (whereas <t> in huffman class replaced "int" , works fine) messagebox.show("check1"); //this check box never executed } } public class huffman < t > t: struct,icomparable < t > ,iequatable < t > { public huffman(mainpage object, byte[] filecontent, func < byte[], int, t > converter) { messagebox.show("check2"); //it never executes length = filecontent.length; size = marshal.sizeof(typeof (t)); byte[] data; (long position = 0; position + size < length; position += size) { data = filecontent; //this data conatains filecontents t processingvalue = converter(data, 0); { //i here processingvalue int16/int32/int64/uin32/uint64 etc. } } } } }
is there problem in bitconverter
function in object creation in mainpage class?
i not able debug huffman class, set break points @ starting , ending points of huffman class control don't come inside , buttons (created using xaml gui) on internet explorer disappears.
here full code : (please pay attention reading binary file (any file extension ".o"(filename.o) canbe taken testing code, read well)):
using system; using system.collections.generic; using system.linq; using system.net; using system.windows; using system.windows.controls; using system.windows.documents; using system.windows.input; using system.windows.media; using system.windows.media.animation; using system.windows.shapes; using system.io; using system.text; using system.runtime.interopservices; using system.diagnostics; namespace finalss { public partial class mainpage : usercontrol { public const int chunk_size = 4096; public const string upload_uri = "http://localhost:50323/fileupload.ashx?filename={0}&append={1}"; public const string upload_dialog_filter = "all files (*.*)|*.*|jpeg images (*.jpg)|*.png|png images (*.png)|*.png"; private stream data; private string filename; private long totalbytes; private long uploadedbytes; byte[] filecontent; public event action simpleevent; public mainpage() { initializecomponent(); progressbar.visibility = visibility.collapsed; textbox.visibility = visibility.collapsed; // filecontent = null; uploadedbytes = 0; totalbytes = 0; } /* public void comboinvoke() { combobox1.items.add("byte"); combobox1.items.add("sbyte"); combobox1.items.add("short"); combobox1.items.add("int"); combobox1.items.add("long"); } */ public void browsebuttonclick(object sender, routedeventargs e) { openfiledialog dlg = new openfiledialog(); dlg.multiselect = false; dlg.filter = upload_dialog_filter; bool? retval = dlg.showdialog(); if (retval != null && retval == true) { progressbar.visibility = visibility.visible; textbox.visibility = visibility.visible; textbox.text = "uploading file..."; data = dlg.file.openread(); totalbytes = data.length; uploadedbytes = 0; filename = dlg.file.name; progressbar.maximum = totalbytes; uploadfilechunk(); } } private void uploadfilechunk() { textbox.text = "upload in progress..."; string uploaduri = ""; if (uploadedbytes == 0) { uploaduri = string.format(upload_uri, filename, 0); // dont't append } else if (uploadedbytes < totalbytes) { uploaduri = string.format(upload_uri, filename, 1); // append } else { return; // upload finished } filecontent = new byte[chunk_size]; int bytesread = data.read(filecontent, 0, chunk_size); data.flush(); webclient wc = new webclient(); wc.openwritecompleted += new openwritecompletedeventhandler(wc_openwritecompleted); uri u = new uri(uploaduri); wc.openwriteasync(u, null, new object[] { filecontent, bytesread }); uploadedbytes += filecontent.length; messagebox.show("check0"); huffman<uint> obj1 = new huffman<uint>(this, filecontent, bitconverter.touint32); messagebox.show("check1"); } void wc_openwritecompleted(object sender, openwritecompletedeventargs e) { progressbar.value = uploadedbytes; if (e.error == null) { object[] objarr = e.userstate object[]; byte[] filecontent = objarr[0] byte[]; int bytesread = convert.toint32(objarr[1]); stream outputstream = e.result; outputstream.write(filecontent, 0, bytesread); outputstream.close(); if (uploadedbytes < totalbytes) { uploadfilechunk(); } else { textbox.text = filename; } } } /// <summary> /// /////////////////////////////////////////////////////// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void showbuttonclick(object sender, routedeventargs e) { if (simpleevent != null) simpleevent(); } private void compressbuttonclick(object sender, routedeventargs e) { } private void closebuttonclick(object sender, routedeventargs e) { } private void combobox1_selectionchanged(object sender, selectionchangedeventargs e) { } private void textbox_textchanged(object sender, textchangedeventargs e) { } private void treeview_selecteditemchanged(object sender, routedpropertychangedeventargs<object> e) { } private void listbox1_selectionchanged(object sender, selectionchangedeventargs e) { } private void usercontrol_loaded(object sender, routedeventargs e) { } private void listbox2_selectionchanged(object sender, selectionchangedeventargs e) { } private void treeviewitem_selected_1(object sender, routedeventargs e) { } private void treeviewitem_selected(object sender, routedeventargs e) { } } public class huffman<t> t : struct, icomparable<t>, iequatable<t> { long length; int size; byte[] data; public class node { public node next, left, right; public t symbol; // symbol of generic type. public int freq; public int is_processed; } public node front, rear; public huffman(mainpage form, byte[] filecontent,func < byte[], int, t > converter) { messagebox.show("check2"); // form.simpleevent += () => showclick(form,filecontent); length = 0; front = null; rear = null; messagebox.show("check4"); length = filecontent.length; size = marshal.sizeof(typeof(t)); stream stream = new memorystream(filecontent); { (long position = 0; position + size < length; position += size) { data = filecontent; t processingvalue = converter(data, 0); { node pt, temp; bool is_there = false; pt = front; while (pt != null) { form.listbox1.visibility = visibility.visible; if (pt.symbol.equals(processingvalue)) { pt.freq++; is_there = true; break; } temp = pt; pt = pt.next; } if (is_there == false) { temp = new node(); temp.symbol = processingvalue; temp.freq = 1; temp.left = null; temp.right = null; temp.next = null; temp.is_processed = 0; if (front == null) { front = temp; } else { temp.next = front; front = temp; } } } } stream.close(); } messagebox.show("yes correctly done"); merge_sort(front); print_tree(front, form); } public node merge_sort(node head) { if (head == null || head.next == null) { return head; } node middle = getmiddle(head); node shalf = middle.next; middle.next = null; return merge(merge_sort(head), merge_sort(shalf)); } public node merge(node a, node b) { node dummyhead, curr; dummyhead = new node(); curr = dummyhead; while (a != null && b != null) { if (a.freq <= b.freq) { curr.next = a; = a.next; } else { curr.next = b; b = b.next; } curr = curr.next; } curr.next = (a == null) ? b : a; return dummyhead.next; } public node getmiddle(node head) { if (head == null) { return head; } node slow, fast; slow = fast = head; while (fast.next != null && fast.next.next != null) { slow = slow.next; fast = fast.next.next; } return slow; } /////// public void print_tree(node treee,mainpage obj) { node pt = treee; while (pt != null) { obj.listbox1.items.add("symbol :" + pt.symbol + " -" + " frequency : " + pt.freq); // debug.writeline("symbol :" + pt.symbol + " -" + " frequency : " + pt.freq); pt = pt.next; } } } }
here xml code :
<usercontrol x:class="finalss.mainpage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:ignorable="d" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" d:designheight="300" d:designwidth="400" loaded="usercontrol_loaded"> <grid x:name="layoutroot" background="wheat" visibility="visible" height="348" width="681"> <grid.columndefinitions> <columndefinition width="37*" /> <columndefinition width="86*" /> <columndefinition width="558*" /> </grid.columndefinitions> <sdk:treeview selecteditemchanged="treeview_selecteditemchanged" margin="12,12,12,41" background="wheat" grid.columnspan="3"> <sdk:treeviewitem header="file" selected="treeviewitem_selected" > <sdk:treeviewitem.items> <sdk:treeviewitem selected="treeviewitem_selected_1"> <sdk:treeviewitem.header> <stackpanel orientation="horizontal" > <button content="browse file" width="76" height="20" click="browsebuttonclick"></button> </stackpanel> </sdk:treeviewitem.header> </sdk:treeviewitem> <sdk:treeviewitem selected="treeviewitem_selected_1"> <sdk:treeviewitem.header> <stackpanel orientation="horizontal"> <button content="show data" width="75" height="20" click="showbuttonclick"></button> </stackpanel> </sdk:treeviewitem.header> </sdk:treeviewitem> <sdk:treeviewitem selected="treeviewitem_selected_1"> <sdk:treeviewitem.header> <stackpanel orientation="horizontal"> <button content="compress" width="75" height="20" click="compressbuttonclick"></button> </stackpanel> </sdk:treeviewitem.header> </sdk:treeviewitem> <sdk:treeviewitem selected="treeviewitem_selected_1"> <sdk:treeviewitem.header> <stackpanel orientation="horizontal"> <button content="close" width="75" height="20" click="closebuttonclick" ></button> </stackpanel> </sdk:treeviewitem.header> </sdk:treeviewitem> </sdk:treeviewitem.items> </sdk:treeviewitem> </sdk:treeview> <progressbar name="progressbar" height="9" horizontalalignment="left" margin="216,82,0,0" verticalalignment="top" width="139" foreground="#ff3ab802" grid.column="2" visibility="collapsed" /> <textbox name="textbox" height="23" horizontalalignment="left" margin="146,68,0,0" verticalalignment="top" width="66" grid.column="2" textchanged="textbox_textchanged" visibility="collapsed" /> <listbox height="148" horizontalalignment="left" margin="0,152,0,0" name="listbox1" verticalalignment="top" width="197" itemssource="{binding}" visibility="collapsed" grid.column="2" selectionchanged="listbox1_selectionchanged"></listbox> <listbox height="148" horizontalalignment="right" margin="0,154,160,0" name="listbox2" verticalalignment="top" width="203" selectionchanged="listbox2_selectionchanged" visibility="collapsed" grid.column="2"> <listboxitem /> <listboxitem /> </listbox> <combobox height="19" horizontalalignment="left" margin="13,204,0,0" name="combobox1" verticalalignment="top" width="104" selectionchanged="combobox1_selectionchanged" grid.columnspan="2"> <comboboxitem /> <comboboxitem /> </combobox> </grid> </usercontrol>
as far surety concerned problem due bitconverter in huffman constructor invoke function func converter creates problem. i guess need use func converter other way , big helping me.
i wrong but...
i don't know think can doing without adding more code -
<t>
is placeholder datatype, used compiler create class works type. class/function created compiler when specify variable class type.
it sounds me trying create type @ runtime , call function. doing have not allowed compiler create method/class support variable type.
you may need declare class each type in dummy file compiler generate classes types.
take @ article more info: http://www.cs.umd.edu/class/fall2002/cmsc214/projects/p2/proj2.temp.html
Comments
Post a Comment