Variable Property/field scope confusion

G

garyusenet

Hello I'm trying to get to grips with variable scope.
I have a very basic programme i'm working on - and am once again being
confused by scope of variables.

I am just trying to write a new method which will read a file into a
text string. However I can't access my path/file variable as it is part
of another method.

The code for my program is here:

You can see the method i've started trying to write towards the end,
it's called 'ReadFile()' i attempted to write new StreamReader(path) on
the right hand side of the statement but as you can see that isn't
working. As i cant access the path variable i determined at
btnOpenFile_Click earlier in the programme, from the ReadFile() part of
my programme.

Can someone suggest the best way of me modifiying this code, so that I
can access the path variable. And also please explain the thinking
behind the modification.

I'm indebted for your help,

Gary.
 
G

gary

Hello Gary,

The problem here is that you get the path and store it in a variable
that only has scope for the life of the method, once the method
completes the variable goes out of scope. You have to store it
somewhere where you can access it from other methods. The simplest way
to do this is to create a property on the form to hold the path then it
will be available to you in other methods.
 
J

Jon Skeet [C# MVP]

Hello I'm trying to get to grips with variable scope.
I have a very basic programme i'm working on - and am once again being
confused by scope of variables.

I am just trying to write a new method which will read a file into a
text string. However I can't access my path/file variable as it is part
of another method.

So instead of a local variable, you need a member variable, also known
as a field.

I would *strongly* suggest picking up a C# tutorial or book at this
point - it's very hard to learn the basics of a language from a
newsgroup. (Newsgroups are great for answering specific questions, but
not for initial learning.)

Jon
 
G

garyusenet

Thankyou I will try to read more. I have already read the visual guide
that came as a registration bonus, and also have the Printed ECMA tech
spec for c# which i'm working through but I do find that some areas are
vague.

If i include this in my class. Would this solve my problem, and is this
what you both have suggested when you speak of properties?

// private member variables
private string path;

// create a property
public string path
{
get { return path; }
set { path = value; }
}


Thankyou
 
J

Jon Skeet [C# MVP]

Thankyou I will try to read more. I have already read the visual guide
that came as a registration bonus, and also have the Printed ECMA tech
spec for c# which i'm working through but I do find that some areas are
vague.

If i include this in my class. Would this solve my problem, and is this
what you both have suggested when you speak of properties?

// private member variables
private string path;

// create a property
public string path
{
get { return path; }
set { path = value; }
}

Well, that shouldn't compile as the property and the variable have the
same name. I don't think you really need the property, do you? However,
the basic premise is correct.

Jon
 
G

garyusenet

Could you tell me the difference between a property and a field please?

Could you tell me how I create a field for storing my path variable in?

Thankyou
 
J

Jon Skeet [C# MVP]

Could you tell me the difference between a property and a field please?

Again, this is the kind of thing that is much better explained in a
book or a tutorial than in a newsgroup.
Could you tell me how I create a field for storing my path variable in?

You've done so already - field is another name for instance variable.

It's just you don't *also* need the property.

Jon
 
G

garyusenet

But as it stands i cant access the variable, this is why i thought if i
made it into a property I could access it.

Can you reccomend any very good online resources for learning c#
please?
I can not use MSDN as its language is too technical.

Sorry to take your time,

Thankyou,

Gary-
 
G

garyusenet

Ah i think i understand, all i needed to do was to take the variable
setup out of the method, and put it after the starting { of my class.
 

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