using and scope

  • Thread starter Anders Eriksson
  • Start date
A

Anders Eriksson

I have this code, which I have copied from the documentation of the
Lumenworks CsvReader.

using (CachedCsvReader csv = new CachedCsvReader(
new StreamReader(openFileDialog1.FileName), true))
{
// Field headers will automatically be used as column names
dataGridView1.DataSource = csv;
}

Now as I understand by using 'using' the variables defined in it will be
destroyed after exiting the using clause. This would mean that the csv
variable will not exist outside of this.

How come that the values are still visible in the dataGridView1?

Do the datagridview copy all the values and csv is not needed anymore?

Should I use this code or is there a better way?

// Anders
 
J

Jeff Johnson

They are not variables in this case. They are objects. A variable is a
primitive type string, int, double etc although those are objects too
since everything in .Net derives for System.Object.

That is completely wrong. "Variables" are simply code constructs that allow
you to associate an identifier with data. The data can be a value type (int,
byte, bool, etc.) or a reference type (any object). There is no "in this
case" at all.

The real answer is that the variable csv is simply a reference to data in
memory, and that the DataSource property is told to reference that same blob
of memory, so that when the csv variable goes out of scope at the end of the
using block, the actual data is still retained because it is being
referenced by something else (the property).
 
J

Jeff Johnson

Jeff Johnson said:
That is completely wrong. "Variables" are simply code constructs that
allow you to associate an identifier with data. The data can be a value
type (int, byte, bool, etc.) or a reference type (any object). There is no
"in this case" at all.

The real answer is that the variable csv is simply a reference to data in
memory, and that the DataSource property is told to reference that same
blob of memory, so that when the csv variable goes out of scope at the end
of the using block, the actual data is still retained because it is being
referenced by something else (the property).

Oh, as Pete mentioned, it's probably useless to try to deal with the object
that is declared in the using statement, because it will be disposed when it
goes out of scope, so even though you assign a reference to a variable that
was outside of the scope of the using block, you won't have anything useful
in there.
 
J

Jeff Johnson

You kiss my ass. That's all I have to say about it. OMG! You motherfuckers
unbelievable.

I honestly knew that this was exactly what was going to happen the moment I
pointed out your error. It wasn't for your benefit, actually; it was for the
people who might read your post and actually believe what you said. There's
no way I thought that YOU would actually understand you made a mistake.
 
J

James A. Fortune

I honestly knew that this was exactly what was going to happen the momentI
pointed out your error. It wasn't for your benefit, actually; it was for the
people who might read your post and actually believe what you said. There's
no way I thought that YOU would actually understand you made a mistake.

He understood. He might've even made the mistakes on purpose. But
don't argue with bots if you don't want him laughing at you. The
bathos is usually a tip-off, even though he disguised it better by
taking two posts to reach bottom.

James A. Fortune
(e-mail address removed)
 
A

Arne Vajhøj

<snipped>
<yawn>

LOL! I don't take kindly to .NET.Nannies or any form of a nanny crawling
out the woodwork.

But nannies are necessary when we seal with C#
programmers<del><del><del><del><del><del><del><del><del><del>typers
that has not understood what a variable is.

Arne
 
A

Arne Vajhøj

You kiss my ass. That's all I have to say about it. OMG! You
motherfuckers unbelievable.

Maybe the other kids were impressed when you used that language
in 5th grade.

But ...

Arne
 
A

Arne Vajhøj

Let me tell you something else you on the scene late SOB. You, the
maniac Peter, Jeff whatever, and ass kiss Fortune are simply ass-hole
nannies. You are a dime a dozen on Usenet.

I myself I don't give a shit what people like think. The only thing you
people can do is kiss my ass, spit shine my shoes with your tongues and
****-off.

Let me repeat:

#Maybe the other kids were impressed when you used that language
#in 5th grade.
#
#But ...

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top