Convert.ToInt32 vs CInt

This is a search subject I’ve done quite a few times in the past and never really found a good answer. I’ve found results either asking the same question and debate/bicker threads about the obsolete-ness of CInt, but none that describe the difference between the two. Plenty of “if you try to convert X using Y, Z will happen if you don’t W X first” that don’t give an explanation.

A recent epiphany finally lifted the veil on the question’s answer: Convert.ToInt32 (and the rest of the .ToX methods in the Convert class) is a method – CInt (and its relatives) are type-casting directives (similar to the C# syntax (int)x; or (string)y;).

The prime difference is the Convert class’ .ToX methods perform additional logic (as methods are wont to do) before employing a type-casting directive and returning the result. Disassembling Convert.ToByte in the .NET Framework library “mscorlib.dll” with Red Gate .NET Reflector supplies the evidence:

Public Shared Function ToByte(ByVal value As Integer) As Byte

  If ((value < 0) OrElse (value > &HFF)) Then
    Throw New OverflowException(Environment.GetResourceString("Overflow_Byte"))
  End If

  Return CByte(value)

End Function

This is the VB.NET representation of mscorlib’s IL for the method; if you disassemble this method into C#, the last line of the method reads: (byte)value;

The Convert class’ conversion methods employ a type-casting mechanism as they return their result. They also throw exceptions when the conversion could presumably be invalid. The difference here is that if you use direct type-casting (CInt, etc.) you may or may not get an exception when an employing them on an invalid argument.

I say “may or may not”, because the default setting for throwing Overflow/Underflow exceptions is different for VB.NET projects versus C# projects. In VB.NET libraries the default is to throw an exception when an overflow would occur and C# libraries default to the converse – in C# if you try to cast the integer value “10000″ to a byte the result will be 0 and code execution will continue.

The following truth counters the argument/rumor that “VB.NET’s CInt and its siblings are obsolete”: CInt is shorthand for “CType(, Integer)”. CType is a necessary core construct of VB.NET and without it there would be no type-casting to be had for value types (DirectCast has a different intent so using it to cast value-types should be considered an abomination). There is no reason to believe the VB.NET language group would remove the shorthand because it is a carry-over from VB6 or outmoded or whatever other argument that might be made.

Why write “CType(someVariable, Integer)” when you can write “CInt(someVariable)” and save 10 keystrokes (35%). What a deal!.

Dear Winter of 2011-2012

Monday we got the first real snow of the season. It’s been in the upper 40s to lower 60s all season and it’s been real irritating for someone that actually wants snow. I’m ready to move further north where winter means something. Here in St. Louis winter is mostly just cold and wet. Some times there’s snow, but rarely of any significant quantity.

This season was predicted to be one-half mild and the other half severe. I’m not sure we’ll get the “severe” part. Monday was nice start, but today there was rain and warmer temperatures and it was all gone by the time I got home from work.

I love white winters. The landscape becomes so peaceful, quiet, and still when it’s covered with snow. This time, though… brown. Brown, brown, brown.

Someone call up Bing Crosby and ask him to write a parody of his own song. Ask him to title it: “All I’m Getting is a brown Christmas”

I Got Better

Better at finding things to write about, I mean, not the other thing (recovering from being turned into a newt). Well, not really… These next few posts come from thoughts that “just popped in there” to quote Ray Stantz of the Ghostbusters.

That’s another thing I find irritating about myself: I quote and reference and in-joke drop way too much (see above). Nothing worthwhile here; just a bunch or random stuff from the rest of The Intarwebs. Not going to be a rich and famous anybody without something original. Probably won’t be rich and famous for blogging anyway.

A coworker introduced me to an iOS app named "Flipboard". This thing is pretty nice. I stopped really spending time on Facebook, because I’ve turned into a producer of Facebook information rather than a consumer. Flipboard, however, may increase the consumption even over what the iOS Facebook app has.

The iPad version presents several feeds to you in a magazine-ish format (Facebook, Twitter, various other Internet feeds {customizable, of course}) and you can flip through at your leisure and each page is pseudo-randomly arranged with bits of content from the configured sources. I’m beginning to like it. If you’re an iOS device user you should check it out.

Is this the first time a post has gone out on time? I think so!

I’m Still Impatient

I still have a “microwave mentality” regarding growing a blog audience. I still expect one to appear overnight. Perhaps I over estimate friends’ interests in what I have to say. Facebook and Twitter seem to hide a few interested individuals, but I think I have more acquaintances that full on friends; people I know from that one time way back when. Hell, I’ve even become one myself – I hardly talk to my best friend anymore.

I think my desire for people to have an interest in what I have to say is still a part of my life I’ve been trying to leave behind: the part where I become rich and famous in a world where a man, on average, has only 70 years and the really lucky ones have 115. Contemplating that I’ve already expended half my lifespan is sobering. Having children of my own only reinforces the sobering truth that I am not going to live forever.

I “recently” played Neverwinter Nights 2 and a line from an early NPC has stuck with me: “Oh, I’m sure there’s a hundred Cormicks in a city like Neverwinter…” Meaning: Cormick (another NPC) is not unique in the world. Whenever I think I’m the first to have a brilliant idea I remember this line.

However, there is a second part to this line in the story: “… but for this town, he was as big as folks get.” I’d forgotten that part. Just because you’re not the first to ever have an idea doesn’t mean you can’t do it better than the other 99 out there.

Oh look! I’m caught up on retroposting again. Next update should be Friday. Here’s to fame and fortune!

Less Worry

I was thinking the other day about how much I worry about protecting my personal information (Social Security number and such) and how many times I’ve perhaps been a Paranoid Parrot.

For example, in the past I would have turned off location services in things like Google Maps or iPhone photo tagging thinking: “What if the government is tracking me?” or “What if my phone gets lost? Someone’ll find out where I live!” I even used to leave most of the contact information blank in my phone contacts.

I worry about little. Why worry? What good will come of it? You’re only making a hard time for yourself. I am reminded of Jesus’ teaching to His disciples in Luke Chapter 12 – He was speaking in the folly of worry and storing up things for yourself in this world (specifically verse 25): “Who of you by worrying can add a single hour to his life?”

Do not worry so much about the future that you become a Paranoid Parrot and do not enjoy the good things life has to offer. Do not dwell on material things, but enjoy your time with others. The relationships you establish here are worth far more than gold in Heaven.

Idle Words

I was considering the other day the large quantity of simply ridiculous things that come out of my mouth. I wisecrack too often – the kinds of things that, in retrospect, make you think “Man… I really sounded/acted like a fool” and “I wish I’d kept my stupid cake-hole shut”.

From an indeterminate point in the recent past through to the future I’m going to try to do more thinking before I open my mouth and say another foolish thing.

Since this is, again, another retropost I can say I’m doing better at not filling the air with foolish things.

I was reminded of a verse from the Bible regarding such things: ~”Matthew 12:36 – But I say unto you, That every idle word that men shall speak, they shall give account thereof in the day of judgment.” The context regards speaking wicked things, though if taken as a simple rule, we would be better off to keep control of our tongues and not speak without forethought.

Better to remain silent and be thought a fool, than to speak and remove all doubt.

Humpback Whale Day

Hi, this site is all about humpback whales, REAL HUMPBACK WHALES. This site is awesome. My name is Rando and I can’t stop thinking about humpback whales. These guys are cool; and by cool, I mean totally sweet.

Facts:

  1. Humpback whales are mammals.
  2. Humpback whales fight ALL the time.
  3. The purpose of the humpback whale is to flip out and kill people.

So sayeth Robert Hamburger. Well… he almost said that. He was actually talking about ninjas, but those are pretty close to humpback whales anyway (see above facts) so it still counts.

Real Ultimate Power still exists, but I don’t know how easily you can find clone sites. Those were probably the funnier of the lot: replace “ninja” in the above intro with things like “doctor”, “hobo”, “clown”, etc.

In keeping with what has become a de facto tradition: retropost!