Console.WriteLine("Hello World!");
Random programming things I'd want to remember
Sunday, August 1, 2010
Friday, July 30, 2010
Item loader
In order to fill the data into the ItemTester, I will write another application, ItemLoader. Here are the requrements:
The program, ItemLoader, is an application that helps create the database of question/answer pairs for ItemTester.
Functional Requirements.
The program will:
1. allow the user to enter question/answer pairs one-by-one (also requiring topic(s) for an item).
2. allow the user to process a batch file in a predefined format.
3. check the new words against the database to make sure this question/answer pair does not already exist.
The format for the batch file:
Question///Answer///tag1;tag2;tag3
3. write out the user's input into the XML file.
The program, ItemLoader, is an application that helps create the database of question/answer pairs for ItemTester.
Functional Requirements.
The program will:
1. allow the user to enter question/answer pairs one-by-one (also requiring topic(s) for an item).
2. allow the user to process a batch file in a predefined format.
3. check the new words against the database to make sure this question/answer pair does not already exist.
The format for the batch file:
Question///Answer///tag1;tag2;tag3
3. write out the user's input into the XML file.
A new app
I decided to write an application to learn new foreign words. The app will be pretty simple -- it will offer you a word and four choices of what it can be, you have to choose the right answer. In order to do things right, I want to start with the Requirements Document. This will be my contract and I will stick to it.
Functional Requirements
The application must help the user train to learn new words. The interface should offer the user a question and four possible answers. The user must choose one of the four answers, if he/she gets it right, the application updates the number-correct field. Once the user is consistently right, the word is shown less and less.
The application will:
1. read the question/answer pairs from storage, load them into the memory.
2. distinguish the question/answer pairs by topics. A question/answer pair may belong to multiple topics.
3. offer the user the choice of topics. A user can choose one or more topics to practice.
4. keep track of the percentage of the correct answers, incrementing/decrementing on each answer on the word.
5. ask user what is the threshold after which he/she does not want to see the word anymore.
6. offer a "refresher" mode for those words whose number-correct ratio is above the threshold.
7. offer the user a choice of four answers, one of which is correct, and the other three are the answers for items belonging to the same topic.
8. shuffle the positions of the possible answers. If the topic has less than 4 items total, the application must select other wrong answers from any other topics.
Database requirements:
XML file will suffice.
Other requirements:
The program must be initially implemented in Windows forms using LINQ. The program's functionality must be abstracted such that it is easy to migrate it to an ASP.Net application.
The program will be written in .Net, C#.
The program must be easy to use.
Functional Requirements
The application must help the user train to learn new words. The interface should offer the user a question and four possible answers. The user must choose one of the four answers, if he/she gets it right, the application updates the number-correct field. Once the user is consistently right, the word is shown less and less.
The application will:
1. read the question/answer pairs from storage, load them into the memory.
2. distinguish the question/answer pairs by topics. A question/answer pair may belong to multiple topics.
3. offer the user the choice of topics. A user can choose one or more topics to practice.
4. keep track of the percentage of the correct answers, incrementing/decrementing on each answer on the word.
5. ask user what is the threshold after which he/she does not want to see the word anymore.
6. offer a "refresher" mode for those words whose number-correct ratio is above the threshold.
7. offer the user a choice of four answers, one of which is correct, and the other three are the answers for items belonging to the same topic.
8. shuffle the positions of the possible answers. If the topic has less than 4 items total, the application must select other wrong answers from any other topics.
Database requirements:
XML file will suffice.
Other requirements:
The program must be initially implemented in Windows forms using LINQ. The program's functionality must be abstracted such that it is easy to migrate it to an ASP.Net application.
The program will be written in .Net, C#.
The program must be easy to use.
Sunday, January 17, 2010
My resolutions for 2010
1. Program one substantial application using a functional language
2. Read 10 programming books:
- Real World Haskell
- Functional programming tutorial
- Pragmatic programmer
- Code Complete
- Microsoft TDD book
- Structure and Interpretation of computer programs
- Review Joe Celko's SQL for Smarties
- Read Joe Celko's SQL Puzzles & Answers
+ others (at least two).
3. Learn basic Japanese
4. Enrich my cooking skills
5. Work out at least 2 times a week
6. Find an interesting job
2. Read 10 programming books:
- Real World Haskell
- Functional programming tutorial
- Pragmatic programmer
- Code Complete
- Microsoft TDD book
- Structure and Interpretation of computer programs
- Review Joe Celko's SQL for Smarties
- Read Joe Celko's SQL Puzzles & Answers
+ others (at least two).
3. Learn basic Japanese
4. Enrich my cooking skills
5. Work out at least 2 times a week
6. Find an interesting job
Monday, May 11, 2009
How to edit a GridView items inside a repeater?
You know, the usual:
<asp:Repeater ...
<itemtemplate>
<asp:gridview id="gv1" runat="server">
</itemtemplate>
</asp:Repeater>
When I was working on my app, I had a long gridview of multiple records per day, multiple days, all one list. When it was 10 records, it was ok. But as I started adding data, it started looking long and boring. I decided to group it by day, and it looked nice until I had to edit individual records. I did not change any event declarations from before, my methods had the same names, and they seem to be working, but none of the gridviews would change their edititemindexes. I checked out a couple of posts and people had the same issues, they assign events, they get executed, but gridviews would not react in any way.
The problem is happening because every time I'd call .DataBind method on the Repeater (then calling the .DataBind method on individual gridviews in the repeater's ItemDataBound event), the entire Repeater would get rebuilt and the EditItemIndex property on that particular gridview would not matter anymore because the server would not know which gridview needed to be edited.
The trick was relatively easy. Instead of rebinding the entire repeater, only that particular gridview needed to be re-databound. I figured out a way to pull just those particular day's records and my GridView_RowEditing event looks like:
protected void GridView1_RowEditing(object sender, GridViewEventArgs e)
{
GridView gv1 = (GridView)sender;
if(gv1 != null)
{
gv1.EditIndex = e.NewEditIndex;
gv1.DataSource = myDataSource;
gv1.DataBind();
}
}
Instead of binding the entire Repeater and losing all valuable information, I just bound that particular gridview by casting the sender object as a GridView. RowCancelingEvent and RowUpdatingEvent -- same thing. Cast the sender object and work with it as much as you need.
<asp:Repeater ...
<itemtemplate>
<asp:gridview id="gv1" runat="server">
</itemtemplate>
</asp:Repeater>
When I was working on my app, I had a long gridview of multiple records per day, multiple days, all one list. When it was 10 records, it was ok. But as I started adding data, it started looking long and boring. I decided to group it by day, and it looked nice until I had to edit individual records. I did not change any event declarations from before, my methods had the same names, and they seem to be working, but none of the gridviews would change their edititemindexes. I checked out a couple of posts and people had the same issues, they assign events, they get executed, but gridviews would not react in any way.
The problem is happening because every time I'd call .DataBind method on the Repeater (then calling the .DataBind method on individual gridviews in the repeater's ItemDataBound event), the entire Repeater would get rebuilt and the EditItemIndex property on that particular gridview would not matter anymore because the server would not know which gridview needed to be edited.
The trick was relatively easy. Instead of rebinding the entire repeater, only that particular gridview needed to be re-databound. I figured out a way to pull just those particular day's records and my GridView_RowEditing event looks like:
protected void GridView1_RowEditing(object sender, GridViewEventArgs e)
{
GridView gv1 = (GridView)sender;
if(gv1 != null)
{
gv1.EditIndex = e.NewEditIndex;
gv1.DataSource = myDataSource;
gv1.DataBind();
}
}
Instead of binding the entire Repeater and losing all valuable information, I just bound that particular gridview by casting the sender object as a GridView. RowCancelingEvent and RowUpdatingEvent -- same thing. Cast the sender object and work with it as much as you need.
Wednesday, November 12, 2008
Project Euler in F#, Problem 6
Here's the code:
let square x = x * x
let list1 = [1 .. 10]
let rec sum list =
match list with
| h::tail -> (sum tail) + h
| []->0
let rec sumSquaredListElements list =
match list with
| h::tail -> (sumSquaredListElements tail) + h * h
| [] -> 0
printfn "%A" list1
let print x = printfn "%i" x
let x1 = square (sum list1)
let main () = print x1
main()
let square x = x * x
let list1 = [1 .. 10]
let rec sum list =
match list with
| h::tail -> (sum tail) + h
| []->0
let rec sumSquaredListElements list =
match list with
| h::tail -> (sumSquaredListElements tail) + h * h
| [] -> 0
printfn "%A" list1
let print x = printfn "%i" x
let x1 = square (sum list1)
let main () = print x1
main()
Tuesday, November 11, 2008
Learning a functional language
A challenge in learning a language is application of the newly-acquired knowledge. A wonderful web site, "Project Euler," presents a number of mathematical puzzles. I could learn F# by solving those problems. Here's the answer to problem number 1:
#light
open System
let by32 = List.filter(fun n -> n%3 = 0 || n%5 = 0) [1 .. 9]
let rec sum list =
match list with
| h::tail -> (sum tail) + h
| []->0
printfn "%A" by32
let print x = printfn "%i" x
let x1 = sum by32
let main () = print x1
main()
Console.ReadKey(true)
#light
open System
let by32 = List.filter(fun n -> n%3 = 0 || n%5 = 0) [1 .. 9]
let rec sum list =
match list with
| h::tail -> (sum tail) + h
| []->0
printfn "%A" by32
let print x = printfn "%i" x
let x1 = sum by32
let main () = print x1
main()
Console.ReadKey(true)
Wednesday, March 19, 2008
Unit testing in Visual Studio 2008
I wanted to try out TDD (Test-Driven Development) for a long time. I checked out NUnit, even got a book on it (read just 1 chapter and got the idea, since then read 3 more chapters :)... Time passed since then and due to some other reasons I haven't touched the subject. Now I have the time and I decided to explore TDD again. This series of posts will describe my experience.
1. Unit testing (why it's here -- below).
When I read about TDD before, I also read about unit testing tools that are in VS 2008, but I did not have a chance to play with them at the time. Now I explored what they are and they aren't quite TDD, they are unit tests. The difference between TDD and unit testing is that in TDD tests drive development -- tests are written before the actual features. Unit testing can happen after a feature is put in. VS2008 even has a nice test generator based on the current code. The time spent exploring unit testing was definitely worth it, but I want to develop something using TDD.
Below is the brief rundown on unit testing. By the way, I found a very good article on unit testing that covers what I am about to describe in greater detail. The only other thing I wish it covered is how to get the code coverage analysis from within VS2008, but I think it's just a matter of pushing a couple of buttons in the IDE. By the way, code coverage was something that made me use unit testing tools in VS2008 in the first place, I wanted to see the lines of code that are tested and those that aren't.
So here it comes:
I have an idea in mind for a simple app. A shopping list -- I want to be able to put in new products in the list and check them off when I have them and mark them for purchase when I am out (note to self -- get eggs!). I decided that that would be a good place to exercise the red-green-refactor metaphor. I fired up VS2008, created a simple web project, and then pushed the magic button to generate another project in the same solution for unit tests. Then I created two simple tests to check the database connectivity (I will list somewhat the same code later so I don't put it in now to not repeat myself) and was happy.
Not for long though -- I put in the test code right inside a class the test project, and I did not use any of the web site's classes for it. I sure was checking the connectivity, but that's just testing.
In order to address that, I created a SimpleDataLayer class in the web site, added a simple method to return a SQL connection and wanted to instantiate it from the test project. I could not. It turns out that I created the site in the regular place where my all projects reside (and I sure did not change it in the VS settings -- TODO!), but the tests project got created under Documents/User/... blah. So I could not add a reference and keep testing. I searched google and found on MSDN an option to create Unit Tests (open up the class, right-click in code, Create Unit Tests) and whoa!!! A class appeared and it sure had a lot of things I did not know about (the article explains it nicely).
As you can see, there is now a "ShoppingListSite.accessor," which in fact, has nothing but a path to the web site and the word "WebSite" in it. It must be just a pointer.
Here is what a test looks like:

What exactly is the "SimpleDataLayer_Accessor"? It turns out that it is sort of a proxy between the private class SimpleDataLayer and the one containing the test methods. An instance of the SimpleDataLayer_Accessor can be referred to in the same way as if it was an instance of a SimpleDataLayer.
Another interesting point is that a test is generated inconclusive by default. I think it's a clever idea when someone is using the automated generation of unit tests against a large project, when such a test is run it'll turn out as neither failed nor passed but rather inconclusive.
That's it for now, I'll keep you posted once I make more progress on it.
1. Unit testing (why it's here -- below).
When I read about TDD before, I also read about unit testing tools that are in VS 2008, but I did not have a chance to play with them at the time. Now I explored what they are and they aren't quite TDD, they are unit tests. The difference between TDD and unit testing is that in TDD tests drive development -- tests are written before the actual features. Unit testing can happen after a feature is put in. VS2008 even has a nice test generator based on the current code. The time spent exploring unit testing was definitely worth it, but I want to develop something using TDD.
Below is the brief rundown on unit testing. By the way, I found a very good article on unit testing that covers what I am about to describe in greater detail. The only other thing I wish it covered is how to get the code coverage analysis from within VS2008, but I think it's just a matter of pushing a couple of buttons in the IDE. By the way, code coverage was something that made me use unit testing tools in VS2008 in the first place, I wanted to see the lines of code that are tested and those that aren't.
So here it comes:
I have an idea in mind for a simple app. A shopping list -- I want to be able to put in new products in the list and check them off when I have them and mark them for purchase when I am out (note to self -- get eggs!). I decided that that would be a good place to exercise the red-green-refactor metaphor. I fired up VS2008, created a simple web project, and then pushed the magic button to generate another project in the same solution for unit tests. Then I created two simple tests to check the database connectivity (I will list somewhat the same code later so I don't put it in now to not repeat myself) and was happy.
Not for long though -- I put in the test code right inside a class the test project, and I did not use any of the web site's classes for it. I sure was checking the connectivity, but that's just testing.
In order to address that, I created a SimpleDataLayer class in the web site, added a simple method to return a SQL connection and wanted to instantiate it from the test project. I could not. It turns out that I created the site in the regular place where my all projects reside (and I sure did not change it in the VS settings -- TODO!), but the tests project got created under Documents/User/... blah. So I could not add a reference and keep testing. I searched google and found on MSDN an option to create Unit Tests (open up the class, right-click in code, Create Unit Tests) and whoa!!! A class appeared and it sure had a lot of things I did not know about (the article explains it nicely).
As you can see, there is now a "ShoppingListSite.accessor," which in fact, has nothing but a path to the web site and the word "WebSite" in it. It must be just a pointer.Here is what a test looks like:

What exactly is the "SimpleDataLayer_Accessor"? It turns out that it is sort of a proxy between the private class SimpleDataLayer and the one containing the test methods. An instance of the SimpleDataLayer_Accessor can be referred to in the same way as if it was an instance of a SimpleDataLayer.
Another interesting point is that a test is generated inconclusive by default. I think it's a clever idea when someone is using the automated generation of unit tests against a large project, when such a test is run it'll turn out as neither failed nor passed but rather inconclusive.
That's it for now, I'll keep you posted once I make more progress on it.
Subscribe to:
Comments (Atom)
Labels
508
(2)
accessibility
(2)
angularjs
(2)
aspnet
(2)
BackgroundWorker
(2)
benchmarking
(1)
binaryreader
(1)
bootable
(1)
c#
(40)
canvas
(1)
checkbox
(1)
code snippet
(1)
config
(1)
controls
(1)
data manipulation
(2)
debugging
(1)
embeddedresource
(3)
entity-framework
(2)
Excel
(1)
f#
(2)
firefox
(1)
foreign language
(1)
git
(2)
hardware
(1)
html
(1)
iso
(1)
ItemLoader
(1)
ItemTester
(1)
japanese
(1)
javascript
(3)
knockout
(1)
ms-access
(2)
multithreading
(3)
mvc
(1)
mvvm
(7)
objective-c
(10)
office
(1)
powershell
(1)
productivity
(1)
programming
(53)
project_euler
(2)
radiobutton
(1)
reflection
(3)
resolution
(1)
shortcut
(1)
silverlight
(3)
source control
(1)
sql
(2)
system administration
(2)
system rescue cd
(1)
tab
(1)
tdd
(4)
tip
(2)
UI
(1)
usb
(1)
visualstudio
(6)
visualstudio2008
(1)
webapi
(1)
webdev
(2)
windows
(1)
windows-phone
(4)
windows-phone-7
(8)
winforms
(4)
wpf
(4)
xaml
(8)