ken4ward
ken4ward

Reputation: 2296

Calling a static variable

Very simple and straight forward. I created 2 classes: The first class. In this i declared a static variable so as to be able to access it in the other class without initialization.

protected class TCHome
{
   protected static String write = "blablabla";
}

the second class:

public class Home
{
   TCHome.write - //write does not come up to be accessible at all.;
}

What I'd expected is that I should be able to make a direct call to the variable - "write" but it does not come up at all. What's the right way? Please.

Upvotes: 0

Views: 134

Answers (7)

Hamid Pourjam
Hamid Pourjam

Reputation: 20764

protected means the type or member can be accessed only by code in the same class or struct, or in a class that is derived from that class.

You should make it public or internal.

  • public: The type or member can be accessed by any other code in the same assembly or another assembly that references it.
  • private: The type or member can be accessed only by code in the same class or struct.
  • protected: The type or member can be accessed only by code in the same class or struct, or in a class that is derived from that class.
  • internal: The type or member can be accessed by any code in the same assembly, but not from another assembly.
  • protected internal: The type or member can be accessed by any code in the assembly in which it is declared, or from within a derived class in another assembly. Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.

reference

Also consider using constants if it suits your needs.

protected class TCHome
{
   public const string Write = "blablabla";
}

or make it a property, having public accessible fields is not a good approach.

protected class TCHome
{
   public static string Write { get; set; } //maybe private set;

   static { Write = "blablabla"; } //this is called static constructor
 }

or a readonly string

protected class TCHome
{
   public static readonly string Write = "blablabla";
}

Upvotes: 3

Gavin Zhao
Gavin Zhao

Reputation: 41

The access modifier should be public, like below

public class TCHome
{
   public static String write = "blablabla";
}     

Upvotes: 3

Alex Gidan
Alex Gidan

Reputation: 2679

Protected modifier is orthogonal to static. In other words, these are independent concepts, so there is no reason why you could access a protected method or variable from a class that is not derived.

To access write member from Home it should be public:

protected class TCHome
{
       public static String write = "blablabla";
}


public class Home
{
   TCHome.write - //write does not come up to be accessible at all.;
}

Here an overview of the access modifiers in C#:

Access modifiers are specified as part of the method declaration syntax and can be:

internal
private
protected
protected internal
public

If no modifier is specified, the method is given private access.

virtual methods can be overriden by a derived class using the override keyword.

abstract methods must be overriden in a derived class. If any method of a class is abstract, the entire class must be declared as abstract.

sealed methods are methods that override an inherited virtual method having the same signature. When a method is sealed, it cannot be overriden in a derived class. Method Access Modifiers

public indicates the method is freely accessible inside and outside of the class in which it is defined.

internal means the method is only accessible to types defined in the same assembly.

protected means the method is accessible in the type in which it is defined, and in derived types of that type. This is used to give derived classes access to the methods in their base class.

protected internal means the method is accessible to types defined in the same assembly or to types in a derived assembly.

private methods are only accessible in the class in which they are defined.

Source: http://blog.crsw.com/c-class-and-method-modifiers-overiew/

Upvotes: 2

Piyush
Piyush

Reputation: 826

You should be using public or internal access specifier as per your requirement

protected class TCHome
{
   public static String write = "blablabla";
}

OR

protected class TCHome
{
   internal static String write = "blablabla";
}

Upvotes: 2

MakePeaceGreatAgain
MakePeaceGreatAgain

Reputation: 37095

You have two options:

either make your variable public (which is not recommended as public fields are bad, better make property or readonly field) or let your class Home derive from TCHome.

public class Home : TCHome {
    Home() {TCHome.write = /* ... */ }

}

Upvotes: 2

Waqar Majid
Waqar Majid

Reputation: 111

Inherit tchome in home class. or change tchome from protected to public.

Upvotes: 2

clement
clement

Reputation: 4266

You have put protected to the write, there must be subclass.

use public:

public static String write = "blablabla";

Upvotes: 2

Related Questions