473,395 Members | 1,554 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,395 software developers and data experts.

Method Scope

I am trying to use a string variable (strX) inside a method. The method looks
like the following:

Class Something
{
public string strX;

public static DataSet ReturnDataSet(string strSql)
{
//This method returns a Dataset
strX
}
}
For some reason unknown to me, strX loses scope inside this method. Can
someone please explain why and is there a way around this? Thanks
Sep 6 '08 #1
2 1530
"Joseph" <Jo****@discussions.microsoft.comwrote in message
news:52**********************************@microsof t.com...
>I am trying to use a string variable (strX) inside a method. The method
looks
like the following:

Class Something
{
public string strX;

public static DataSet ReturnDataSet(string strSql)
{
//This method returns a Dataset
strX
}
}
For some reason unknown to me, strX loses scope inside this method. Can
someone please explain why and is there a way around this? Thanks
The reason is that the method is static while the variable is not.
Depending on what you are trying to achive, the solution is to either
mark the variable as static (public static string strX), or make the method
an instance method (remove the "static" keyword).

Sep 6 '08 #2
Joseph <Jo****@discussions.microsoft.comwrote:
I am trying to use a string variable (strX) inside a method. The method looks
like the following:

Class Something
{
public string strX;

public static DataSet ReturnDataSet(string strSql)
{
//This method returns a Dataset
strX
}
}
For some reason unknown to me, strX loses scope inside this method. Can
someone please explain why and is there a way around this? Thanks
The method is static - in other words, it's not related to any
particular instance of Something.

The variable is an instance variable, which means each Something can
have a different value. That means it doesn't make sense to refer to it
from a static method, without a particular instance.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 6 '08 #3

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

Similar topics

6
by: Hal Vaughan | last post by:
Being self taught, this is one thing I've always had trouble with -- I finally get it straight in one situation and I find I'm not sure about another. I have a class that keeps calling an...
12
by: George Jempty | last post by:
OK, wanted to make a more positive contribution to start than just plonking somebody. Here's something I've discovered using "priveleged methods" a la Douglas Crockford...
2
by: daniel.w.gelder | last post by:
Hey again. I'm still working on my functor class, and now it works with method functions too; you pass it the method function, as well as the object you want to be 'this' when the method is...
6
by: Martin | last post by:
I'd like to be able to get the name of an object instance from within a call to a method of that same object. Is this at all possible? The example below works by passing in the name of the object...
2
by: Jon Davis | last post by:
The garbage handler in the .NET framework is handy. When objects fall out of scope, they are automatically destroyed, and the programmer doesn't have to worry about deallocating the memory space...
22
by: WXS | last post by:
Sometimes a method in a class requires the use of class instance variables/fields that will not be used outside of the method itself. Currently this means you must create a instance field in the...
26
by: Patient Guy | last post by:
The code below shows the familiar way of restricting a function to be a method of a constructed object: function aConstructor(arg) { if (typeof(arg) == "undefined") return (null);...
10
by: Mihai Osian | last post by:
Hi everyone, Given the code below, can anyone tell me: a) Is this normal behaviour ? b) If it is, what is the reason behind it ? I would expect the A::method(int) to be inherited by B. ...
19
by: zzw8206262001 | last post by:
Hi,I find a way to make javescript more like c++ or pyhon There is the sample code: function Father(self) //every contructor may have "self" argument { self=self?self:this; ...
9
by: VK | last post by:
<OT>I am finishing TransModal 0.1 so planning to move it from alpha to beta stage.<OT> Besides that I am planning to write an introductory to inheritance schema currently used in Javascript...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.