Serializable class

artlemaks

New member
Joined
Oct 9, 2015
Messages
2
Programming Experience
1-3
Hi all!


I am new to this forum and this is my first post. I am currently taking a diploma course in Visual C# Programming. So far so good! But I got stuck on one of the assignments. This is the assignment:
In memory this information should be managed by objects of class
logon. The items listed in the table above should be represented as attributes of the class. This class should be serializable, and should be stored in the file as logon objects. All logon attempts whether they are successful or not should be recorded in the log file. The log file should be name loginfo.dat. All records should be appended to the end of the file. The file should keep records for a maximum of 10 days. Records older than 10 days old should be regarded as obsolete and should be deleted automatically.
Use LINQ to indentify all records greater than 10 days old.


I have tried many different ways of achieving this, but failed miserably. I tried to follow different online tutorials as well as the course notes but nothing seems to be working for me.


This is the code that I have so far: [C#] [Serializable()] public class Logon { public str - Pastebin.com


Any advice is greatly welcomed!


Thanks!
 
I'm afraid that you're not likely to get much help with a question like that. You really need to isolate one specific issue. If you want to post code then post only the relevant code directly in your post. If there's too much code for that then you need to narrow down the issue. For instance, we don't even know whether you can serialise an object, regardless of anything else. You need to divide and conquer, i.e. break the problem down into parts and address each part individually. Any specific issues you encounter along the way should all be addressed in separate threads.
 
Sorry for not being specific enough. I've been working on this hard over the weekend and managed to get the object serialized.
I am still struggling with the last part of this question which is: "All records should be appended to the end of the file. The file should keep records for a maximum of 10 days. Records older than 10 days old should be regarded as obsolete and should be deleted automatically. Use LINQ to indentify all records greater than 10 days old."
I presume that I have to deserialize the .dat file first, which I have done:
C#:
[COLOR=#000088]public [/COLOR][COLOR=#000088]static [/COLOR][COLOR=#000088]void [/COLOR][COLOR=#660066]LoadFromFile[/COLOR][COLOR=#666600]()[/COLOR][COLOR=#000000]
         [/COLOR][COLOR=#666600]{[/COLOR][COLOR=#000000]
            [/COLOR][COLOR=#880000]//create a FileStream to open the .dat file[/COLOR][COLOR=#000000]
            [/COLOR][COLOR=#660066]FileStream[/COLOR][COLOR=#000000] fileStream [/COLOR][COLOR=#666600]=[/COLOR][COLOR=#000088]new[/COLOR][COLOR=#660066]FileStream[/COLOR][COLOR=#666600]([/COLOR][COLOR=#008800]"loginfo.dat"[/COLOR][COLOR=#666600],[/COLOR][COLOR=#660066]FileMode[/COLOR][COLOR=#666600].[/COLOR][COLOR=#660066]Open[/COLOR][COLOR=#666600]);[/COLOR][COLOR=#000000]
            [/COLOR][COLOR=#880000]//create a BinaryFormatter to deserialize the object[/COLOR][COLOR=#000000]
            [/COLOR][COLOR=#660066]BinaryFormatter[/COLOR][COLOR=#000000] formatter [/COLOR][COLOR=#666600]=[/COLOR][COLOR=#000088]new[/COLOR][COLOR=#660066]BinaryFormatter[/COLOR][COLOR=#666600]();[/COLOR][COLOR=#000000]
            [/COLOR][COLOR=#660066]StreamReader[/COLOR][COLOR=#000000] objReader [/COLOR][COLOR=#666600]=[/COLOR][COLOR=#000088]new[/COLOR][COLOR=#660066]StreamReader[/COLOR][COLOR=#666600]([/COLOR][COLOR=#008800]"loginfo.txt"[/COLOR][COLOR=#666600]);[/COLOR][COLOR=#000000]
            [/COLOR][COLOR=#000088]var[/COLOR][COLOR=#000000] cutoff [/COLOR][COLOR=#666600]=[/COLOR][COLOR=#660066]DateTime[/COLOR][COLOR=#666600].[/COLOR][COLOR=#660066]Now[/COLOR][COLOR=#666600].[/COLOR][COLOR=#660066]AddDays[/COLOR][COLOR=#666600](-[/COLOR][COLOR=#006666]10[/COLOR][COLOR=#666600]);[/COLOR][COLOR=#000000]
            [/COLOR][COLOR=#660066]List[/COLOR][COLOR=#666600]<[/COLOR][COLOR=#660066]DateTime[/COLOR][COLOR=#666600]>[/COLOR][COLOR=#000000] dates [/COLOR][COLOR=#666600]=[/COLOR][COLOR=#000088]new[/COLOR][COLOR=#660066]List[/COLOR][COLOR=#666600]<[/COLOR][COLOR=#660066]DateTime[/COLOR][COLOR=#666600]>();[/COLOR][COLOR=#000000]
            [/COLOR][COLOR=#000088]while[/COLOR][COLOR=#666600]([/COLOR][COLOR=#000000]fileStream[/COLOR][COLOR=#666600].[/COLOR][COLOR=#660066]Length[/COLOR][COLOR=#666600]!=[/COLOR][COLOR=#000000] fileStream[/COLOR][COLOR=#666600].[/COLOR][COLOR=#660066]Position[/COLOR][COLOR=#666600])[/COLOR][COLOR=#000000]
            [/COLOR][COLOR=#666600]{[/COLOR][COLOR=#000000]
               [/COLOR][COLOR=#880000]//deserialize the object from the .dat file[/COLOR][COLOR=#000000]
               [/COLOR][COLOR=#660066]Logon[/COLOR][COLOR=#000000] deserializedSample [/COLOR][COLOR=#666600]=[/COLOR][COLOR=#666600]([/COLOR][COLOR=#660066]Logon[/COLOR][COLOR=#666600])[/COLOR][COLOR=#000000]formatter[/COLOR][COLOR=#666600].[/COLOR][COLOR=#660066]Deserialize[/COLOR][COLOR=#666600]([/COLOR][COLOR=#000000]fileStream[/COLOR][COLOR=#666600]);[/COLOR][COLOR=#000000]
            [/COLOR][COLOR=#666600]}[/COLOR][COLOR=#000000]
           
            fileStream[/COLOR][COLOR=#666600].[/COLOR][COLOR=#660066]Close[/COLOR][COLOR=#666600]();[/COLOR][COLOR=#000000]
           
            
            [/COLOR][COLOR=#880000]//Console.Read();[/COLOR][COLOR=#000000]

         [/COLOR][COLOR=#666600]}
[/COLOR]
And it works fine. I am stuck on the next step! Should I create a list in which I will put all the deserialized data and then do a LINQ query on it? If that's the case, how do I then remove the lines that should be removed? As you can see, I had a list of dates created, I was loading the data into it in the while loop, and then I was able to do use LINQ to pick out the dates that are older than 10 days. But that didn't help me with figuring out how to, or which lines exactly should be removed.
 
Back
Top Bottom