Why are there so many different ways?

  • Thread starter Thread starter Charles Law
  • Start date Start date
C

Charles Law

As far as I can tell the following are equivalent

Dim i As Integer

i = Convert.ToInt32(1)
i = CInt(1)
i = CType(i, Integer)

Is there any particular reason why we should have all these ways to do the
same thing? If they are different, what is the difference? Otherwise,
wouldn't it be simpler to have just one way?

Charles
 
CInt is a holdover from VB and doesn't exist elsewhere in the .NET Framework

System.Convert only handles conversions to and from primitive types

CType can cast to any class, be it defined in the BCL or custom

----- Charles Law wrote: ----

As far as I can tell the following are equivalen

Dim i As Intege

i = Convert.ToInt32(1
i = CInt(1
i = CType(i, Integer

Is there any particular reason why we should have all these ways to do th
same thing? If they are different, what is the difference? Otherwise
wouldn't it be simpler to have just one way

Charle
 
You forgot one, DirectCast. DirectCast is more efficient than all the
methods you mentioned, but only works in situations where boxing comes into
play.

I have never heard of a performance difference between those methods that
you mentioned. I haven't checked the IL, but I assume the use the same
thing under the hood.

--
Eric Marvets
Principal Consultant

the bang project

<shameless self promotion>

Email (e-mail address removed) for Information on Our Architecture and
Mentoring Services

</shameless self promotion>
 
Charles Law said:
i = Convert.ToInt32(1)

In this case this is the best one. Its OO, and specific to task.
i = CInt(1)

IIRC this one is for backwards compat in VB.
i = CType(i, Integer)

More generic. Suitable for other tasks but to general for this specific one.


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Develop ASP.NET applications easier and in less time:
http://www.atozed.com/IntraWeb/
 
Hi Charles,

That is why I like the current version from VB.net more than any other
language. It acts as a natural language.

And you ask this as an Englishman where people use a languages which have so
many different words to describe simple things.

Because the language derives from the languages from the Originals, the
Kelts, the Romans, the Angels(danish), the North Sea language (Fries/Dutch),
the French and maybe even more.

There is no reason why it is done, and still it is done.

Just my thought,

Cor
 
Hi Cor

Thanks everyone for answering. While I've got your attention, I have posted
a couple of other questions recently about SQL Server and InvokeRequired, if
anyone would like to pop over and have a look ;-)

Cheers

Charles
 
Hi Charles,

The first time I saw SQL (and that is long ago) I started direct hating it
because it is so based on the English language and so very irregulair while
it demands language sequences.

Also the things as Invoke are things I do not like anymore, I do not want to
go anymore deep in the computer for things, which have tomorrow a simple
solution (a little bit lazy about that).

I seldom see regulars who are great (where I doubt in Marina) with SQL in
this dotnet newsgroups (I hope they become not angry, however than they give
you the solution for your Pivot question and your goal is reached). I will
place a sentence for Marina with that Pivot question of you, normally when
see sees it, she will help.

Invoke are the things Armin is intrested in and somewhat for Jay moreover
because office is involved in that question, I think it is a good idea to
ask this Invoke in the language.vb group.

When there is no answer, I will help you to put Armins and Jays attention on
it, althoug when they knows answers on those questions they mostly answers
them.

And when there are then no answers, maybe I start looking for you for an
answer.

:-)

Cor
 
Back
Top