CSharp/C# >> Method Scope

by Sm9zZXBo » Sun, 07 Sep 2008 02:01:01 GMT

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



CSharp/C# >> Method Scope

by Alberto Poblacion » Sun, 07 Sep 2008 02:06:44 GMT



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).

CSharp/C# >> Method Scope

by Jon Skeet [C# MVP] » Sun, 07 Sep 2008 02:29:50 GMT


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 - < XXXX@XXXXX.COM >
Web site: http://www.pobox.com/ ~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com

Similar Threads

1. Anonymous Methods scope question - CSharp/C#

2. Critical sections and method scope variables

Hi, look at this:

VALUE A::Method(int aa, int ab)
{
    float lv;
    VALUE ret;

    EnterCriticalSection(handle);
    (... access shared resource and local variables ... )
    LeaveCriticalSection(handle);

    return ret;
}

There are more than one thread executing this method. So I ask:
- Will I have problems with "aa", "ab", "lv" or "ret"?
- The variables listed before will be allocated at some kind of thread stack
?
- There are any function that leaves the critical section only after
returning "ret" ?


Thanks...
Rafael Pivato


3. Method scope limit ?????

4. ADSI returning groups in Global scope and Domain local scope instead of Universal scope

Hi everyone,

I'm having a problem with reading user groups on Active Directory using C#.
It returns all the groups in the Universal scope for a specific user.
However, I only need the groups in Global scope and Domain local scope. Does
anyone know I can modify the following code to this?

DirectoryEntry entry = new DirectoryEntry("LDAP://" + Domain, CurrentUser,
pwd, AuthenticationTypes.Secure);
DirectorySearcher mySearcher = new DirectorySearcher(entry);


// Change this search for anything
mySearcher.Filter = ("(sAMAccountName="+CurrentUser+")");
try
{
System.DirectoryServices.SearchResult resEnt = mySearcher.FindOne();

// Display all groups for this user
object obGroups = de.Invoke("Groups");
foreach (object ob in (IEnumerable)obGroups)
{
DirectoryEntry obGpEntry = new DirectoryEntry(ob);
group = obGpEntry.Name.Replace("CN=", "");
Response.Write("Group: " + group + "<br>");
}
...


Thank you
Maz A.


5. variable scope inside a method - CSharp/C#

6. Scope of internal method in C#?

I've seen lots of mentions of the scope of an "internal" class.

What's the scope of an "internal" method.  Why would one use it as
opposed to an "internal" class?

Thanks in advance.

Adam

7. ANN: Product Scope 7 and Profile Exchanges enhanced with SetupCast publishing methods

8. Scope bloat when factoring code out of big methods