473,322 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Variable Property/field scope confusion

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.

Nov 24 '06 #1
9 1352
Here is the code, i forgot to post the link!

http://rafb.net/paste/results/X1sP7593.html

Nov 24 '06 #2
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.

--
Cheers,
Gary
http://www.garyshort.org/

ga********@myway.com wrote:
Here is the code, i forgot to post the link!

http://rafb.net/paste/results/X1sP7593.html
Nov 24 '06 #3
ga********@myway.com wrote:
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

Nov 24 '06 #4
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

Nov 24 '06 #5
garyuse...@myway.com wrote:
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

Nov 24 '06 #6
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

Nov 24 '06 #7
ga********@myway.com wrote:
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

Nov 24 '06 #8
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-

Nov 24 '06 #9
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.

Nov 24 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Steve Dondley | last post by:
Take the following variable: $prefix = 'start'; And assume there is an object that has a property called 'start_time'. How can I access that property in the object using the $prefix variable?...
10
by: Cybertof | last post by:
Hello, Do you know why i get the error "Left hand side must be variable, property or indexer" with the following code : struct FundDataStruct { internal int quantity;
12
by: Bill | last post by:
When I compile the following code fragment, the compiler errors saying the variable connection is not initialized. As I understand C#, I thought it would be initialized. Please help me and explain...
4
by: Sandeep Singh | last post by:
I m building an application in which I redirect the user to a particular page. In the Page Load event of the that page , I assign a variable some value. Then I call the Button click event on that...
3
by: pelleas | last post by:
Hello to all! 1a. I'm a newbie having trouble accessing a pointer from one .cpp file which is defined within a member function found in a separate .cpp file. I'm looking for a line or two of...
4
by: garyusenet | last post by:
Hello everyone. I have a project at the moment which is working fine. I originally coded my code in form1.cs, as this was the default code file generated by Visual Studio. However now i'm trying...
7
by: rpeacock | last post by:
I was curious to know if it is possible to run an Update query by passing the field name as a variable for updating. The simple example should make more sense. Basically I am trying to change the...
7
by: Johannes Bauer | last post by:
Hello Group, please consider the following code #include <vector> #include <iostream> #define USE_CONST #define USE_STRING
27
by: Erwin Moller | last post by:
Hi group, Consider this simple script (tested on FF3): <script type="text/javascript"> test = 'outer'; for (var i=0;i<2;i++){ alert(test); var test = 'inner'; alert (test);
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.