using arraylist

  • Thread starter Thread starter wanwan
  • Start date Start date
W

wanwan

I wrote a class and have objects from this class added to an
arraylist. When I retrieve the objects from the arraylist, should I
use ctype or directcast to cast them back to the original class.?
 
Assuming that both CType and DirectCast work for your class, then the best
thing to do is measure the relative performance of both and use the one that
gives the best results.
 
Eric

DirectCast means cast direct without testing if that is possibel
Ctyps means convert if needed and cast as not needed.

So there will probably only an not measurable quicker behaviour for the
Direct Cast

Cor
 
wanwan said:
I wrote a class and have objects from this class added to an
arraylist. When I retrieve the objects from the arraylist, should I
use ctype or directcast to cast them back to the original class.?

As you want to cast the reference, not covert the object, you should use
DirectCast.

If you use framework 2.0, you can use a generic list instead of the
ArrayList, then you don't need the cast.
 
Hello
If you use framework 2.0, you can use a generic list instead of the
ArrayList, then you don't need the cast.

Before you misinterpreted this as "faster" by documentation yes however
in practice casting is faster

regards

michel
 
Could you write that in a sentence that makes sense?

Did I misinterpret anything? Before or after what? What documentation?
What is it that casting is faster than?
Hello
If you use framework 2.0, you can use a generic list instead of the
ArrayList, then you don't need the cast.

Before you misinterpreted this as "faster" by documentation yes however
in practice casting is faster

regards

michel
 
Could you write that in a sentence that makes sense?

Did I misinterpret anything? Before or after what? What documentation?
What is it that casting is faster than?


I can't find an example of syntax for writing an arraylist as a
generic list. Can I please have an example?
 
wanwan,

Instead of this:

Dim myList As New ArrayList

do this:

Dim myList As New List (Of myClass)

Kerry Moorman
 
Personally I'd use DirectCast but Stephany has given the best advice.
Rather than wonder stuff and rather than reading unsubstantiated opinions
from strangers who speculate on everything... do us a favor and run a short
test. Construct loops using CType and DirectCast long enough to produce
meaningful results and post a short version of the code and the times you
generate here.

First everybody gets a little smarter for the exercise, second we have facts
rather than opinions and third each of us can test the code on our own
machines if we feel like confirming the results. Basing our opinions on
facts isn't a bad thing. :-)
 
Hello


Before you misinterpreted this as "faster" by documentation yes however
in practice casting is faster

regards

michel

Michel,

I'm not sure I understand you? Are you saying that ArrayList with a cast is
fater (from a performance perspective) then List(Of T) with out a cast?
 
Tom,

I did know you as a nice guy, being busy with VB.Net. It is striking now
that you act as a C# adict.

I have seen here in these newsgroups that a lot of people don't know that
there is a C# equivalent for DirectCast. And therefore tell that CType is
better, however because that they think that the DirectCast does not exist
in C#. However although if it was not in C#, therefore it is not better to
use the not special build methods for operations.

There is in C# the C(onvert)Type equivalent is (TheType) MyType
and the Directcast which is witten as "MyType as TheType"

It is simple when to use the directCast instead of the CType. If a class is
derived from the to cast one or if the same interface is used than it is
DirectCast, it is standard faster and even there is not any optimalizing
needed. CType can be used as is told to the class that it is convertible. If
you don't know it use CType because if that does not work than DirectCast
does for sure not and that is the only advantage from CType. A weak
argument, because than you would not use an arraylist either because you
know that the array exist..

You probably start telling now that nobody knows what the interfaces or the
base classes are. If a VB.Net programmer or a C# programmer does not know
which classes implement by instance the IList or does not know that TextBox
inherits from Control than they should fast go learn that (here was another
text).

Cor
 
Seriously Cor it's hard for me to care what you think and I've pretty much
stopped reading your replies.

In the last few weeks you've mentioned some Nazi collaborator on a thread
about contracts, you mentioned Hitler the other day, you've just posted a
reply to Herfried's posting about Try... Catch explaining nothing at all
while not answering his simple question, "why have you written he is wrong?"
You replied to me how a module was different from a static class and a
handful of messages later replied to somebody else that a module was "just
like" a static class.

Try and separate what you "think" (which I believe few can figure out) from
what you know and here is a hint; when you start a sentence with "you
probably..." just stop typing as you really have no idea what anybody
thinks. You never suggest a person thinks something smart, you always
suggest a person thinks something dumb.

Stephany and I suggested "wanwan" test if there was any significant time
penalty, Branco's reply points to an article containing relevant information
(including the mention of ArrayList), your reply is a complaint along with
some idle speculation.

Look up the word "addict" while you're at it, it doesn't apply here in any
way.
 
Cor said:
Tom,

I did know you as a nice guy, being busy with VB.Net. It is striking now
that you act as a C# adict.

I have seen here in these newsgroups that a lot of people don't know that
there is a C# equivalent for DirectCast. And therefore tell that CType is
better, however because that they think that the DirectCast does not exist
in C#. However although if it was not in C#, therefore it is not better to
use the not special build methods for operations.

There is in C# the C(onvert)Type equivalent is (TheType) MyType
and the Directcast which is witten as "MyType as TheType"

Either you don't know what DirectCast does, or you don't know what the
as operator does.
 
Tom,

Only one question, will you please write the truth what I wrote and not try
to bring me in a demagogic way in discredit.

That is easy for you in English, how much I will study English I will lose
that (as long as I had not made my profession from that).

Be however aware that all people who speak more than one language forever
smile when they see people who speak only English do that. It gives a view
of a poor mind seeing people doing that in a discussion.

However using this in a thread were Stephany is in involved gives it even
another view. Sorry I cannot go on, would bring me on a level I don't want
to be.

(By the way, I have misinterpretted your first message, I see now that it
was only to help Stephany, as forever in those cases, sorry for that).

Cor
 
Cor Ligthert said:
Tom,

Only one question, will you please write the truth what I wrote and not
try to bring me in a demagogic way in discredit.

Okay see below.
That is easy for you in English, how much I will study English I will lose
that (as long as I had not made my profession from that).

The word "Hitler" is the same in Dutch and in English. The refusal to reply
with an example to Herfried is the same in Dutch as in English.
Be however aware that all people who speak more than one language forever
smile when they see people who speak only English do that. It gives a view
of a poor mind seeing people doing that in a discussion.

You can forever smile as you please but again you have no idea what
languages I speak. Perhaps you ought to consider the view of a poor mind
seeing you doing that in a discussion.
However using this in a thread were Stephany is in involved gives it even
another view. Sorry I cannot go on, would bring me on a level I don't want
to be.

Well try this (which you won't). Rather than being the "commentator" try to
actually post some code demonstrating your point. So when Herfried, or I or
anybody suggests something is a "good" idea you could post a technical
example of why it isn't. Hey do this... find some code you've posted and
post it again here or is all advice all the time?

It's a simple matter to verify (by searching Google) that you post your
agreement and disagreement on almost every matter.

Hey look what you wrote in Jan 2007:
If you use the standard ADO.Net methods you can only use it in an OOP way.
That can by clumsy by using it still with every small objects (item) or
effective using a lot of binding. The latter is however not influencing
your
OOP way. The OOP way is that you create as much as possible instancible
and
destructable objects. (The latter done by the managed code). And try to
avoid things as Modules or as they are called in VB Shared Classes (In C#
static classes and singletons).



From Oct 2005:

Cor Ligthert said:
If Hitler had won the war, than we would not have had *this* kind of
problems to discuss.
Just my thought,

From Feb 2007:

Hitler had a National Socialistic Party.

The word Socialistic had (almost) nothing to do with his party (slightly it
has and you can even debate about it).

This is equivalent for me with the static/shared class, which is no class,
until you have a complete nation behind you which is indoctrinated.

Maybe a simpler one for those not so well informed about politics in Europe.
Because somebody calls a dog a horse, does not make that a dog is a horse
although a dog has four legs.


from Dec 2006:

Herfried,

The technologies is not the most important.

Let me take this time not a analogy with cars.

One of the first inventions from human was fire and how to make meat
consumable for humans, it seems that it was the start of their spread over
the world.

We still eat meat and still use in a way fire for that. However we do it
much more hygienic, a reason why we life much longer now.

Cor

Followed immediately with this reply the same day:

Robin,

Be wise do not let you bring to use this newsgroup for a politic discussion.

Just ignore it,

Cor

From Feb 2007:

Raibeart,
Since I am a Christian,

With writting those word at the beginning of your message you are bringing
for me every true Christian in discredite, who has given you the right to
write that, for sure not Jezus Christus, because he died to fight against
what you wrote.

Your words are very insulting and therefore probably 100% responsible for
the killing of not responsible persons from whatever nation or religion
(with the first goal of course Americans). Please keep your terrorists
feeding words out of this newsgroup.

Before you think that I am afraid by terrorists. Yes I am, but I don't see
them as a part of a religion or whatever, they are for me from the same
order as you.

Cor
 
As you are unable to read the documentation yourself, I will explain it
to you.

The DirectCast will attempt to make a cast, and throw an exception if it
fails.

The as operator will attempt to make a cast, and returns null it it fails.

Do you now see the difference, or is there still something that is
unclear to you?

Goran,

As it is something else than this, .

http://msdn2.microsoft.com/en-us/library/cscsdfbt.aspx

http://msdn2.microsoft.com/en-us/library/7k6y2h6x.aspx

So let your light shine on it and explain it to us.

Cor
 
Goran,

No I don't see the difference, you are telling in my opinion the same as the
difference between a French bread and an American bread. When I have
consumed it, it is completely the same.

Cor

Göran Andersson said:
As you are unable to read the documentation yourself, I will explain it to
you.

The DirectCast will attempt to make a cast, and throw an exception if it
fails.

The as operator will attempt to make a cast, and returns null it it fails.

Do you now see the difference, or is there still something that is unclear
to you?
 
Tom,

I should stop,
The word "Hitler" is the same in Dutch and in English. The refusal to reply
with an example to Herfried is the same in Dutch as in English.

The name Hitler was not the subject where I was talking about. It was the
organisatison that Hitler was leading and the excelent way they were able to
do their propaganda communication, in a way that German speaking people were
believing at the end what they were telling. The way you tell that I used
it, takes it out of its context and is therefore just demagogie.

Well try this (which you won't). Rather than being the "commentator" try
to actually post some code demonstrating your point. So when Herfried, or
I or anybody suggests something is a "good" idea you could post a
technical example of why it isn't. Hey do this... find some code you've
posted and post it again here or is all advice all the time?
Maybe I misread sometimes something, however when I go in a discussion I
take the politeness to check what I write. This was already in the thread.

http://groups.google.com/group/microsoft.public.dotnet.languages.vb/msg/fa2cadd0f761d248?hl=en&

As I said, I don't go to the style you are using now in the discussion.

Cor
 
Back
Top