Friday, September 14, 2007

Integer Palindromes

File this one under "Silly, Simple Code Snippet Of The Day"™...

Several times on various programming forums, I've seen question about how to find out if an integer value is a palindrome or not (the number is the same "forwards" and "backwards"; e.g., 71317 would be a palindrome, while 71316 would not). Most of the suggested solutions revolve around converting the integer value to a string and reversing the characters. This makes sense, given that "71317" is just a character representation of a number, and many language APIs have ready-made facilities for reversing strings of characters (or at least arrays). That said, finding out if a number is a palindrome using basic math is dead simple, too. Here's a base-10 example in C#:
    using System.Diagnostics;

class ScratchUtils
{
public static int Reverse(int i)
{
int result = 0;
while (i != 0)
{
result = (result * 10) + (i % 10);
i /= 10;
}
return result;
}

public static bool IsPalindrome(int i)
{
return i == Reverse(i);
}

public static void Main()
{
Debug.Assert( 0 == Reverse(0));
Debug.Assert( 1 == Reverse(1));
Debug.Assert( 21 == Reverse(12));
Debug.Assert(-21 == Reverse(-12));

Debug.Assert(IsPalindrome(1));
Debug.Assert(IsPalindrome(11));
Debug.Assert(!IsPalindrome(12));
Debug.Assert(IsPalindrome(121));
}
}

Tuesday, September 11, 2007

Java - Cannot Resolve Symbol

You get a "cannot resolve symbol" message because the compiler doesn't recognize something you've typed. It's as if you instructed your friend to "fernt PL^%", to which s/he would likely reply, "Huh?". The compiler errors will tell exactly which symbols it does not recognize; correct these errors by properly defining variables, importing the correct classes, implementing the appropriate methods, etc.

Sunday, September 02, 2007

.NET - Connecting to SQL Server Express from VWDE

I was trying a tutorial for Visual Web Developer Express, and kept running into problems connecting to SQL Server Express. When I tried to configure the security settings by clicking on the security tab in the ASP.NET Web Site Administration Tool, I received the following error:
There is a problem with your selected data store. This can be caused by an invalid server name or credentials, or by insufficient permission. It can also be caused by the role manager feature not being enabled. Click the button below to be redirected to a page where you can choose a new data store.

After researching a bit, I used the SQL Server Configuration Manager to use "Local System" as the built-in account (found under the [Log On] tab for the running SQL Server Express instance). Then, I had to add a Web.config to my project with the following connection strings:

<connectionStrings>
<remove name="LocalSqlServer"/>
<add name="LocalSqlServer"
connectionString="Data Source=localhost;
Initial Catalog=aspnetdb;
Integrated Security=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>


After that, the connection succeeded, and I was able to continue with the tutorial.