Not about everything

November 25, 2009

Why would a compiler not allow comparing different datatypes?

Filed under: programming,world wide web — takaita @ 19:02
Tags: ,

While writing some code in ASP.Net, I got a run time error where two variables of unequal type were compared.

If varA = varB Then
    ' somecode
End If

By accident varA was a String and varB a Number. One would say, that those variables are unequal and life goes on. But no. The program refused to run. Alarm, Error, Wrong, Impossible.

I am totally puzzled. There is only one reason why this could be: a String and a Number (it was a Long, but that’s too much detail maybe), can not be equal, so comparing them is useless. But then again, comparing Strings of unequal length is fine, even though they can not be equal either, or Strings which start with a different letter.

The point of my code is to evaluate if two things are equal or not. I want an answer. So seriously, my question to all programming gurus around the internet, explain why a language can not answer a simple question, but wants to tell me – as the programmer – that I should not ask.


  1. it’s called static typing

    Comment by niv — November 25, 2009 @ 19:07 | Reply

  2. The compiler is saving you from yourself. Comparing two things that are not similar is usually a mistake and usually a very difficult mistake to debug. So it calls it as an error and you can deal with it as necessary.

    The other reason has to do with how numbers are stored particularly when you get to signed and unsigned numbers. When you look at the bits of a signed number, the negative numbers are actually LARGER than the positive because the first bit being set indicates negative. So what are you comparing: the bit value, the signed value or the unsigned value or did you want me to pretend it’s a character string and see if the characters are identical?

    Languages like C allow you to tell the compiler to treat a variable almost any way that you like so if you have a string variable “str” and an integer variable “integ” you can compare them by:
    if ((int)str == integ)
    do somethin’

    Where (int)str means you want the compiler to pretend the str is actually an integer.

    Comment by Mike Roberts — November 25, 2009 @ 20:20 | Reply

  3. If you really want to compare things without worrying about converting them into the right types you can use “Option Strict Off”. I don’t recommned this for production code, but if you are just messing around it can be handy.

    Comment by grauenwolf — November 25, 2009 @ 21:24 | Reply

  4. 4 = “4”

    is it true or not?

    do you want to convert 4 into “4” or “4” into 4?

    if the language specifically STATES that the language will convert this to this then compare then everything is fine. almost everything deriving from the c language tree will ask you to actually convert these things before comparing them. it saves on silly errors.

    there are functions that take a string and return a number. you should be using these when doing these types of compares. use ‘try’ variants and check for not numbers then assign the results to a number then compare the numbers.

    It’s the right way for your language.

    Comment by Arthur — November 25, 2009 @ 21:43 | Reply

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at

%d bloggers like this: