I have a base class with a protected-level static variable, a protected-level static function, and a public function:
public class ClassA
{
protected static int Size = 4;
public static byte[] DoSomething(byte[] params)
{
// use Size somehow
params = DoSomethingElse(params);
return params;
}
protected static byte[] DoSomethingElse(byte[] params)
{
// do whatever
return params;
}
}
And a derived class that hides the protected-level variable and function:
public class ClassB : ClassA
{
public new static int Size = 2;
protected new static byte[] DoSomethingElse(byte[] params)
{
// do something different than base class
return params;
}
}
Now, when I call ClassB.DoSomething, I want the Size value and DoSomethingElse from the ClassBto be used, but the ClassA values are used instead. Is there a way to make this use the ClassB variable and method?
Related
I have to override a function in a base class in such a way that calls to inherited functions also lead to calls to this overridden function instead of the base implementation.
class base_class
{
string abc;
public int get_1()
{
return 1;
}
public int get_number()
{
return get_1()+1;
}
}
class der_class : base_class
{
public int get_1()
{
return 2;
}
}
class Program
{
static void Main(string[] args)
{
der_class abc = new der_class();
Console.WriteLine(abc.get_number());
Console.ReadKey();
}
}
This prints 2. How can I get the output to be 3 by making the get_number to call overridden get_1?
You need the override keyword to actually override a method, otherwise you are hiding it.
class der_class : base_class
{
// note the word override here!
public override int get_1()
{
return 2;
}
}
Also, you need to make the method virtual in the base class:
class base_class
{
string abc;
// note the word virtual here!
public virtual int get_1()
{
return 1;
}
public int get_number()
{
return get_1()+1;
}
}
I cant access static method from new object and not allow create same name non-static method.I need to use same name method static and non-static.
Foo class has some default variables. I create new object and set default variables.
Sample code block
class Foo
{
public void abc()
{
//...
}
public static string xyz(string s)
{
return "bla bla";
}
}
public void btn1_click()
{
System.Windows.Forms.MessageBox.Show(Foo.xyz("value"));
//Works OK
}
public void btn1_click()
{
Foo f1=new Foo();
//f1..
f1.xyz("value");
//Cant access non static method.
}
Thanks in advance.
If the class has default values, the correct place to populate them is in the class constructor:
public class Foo
{
public Foo()
{
// set default values here.
}
}
If you still want to use these default values as static members - no problem:
public class Foo
{
public static const int DEFAULT_INT_VALUE = 5;
public Foo()
{
IntValue = DEFAULT_INT_VALUE;
}
public int IntValue {get;set;}
}
How can I get the type (not a name string, but a type itself) of the current class, in a static method of an abstract class?
using System.Reflection; // I'll need it, right?
public abstract class AbstractClass {
private static void Method() {
// I want to get CurrentClass type here
}
}
public class CurrentClass : AbstractClass {
public void DoStuff() {
Method(); // Here I'm calling it
}
}
This question is very similar to this one:
How to get the current class name at runtime?
However, I want to get this information from inside the static method.
public abstract class AbstractClass
{
protected static void Method<T>() where T : AbstractClass
{
Type t = typeof (T);
}
}
public class CurrentClass : AbstractClass
{
public void DoStuff()
{
Method<CurrentClass>(); // Here I'm calling it
}
}
You can gain access to the derived type from the static method simply by passing the type as a generic type argument to the base class.
I think you will have to either pass it in like the other suggestion or create a stack frame, I believe if you put an entire stack trace together though it can be expensive.
See http://msdn.microsoft.com/en-us/library/system.diagnostics.stacktrace.aspx
if you are calling this static method only from derived classes you can use 'System.Diagnostics.StackTrace' like
abstract class A
{
public abstract string F();
protected static string S()
{
var st = new StackTrace();
// this is what you are asking for
var callingType = st.GetFrame(1).GetMethod().DeclaringType;
return callingType.Name;
}
}
class B : A
{
public override string F()
{
return S(); // returns "B"
}
}
class C : A
{
public override string F()
{
return S(); // returns "C"
}
}
The method can't be static if you're going to call it without passing in a type. You can do this:
public abstract class AbstractClass {
protected void Method() {
var t = GetType(); // it's CurrentClass
}
}
If you also need it to be accessible from a static context, you can add an overload, even a generic overload, e.g.:
public abstract class AbstractClass {
protected static void Method<T>() {
Method(typeof(T));
}
protected static void Method(Type t) {
// put your logic here
}
protected void Method() {
Method(GetType());
}
}
I have such class in c#:
public class Foo
{
public static readonly int SIZE = 2;
private int[] array;
public Foo
{
array = new int[SIZE];
}
}
and Bar class:
public class Bar : Foo
{
public static readonly int SIZE = 4;
}
What I want to accopmlish is to create a Bar instance with array size taken from overrided SIZE value. How to do it properly?
You can't do it this way. You could use a virtual method:
public class Foo
{
protected virtual int GetSize(){return 2;};
private int[] array;
public Foo
{
array = new int[GetSize()];
}
}
It's also possible to use reflection to look for a static field SIZE, but I don't recommend that.
Your SIZE constant is static, and static fields aren't inherited - Foo.SIZE and Bar.SIZE are two different constants that have nothing to do with each other. That's why Foo's constructor call will always initialize with 2, not 4.
What you can do is create a protected virtual void Initialize() method in Foo that initializes the array with 2, and override it in Bar to initialize it with 4.
You cannot inherit static fields; instead use the below:
public class Foo
{
protected virtual int SIZE
{
get
{
return 2;
}
}
private int[] array;
public Foo()
{
array = new int[SIZE];
}
}
public class Bar : Foo
{
protected override int SIZE
{
get
{
return 4;
}
}
}
Virtual is like saying "This is the default value of the base class"; whilst Override changes the value on the class implementing "Foo".
I'm not sure if this is possible but I want to be able to have fields/method of a base class to be visible to the parent.
Say for example I have a class:
public class ExampleFile
{
private Stream _stream;
private long _baseoffset;
public ExampleFile(Stream input)
{
_stream = input;
_baseoffset = input.Position;
}
public void SeekTo(long offset)
{
_stream.Seek(offset + _baseoffset, SeekOrigin.Begin);
}
}
And I then use that class as a base for another class:
public class ExampleClass : ExampleFile
{
public ExampleClass(Stream input)
: base(input)
{
}
public byte[] GetSomething()
{
byte[] id = new byte[5];
SeekTo(2);
base._stream.Read(id, 0, 5);
return id;
}
}
Is there any way I can make the fields/methods of ExampleFile only visible to ExampleClass?
Use the protected modifier instead of public/private on the fields/properties/methods you want exposed to child classes.
public class ExampleFile
{
protected Stream _stream; // no longer private, so the inherited
protected long _baseoffset; //classes can access them
public ExampleFile(Stream input)
{
_stream = input;
_baseoffset = input.Position;
}
public void SeekTo(long offset)
{
_stream.Seek(offset + _baseoffset, SeekOrigin.Begin);
}
}
What do you mean by base class? The base class is supposed be the parent in your case! If you want a member of a class to be accessible to one of its descendants, use the protected modifier. If you want a member of the child class to be accessible to its parent, do so through a getter.