Access Modifiers

  • Thread starter Thread starter Roy Gourgi
  • Start date Start date
R

Roy Gourgi

Hi,

Is it possible to declare a variable or array in the class that is
accessible to all the methods in the class? For the example below, I would
like to make the variable lnVar1 accessible to both methods m1 and m2 but I
am getting an error. Is there another way to do this?

Thanks
Roy


using System;

using System.Collections.Generic;

using System.Text;

namespace cClasses

{

class Program

{

static void Main(string[] args)

{

AA2.m1();

}

}

}







public class AA2

{

protected int lnVar1 = 0;

public static void m1()

{

Console.Write("M1 {0}", lnVar1);

}

public static void m2()

{

Console.Write("M2 {0}", lnVar1);

}

}
 
Roy said:
Hi,

Is it possible to declare a variable or array in the class that is
accessible to all the methods in the class? For the example below, I would
like to make the variable lnVar1 accessible to both methods m1 and m2 but I
am getting an error. Is there another way to do this?

Problem with your code is that the functions are "static" but the
variable is not.

Depending on what better fits the design.. either make the variable
static too, or remove the static keyword from the functions and create
an instance of the AA2 class before calling m1.

hth,
Max
 
Check your different scope parameters. You have made your vaiable
'protected' in the scope of one class.

Also look at the different accessor types (get, set, etc.). That may be of
assistance also.

J
 
Static members can only operate on other static members, not nonstatic/instance
members. Change your class member lnVar1 from protected to a static and
it works just fine.
 
Hi,

Is it possible to declare a variable or array in the class that is
accessible to all the methods in the class? Yes.

For the example below, I would
like to make the variable lnVar1 accessible to both methods m1 and m2 but I
am getting an error. Is there another way to do this?
Both m1 and m2 are static so they can only access static data. lnVar1
is not declared static and hence cannot be accessed by your static
methods. Either make lnVar1 static or remove the static modifiers
from m1 and m2.

Not relevant to your question, but it is better to make class data
like lnVar1 private and to provide a public or protected property to
get at it. With protected data any derived class can do all sorts of
nasties to the data.

rossum
Thanks
Roy


using System;
using System.Collections.Generic;
using System.Text;

namespace cClasses
{
class Program
{
static void Main(string[] args)
{
AA2.m1();
}
}
}


public class AA2
{
protected int lnVar1 = 0;
public static void m1()
{
Console.Write("M1 {0}", lnVar1);
}

public static void m2()
{
Console.Write("M2 {0}", lnVar1);
}
}



The ultimate truth is that there is no ultimate truth
 

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

Similar Threads


Back
Top