Sorry I'm kinda new to c# how would I make a class where I can access it like this:
Myclass.subclass.method();
This is what I have now:
namespace zzcore
{
class myclass
{
class subclass
{
public static void method() { }
}
}
}
What happens here is that a nested class without a visibility modifier is implicitly private. In this context, private means that only the parent class can see it.
Declare both classes as public and you will be able to call myclass.subclass.method();
namespace zzcore
{
public class myclass
{
public class subclass
{
public static void method() { }
}
}
}
Working example: http://ideone.com/tJVKJ
Related
I know this is meaning less question, but I'm confused why sealed class create its instance.
public sealed class SealedClass1
{
static SealedClass1()
{
}
public string GetmeassageFromSealed(string mesage)
{
return mesage;
}
}
Here i decorate my constructor as private
public sealed class SealedClass1
{
static SealedClass1()
{
}
private Singleton()
{
}
public string GetmeassageFromSealed(string mesage)
{
return mesage;
}
}
The sealed keyword in C# means that no class can inherit from that class.
So if you have this scenario:
public class Test { }
The following is valid
public class NewTest: Test { }
However, as soon as you add the sealed keyword, the compiler with throw an error when trying to compile that library.
public sealed class Test { }
public class NewTest: Test { }
This would throw
'NewTest' cannot inherit from sealed class 'Test'.
In short, the sealed keyword is a compile time check to ensure that there is no inheritence.
A Sealed Class can be instantiated, also it can inherit from other classes but it can not be inherited by other classes.
How is the ModelDuck constructor able to access the fields in the Duck class - flyBehaviour and quackBehaviour. Thanks
namespace HeadFirst
{
public class ModelDuck : Duck
{
public ModelDuck()
{
flyBehaviour = new FlyNoWay();
quackBehaviour = new Quack();
}
public override void display()
{
Console.WriteLine("I am a model duck");
}
}
}
public abstract class Duck
{
public FlyBehaviour flyBehaviour;
public QuackBehaviour quackBehaviour;
public Duck()
{
}
}
The properties belong the the base class.
You should always be able to acces the properties and methods of your base class
I have an inheritance tree with a bunch of different classes. Each of these classes has some static properties that I need acces to from time to time. Sometimes I need the property of a particular class, and sometimes I need the property of the specific class some polymorphic instance turns out to be.
This would be easy in, say, Java (I think). Just make a bunch of static fields (can these be overriden? I'm not sure). But in C#, non-static fields can ONLY be accessed via an instance (naturally), and static fields can ONLY be accessed via their corresponding class (unnaturally).
And, you can't "overload" by, er, staticity. If a class has a static and a non static Foo, doing instance.Foo fails because it is unclear to the compiler which Foo you're referring to even though it's impossible you're referring to the static one since it's disallowed.
Ok, I'll provide some code. Say I have this:
class Base
{
public static readonly string Property = "Base";
}
class Child1 : Base
{
public static readonly new string Property = "Child 1";
}
class Child2 : Base
{
public static readonly new string Property = "Child 2";
}
And then, somewhere:
public void SomeMethod(Base instance)
{
System.Console.WriteLine(instance.Property); // This doesn't work.
}
And somewhere else:
public void SomeOtherMethod()
{
System.Console.WriteLine(Child2.Property);
}
I want something like that, that actually works.
As Peter Duniho said, this can be done with reflection.
For example, these can be defined within the base class:
public const string Property = "Base";
public virtual string InstanceProperty
{
get
{
return (string)this.GetType()
.GetField("Property", BindingFlags.Public | BindingFlags.Static)
.GetValue(null);
}
}
And then each derived class just has to redefine Property using the new keyword.
I think the best you'll do in C# is something like this:
public class BaseClass
{
public virtual string InstanceProperty
{
get { return StaticProperty; }
}
public static string StaticProperty
{
get { return "BaseClass"; }
}
}
public class Derived1Base : BaseClass
{
public override string InstanceProperty
{
get { return StaticProperty; }
}
public new static string StaticProperty
{
get { return "Derived1Base"; }
}
}
public class Derived1Derived1Base : Derived1Base
{
}
public class Derived2Base : BaseClass
{
public override string InstanceProperty
{
get { return StaticProperty; }
}
public new static string StaticProperty
{
get { return "Derived2Base"; }
}
}
I have the following classes:
namespace Bla.Bla
{
public abstract class ClassA
{
public virtual void Setup(string thing)
{
}
public abstract bool IsThingValid();
public abstract void ReadThings();
public virtual void MatchThings() { }
public virtual void SaveThings() { }
public void Run(string thing)
{
Setup(thing);
if (!IsThingValid())
{
}
ReadThings();
MatchThings();
SaveThings();
}
}
}
namespace Bla.Bla
{
public class ClassB : ClassA
{
ClassB() { }
public override void IsThingValid()
{
throw new NotImplementedException();
}
public override void ReadThings()
{
throw new NotImplementedException();
}
}
}
Now I try to do the following:
public class ClassC
{
public void Main()
{
var thing = new ClassB();
ClassB.Run("thing");
}
}
Which returns the following error: ClassB is inaccessible due to its protection level.
But they are all public.
This error is a result of the protection level of ClassB's constructor, not ClassB itself. Since the name of the constructor is the same as the name of the class* , the error may be interpreted incorrectly. Since you did not specify the protection level of your constructor, it is assumed to be internal by default. Declaring the constructor public will fix this problem:
public ClassB() { }
* One could also say that constructors have no name, only a type; this does not change the essence of the problem.
Also if you want to do something like ClassB.Run("thing");, make sure the Method Run(); is static or you could call it like this: thing.Run("thing");.
You could go into the designer of the web form and change the "webcontrols" to be "public" instead of "protected" but I'm not sure how safe that is. I prefer to make hidden inputs and have some jQuery set the values into those hidden inputs, then create public properties in the web form's class (code behind), and access the values that way.
I have a name space Company.Controls, which contains several controls. I also have a class called "Common" which contains enums/structures/static methods that I use throughout the controls.
Is there a way to make these "Common" peices belong to the Company.Controls namespace this way I don't have to keep typing "Common.Structure"? Essentially having he "Common" both a namespace and a class.
Just seems messy and confusing when reading the code.
example (all the other controls are in the Blah.Controls.Common namespace)
namespace Blah.Controls
{
public enum ControlTouchState
{
Down = 0x00,
Up = 0x01,
}
public Common()
{
//Stuff here
}
}
Thanks.
You can't get exactly what you want; in C# all methods have to be in a class.
Depending on what is in your Common class, you might be able to find something a slightly more satisfying by using extension methods:
namespace Blah.Controls
{
public class CommonControl { }
public static class Common
{
public static void Foo(this CommonControl cc) { }
}
public class Control1 : CommonControl
{
public void Bar()
{
this.Foo();
}
}
}
Another thing you might consider is using partial classes which would let you write simple wrappers elsewhere:
namespace Blop.Controls
{
public static class Common
{
public static void Foo() { }
}
public partial class Control1
{
public void Bar()
{
Foo();
}
}
public partial class Control1
{
public void Foo()
{
Common.Foo();
}
}
}
Obviously, introducing some inheritence could eliminate some of the duplication; I'm assuming you don't want to do that.
Is there some reason that the nested types in Common MUST be nested? Why not separate them out into their own namespace?
namespace Common
{
public struct Structure
{
// ...
}
public enum Enumeration
{
// ...
}
public class Common
{
// ...
}
}
You could then use the Common namespace as such:
namespace Blah.Controls
{
using Common;
class Control
{
Struct myStruct;
Enumeration myEnum;
Common myCommon; // references the class, not the namespace
}
}