Random programming things I'd want to remember

Monday, March 26, 2012

How to spell ti and di in katakana/Как написать "ти" и "ди" катаканой.

てぃ,ティ:
Type "texi" или "thi", no spaces, no quotes.
Печатайте "texi" или "thi", без кавычек.

でぃ,ディ:
Type "dexi" или "dhi", no spaces, no quotes.
Печатайте "dexi" или "dhi", без кавычек.

Sunday, March 25, 2012

How to order a collection by a custom property

Suppose you have the following code:

public class TestItem { public string Name { get; set; } public int Count { get; set; } } public partial class MainPage : PhoneApplicationPage { // Constructor public MainPage() { InitializeComponent(); List<TestItem> li = new List<TestItem>(); TestItem a = new TestItem { Name = "First name", Count = 1 }; TestItem b = new TestItem { Name = "Second name", Count = 3 }; TestItem c = new TestItem { Name = "Third name", Count = 2 }; //But how do I order the items in the collection by Count property? listBox1.DataContext = li; } }
If I want to order the collection before binding it to the listbox, how do you do that? This is how:

li.Sort((a, b) => a.SortingParam.CompareTo(b.SortingParam));

Wednesday, March 21, 2012

Error: To generate an event handler the class 'MainPage', specified by the x:Class or x:Subclass...

I was testing something out on the Windows Phone 7 platform and, when tried to add a button Click event, Visual Studio 2010 gave me the following error: To generate an event handler the class 'MainPage', specified by the x:Class or x:Subclass attribute, must be the first class in the file. Move the class code so that it is the first class in the file and try again. This is what my MainPage.xaml.cs file looked like:

public class TestItem { public string Name { get; set; } public int Count { get; set; } } public partial class MainPage : PhoneApplicationPage { // Constructor public MainPage() { InitializeComponent(); List<TestItem> li = new List<TestItem>(); TestItem a = new TestItem { Name = "First name", Count = 1 }; TestItem b = new TestItem { Name = "Second name", Count = 2 }; TestItem c = new TestItem { Name = "Third name", Count = 3 }; listBox1.DataContext = li; } }
Then I realized that I put together a dummy class to test binding on a ListBox and put it on the top of the document. Once I moved the TestItem class definition down, the page compiled and I was able to add events from the XAML page. Here is what the working code looks like:

public partial class MainPage : PhoneApplicationPage { // Constructor public MainPage() { InitializeComponent(); List<TestItem> li = new List<TestItem>(); TestItem a = new TestItem { Name = "First name", Count = 1 }; TestItem b = new TestItem { Name = "Second name", Count = 2 }; TestItem c = new TestItem { Name = "Third name", Count = 3 }; listBox1.DataContext = li; } private void btn1_Click(object sender, RoutedEventArgs e) { } } public class TestItem { public string Name { get; set; } public int Count { get; set; } }

Friday, March 16, 2012

Scanning computer with Clam antivirus using System Rescue CD

System Rescue CD (SRCD) has recently become a very important tool in my IT specialist toolbox. Not only this is a live Linux CD with all the goodies (such as gparted, Windows registry editor and so on), it also packs some command-line tools, such as the NTPASSWD utility, or a utility to edit the master boot record on a hard drive.  This post will describe the process of scanning a drive for viruses using Clam Anti-Virus.

1. Boot into SRCD, option 1.
2. Enter (for default keyboard time)v
3. Net-setup eth0 , press Enter
4. Yes
5. 2 (Wired)
6. 1 (DHCP)
7. ifconfig (to check for an ip address)
8. freshclam (this command pulls the updated virus definitons from the server)
9. fdisk -l (that is an L)
10. ntfs3g /dev/sda1 /mnt/windows
11. clamscan (--help)
12. clamscan -r /mnt/windows

Wednesday, March 7, 2012

Objective-C: Imitation of C# "String.Contains()" method

The method returns a YES or a NO:

Method signature:
- (BOOL) containsString:(NSString *) string
                options:(NSStringCompareOptions) options;

Method body:
- (BOOL) containsString:(NSString *) string
                options:(NSStringCompareOptions) options {
    NSRange rng = [self rangeOfString:string options:options];
    return rng.location != NSNotFound;
}

If NSStringCompareOptions make no difference, a 0 can be passed to the method.

Tuesday, March 6, 2012

Windows phone review after a month of use by an iPhone user (Samsung SGH-i917)


I have a Samsung SGH-i917 (a Windows 7 phone) and I would like to share my experience of using it in case someone is thinking about getting a Windows 7 phone. For the record, I have used an iPhone for several years and it influenced my mobile phone usage. I am not, in any way, against Windows 7 Phone platform, I just want to point out things that could use some more work.

Turning on the phone. This phone is light and thin, which makes it harder to find the power button, and that is the only way to turn on the phone or wake it up. It is hard to tell in the dark which way is up or down, that may increase the time needed to find the power button.

E-mail setup. For some reason, I have to set up the phone with a Windows Live ID. I have an old e-mail associated with Windows Live ID, it has a lot of my old contacts. The phone insisted that I associate a Live ID with it. Ok, I did that. It immediately imported all my contacts from that phone. It would be nice if the phone asked me if I want all those contacts imported. Now I am stuck with a long list of people in my phone that I no longer even talk to. Setting up additional e-mails is very easy. Getting rid of e-mail accounts is a bit tricky. First of all, I cannot get rid of the Live ID e-mail account. Second, if I need to get rid of any other e-mail, I had to click and hold the e-mail account to get the context menu. It took me a bit to figure that out, it would be nice to have a hint or buttons on the bottom. Also, with e-mail (I tried hotmail and gmail), if I delete an e-mail on the phone, it would still be in the mailbox once I use the computer to log in. A matter of preference, really.

Adding a phone contact. This process is the most frustrating for me. I usually type the phone number, press save, expect to type in the name and maybe some other info and then I am done. Here are the steps that one need to take to save a phone number on Windows 7 Phone:
1. Type in the phone number (ok so far).
2. Hit save button. Boom! I suddenly see my contact list. Why? Oh, they assume that my friends buy new phone numbers every week. No, actually my contacts are pretty conservative on their phone numbers. So how do I add a new number? Oh, there is a small plus button on the bottom.
3. Press the + button on the bottom.
4. I see the phone number box, I see the type of the phone. Where do I type in the name? And the number keyboard takes up half the screen. But I already typed in the number! Oh, there is a button on the bottom that looks like a … floppy disk? Who remembers what those are anymore?
5. Let's hit the "floppy disk" button. I see the page, big box to add photo, I see a label for the name, mobile phone (and the phone itself is in the tiny numbers, so if I fat-fingered it, no way I can notice the mistake), phone, email, … how do I type in stuff? Oh, there is a "plus" button to the left of each label, while I was expecting a text box…
6. Let's hit the "plus" button. Yay! I can type in the name! And the floppy disk icon on the button again… oh, they used it to save data before, that symbol must mean "save"!
7. Type the name
8. Hit the floppy disk button to save the name
9. Hit the floppy disk button to save the contact
10. Yay, I saved a contact! The phone number is still small, but it is a very important piece of info.

One contact, but I had to hit save button four times! My ideal way to save a contact is the following:

1. Type in the phone number
2. Hit save button
3. Get presented with fields for the new contact (phone number must be in BIG numbers so that I can double-check it with whoever is giving it to me). If I need to add the phone number to an existing contact, give me a button in top right corner. 
4. Fill in all the info
5. Hit save button.

Wi-Fi. I hooked up the phone to wireless internet, that was easy. However, the phone only displays the Wi-Fi connection icon on the home screen. When I am browsing Internet, I don't see the Wi-Fi icon and I am not sure whether I use my cellular plan or Wi-Fi to get that web page. Same with e-mail. It just says "Syncing", but does not show what connection it uses. This matters for me, I was using a prepaid cell phone plan, did not want to run out of money.

Battery indicator. The phone has information on the remaining battery in percents, as well as approximate time of service remaining until it absolutely needs to be charged. The battery icon in top right, however, cannot be customized to display percents. This is also a matter of preference, but I would prefer to see the percentage of remaining battery power.

Search. The built-in search feature is mostly nice. If I search for "weather" it gives me weather at a glance in my location. But when I try searching for "las vegas weather" (I am not in Las Vegas), it gets stuck searching. I tried restarting search and still no answer. Why? Not sure. May be this is a hardware issue. The other nice feature is built-in search for music, scanner, and voice-activated search. However, there are still issues here. I was listening to the radio and used the "search for music" feature. The radio kept playing and the phone kept listening. I cancelled the searched after about a minute. I sure wasn't turning the radio off. Text/barcode/QR code scanner works great. Oh, one other thing on the search: splitting results into web/local/images, what is "local" supposed to be? I am searching for "weather", swipe to local and I see a small piece of map (I can't make out what that location is, anyway, the map is too small), and the map points to the place called "Time Temperature & Weather". Not sure what it is. Works somewhat better when I search for "farmers market", the results make a bit more sense.

Maps, directions. There are two issues here. First, after I found my route and just want to browse the map along the way, the map keeps returning me where it thinks I am at at the moment. I just want to browse the map before I drive, to see the traffic and other things, don't change my map! The other thing about this area is that once I get directions to some place and hit the back button, directions disappear. That is ok. Another scenario: I find directions, move the maps application to the background, then start it again and hit the back button, what happens? Right, instead of clearing the directions from the map, the phone shows me the screen that I was at before.

Buttons. Home key, once pressed, provides slight vibration feedback; nice feature.

Size. The phone feels wide, thin and light.

Camera. Nice camera, takes good pictures, have some additional features, such as effects and even the white balance correction.

Marketplace. Feels cluttered. When I am choosing an item, I would like to see how much it costs right away, without having to tap on it.

Overall, I think this phone and the platform itself could use some more work. Great job on the concept though, I very much like the fact that I can use C# and put my programs right on to the phone.

Saturday, March 3, 2012

XCode 4.2: how to provide arguments to a command-line app

Here is how to provide arguments to a program in XCode 4.2:
Product -> Edit Scheme

Click on "Run" in the left-side part of the dialog box. If needed, switch to "Arguments" tab in the middle part of the dialog box. Under "Arguments Passed On Launch", press "+" and add a value. Then you can access the value using the following code:
NSProcessInfo *proc = [NSProcessInfo processInfo];
NSArray *args = [proc arguments];
NSString *myArg = [args objectAtPosition: 1];

One note worth mentioning: if your argument contains spaces, enclose it in quotes: "my argument".

Thursday, March 1, 2012

Compile Objective-C code From Terminal With ARC Using xcodebuild

I was perfectly happy compiling Objective-C from the command line until I hit a snag. As a result, I found out about xcodebuild from Joshua Smith . I am still in the process of researching this tool, but so far I use it this way:

xcodebuild -project yourprojectname.xcodeproj -alltargets


and then I use the
./yourprojectname 

command to start the app (I can customize the name of the executable in the project settings).

One thing to note: the xcodebuild command needs the .xcodeproj file to run, so you need to provide the path to the .xcodeproj file when issuing the command.