c# - Incorrect reading data from .bin file -
i have 2 programs: 1 test students take , second teachers(teachers create variants of test , add questions it); teacher program creates .bin files tests , student's open , take tests. data structure(class question
) similiar in both programs; teacher's program class code:
[serializable] public class question { public question(string q_text, dictionary<string, bool> ans, image img) { text = q_text; answers = ans; image = img; isanswered = false; } public string text { get; set; } public dictionary<string, bool> answers { get; set; } public image image { get; set; } public bool isanswered; public static void persistobject(dictionary<int, question> q, stream stream) { binaryformatter formatter = new binaryformatter(); formatter.serialize(stream, q); stream.close(); } public static dictionary<int, question> loadobject(stream stream) { try { binaryformatter formatter = new binaryformatter(); dictionary<int, question> deserializedobject = (dictionary<int, question>)formatter.deserialize(stream); stream.close(); return deserializedobject; } catch { return null; } } } }
and student's program class code:
[serializable] class question { public question(string text, image img, dictionary<string, bool> ans) { question_text = text; image = img; answers = ans; isanswered = false; } public string question_text { get; set; } public dictionary<string, bool> answers { get; set; } public image image { get; set; } public bool isanswered; public static dictionary<int, question> loadobject(stream stream) { try { binaryformatter formatter = new binaryformatter(); dictionary<int, question> deserializedobject = (dictionary<int, question>)formatter.deserialize(stream); stream.close(); return deserializedobject; } catch { return null; } } }
but when try read test in student program:
string file = path + test_number + ".bin"; stream mystream = file.open(file, filemode.open); questions = question.loadobject(mystream);
it sets questions
null. when read files in teachers program it's ok.(perhaps, it's ok, because create files in teachers mode too). type of questions
dictionary<int,question>
.what's problem?
Comments
Post a Comment