This is a noob question
using System.name;
class class_name
{
private className Obj;
public class_name()
{
}
public function()
{
Obj.function <----- why i cant acesss the global varible here ??
}
}
When i type the class the instellisence docent show any thing :-s
I'm assuming there was just some confusion with the names and, by function, you meant class_name, or instead of class_name you meant className.
In order to access a method this way, it must be declared as static. Otherwise, you must first create an instance of the class and access the method through the instance.
EDIT The code you posted is very confusing. The following works just fine for me.
class Class1
{
public void Function1()
{
}
}
class Class2
{
private Class1 obj;
public void Function2()
{
obj.Function1();
}
}
Have you instantiated that class?
Related
Hi trying to make a class inside a static class to use in JINT but when it's referenced I get an error
C# code
namespace Hi {
public static class Ok {
public class Wowa {
public Wowa(){}
}
}
}
But when I try to make a new one in JavaScript I get an error "the object cannot be used as a constructor" from JINT
var k = new Hi.Ok.Wowa()
Am I doing this right? How can I set up the C# to be able to use the above code in JavaScript from JINT?
BTW IF instead of "Ok" being a static class, rather a namespace, it works, but I want it as a class because I want to have static methods in it also
you cant use none-static class in a static class (ReadThis) but if you remove (static) in your frist class
namespace Hi {
public class Ok {
public class Wowa {
public Wowa(){}
}
}
}
and it can be said that it does not make much difference because (Static) only makes subcategories of your class have to use (Static).
But if you want your class to be impossible to build on variables, you can use abstract(ReadThis)
namespace Hi {
public abstract class Ok {
public class Wowa {
public Wowa(){}
}
}
}
and
Main()
{
Ok k = new Ok();//Error
}
Imagine you have this:
namespace Hi
{
public static class Ok
{
public class Wowa
{
public Wowa() { }
public static string MyStaticMethod() => "Hello from 'Static Method'";
public string MyNormalMethod() => "Hello from 'Normal Method'";
}
}
}
It's possible to use non-static class Wowa by making an instance of it , and then you can call MyNormalMethod of that instance (you can only call not-static method within instance of that class).
Hi.Ok.Wowa wowa = new Hi.Ok.Wowa();
wowa.MyNormalMethod();
And without making any instance of Wowa you can call static method within it, like this:
Hi.Ok.Wowa.MyStaticMethod();
Finally you can see working code here.
Say I have a generic class Foo, that has a variable that is protected
public class Foo<T>
{
protected bool knowsFu;
}
I also have 2 sub-classes: Bar and Pipe
public class Bar : Foo<Bar> {}
public class Pipe : Foo<Pipe> {}
It is actually possible for me to access the knowsFu in Pipe FROM Bar, e.g.:
public class Bar : Foo<Bar>
{
void UpdateFuInOtherClass(Pipe p)
{
p.knowsFu = false;
}
}
Is this intended behaviour? (If so, what would be the usecase?)
Is there a way for me to prevent other Foo-Subclasses from modifying/reaching the protected variable inside of my current subclass?
More specifically: I'm using a generic class to implement the Singleton-Pattern:
https://en.wikipedia.org/wiki/Singleton_pattern
However, I'm currently able to access any singleton's protected instance-variable, as long as I am inside of another Singleton. Is there a way to prevent this?
EDIT: It might be relevant to note that the protected variable (knowsFu) is actually STATIC as well.
EDIT2: Ok, maybe the example was abit too generic.. here's how I'm actually currently implementing it:
why use Singleton? A:The platform I'm working on is Unity3D, in which the pattern is used frequently
I have a generically typed abstract class SingletonBehaviour
public abstract class SingletonBehaviour<T> where T : MonoBehaviour
{
public static T Instance { get { return instance; } }
protected static T instance { get; private set; } }
// Loading is done through Unitys Awake-Method
}
One of the Singleton-Objects that I'm using is the APIManager
public class APIManager : SingletonBehaviour<APIManager>
{
// Methods like SendHTTPPost(), HTTPGet(), etc.
}
However, since most of my projects need some better API-implementation than that, what I'm currently doing is:
public class ProjectAAPIManager : APIManager
{
// Overriding Instance so my return value is not APIManager but instead ProjectAAPIManager
public static new ProjectAAPIMamager Instance { get { return (ProjectAAPIManager)instance; } }
}
This ^ is the reason my (inner) instance-variable is protected, and not private.
However, because of this, any other SingletonBehaviour in my project can now access the (inner) instance-variable on my ProjectAAPIManager
public class GameController : SingletonBehaviour<GameController>
{
private void AMethod()
{
// Accessing inner variable instead of public one
ProjectAAPIManager.instance.DoSomething();
}
}
As it's only the getter, this currently does not really matter. But what if I'd need access to the setter in my subclass as well?
Also: would it be worth it to generically type my APIManager as well?
Your question is nothing short of bewildering. How can you make a protected member not be accesible from a derived class? Well, a good start is not making it protected.
protected is by definition exactly what you don't want, so don't use it! Use private instead.
If what you are asking is how to make it a readonly member when accessed from derived types, you have two options:
Declare it as readonly in the base class if possible.
Use a protected property instead with a private setter.
Many novice coders seems to think protected members aren't part of the public surface of the type but they really are, as long as the class can be extended. As such, the rules of public members apply: never expose public fields unless they are readonly or constants, use properties instead.
You should not have classes that implement your generic singleton class.
Otherwise, by default, your protected fields will be accessible by the subclasses (it's what "protected" keyword does)
Instead, you should do something like this:
class Program
{
static void Main(string[] args)
{
var barInstance = Foo<Bar>.GetInstance();
}
}
public class Foo<T> where T : new()
{
protected bool knowsFu;
private static T _instance;
public static T GetInstance()
{
if (_instance == null)
_instance = new T();
return _instance;
}
}
public class Bar
{
public Bar()
{
}
}
Edit 1:
To use a singleton, you should not make another class implement the singleton behavior (This is not how the singleton pattern works).
To use the same classes as your second example, you should do something like this.
public class SingletonBehaviour<T> where T : new()
{
public static T Instance
{
get
{
if(instance == null)
instance = new T()
return instance;
}
}
private static T instance { get; set; }
}
public class APIManager // This class should not inherit from the SingletonBehavior class
{
// Methods like SendHTTPPost(), HTTPGet(), etc.
}
public class ProjectAAPIManager : APIManager
{
public ProjectAAPIManager GetInstance() => SingletonBehavior<ProjectAAPIManager>.Instance();
}
I have recently converted a VB class to C# and it seems I ran into a problem; I think I know how to solve it but with all my reading I am now looking for a more clear answer with guidance.
Consider the following code with a struct FileDetail inside (This is just an example - so please do not assume it is FileDetail as in Files..)
The Struct needs to be accessed internally and externally - they are passed by value and not reference types so struct appears to be the way to go here instead of a class (looking at the whole code that is). In the form class MyForm I get the error that FileDetails does not exist in class IAFT.
public class IAFT
{
public struct FileDetail
{
public string FileType;
public int FileNumber;
}
}
public class MyForm
{
MyForm()
{
public IAFT.FileDetail fd = new IAFT.FileDetail();
// IAFT.FileDetail
}
}
ERROR I get.
The type name 'FileDetail' does not exist in the type 'IAFT' (CS0426)
Red squigly in VS2013 is under the type declaration; left hand side of the assignment.
Both are in the same namespace if that is any help.
I have read posts on SO that tell me I can declare my variable fd just as it is above (did not make sense to me since I have no instance - but I tried it.) I do not want to create an instance to get an instance ; I believe I want the one as it exists inside the instance of IAFT. Maybe there is something I am not understanding.
Should I encapsulate the struct as a class instead?
Should I put the struct outside of the class IAFT?
[This is what I was thinking I should do.]
Should I do something else ?
Your code example is wrong and wont compile. Your error is that you are delcaring
public IAFT.FileDetail fd = new IAFT.FileDetail();
Inside the public constructor. You cannot declare scope in a function scoped variable. If you take the Public declaration off your code compiles just fine.
So try this;
public class IAFT
{
public struct FileDetail
{
public string FileType;
public int FileNumber;
}
}
public class MyForm
{
public MyForm()
{
IAFT.FileDetail fd = new IAFT.FileDetail();
}
}
Its perfectly good practise to nest classes.
There's no need to do the struct static nor moving it outside, the problem lies that you declared the var as public inside a function, and that's not allowed, localvariables don't have access modifiers as they're local.
Try this:
public class IAFT
{
public struct FileDetail
{
public string FileType;
public int FileNumber;
}
}
public class MyForm
{
public IAFT.FileDetail fd = new IAFT.FileDetail();
}
I would to it this way:
public class IAFT
{
fileDetail FileDetail = new fileDetail();
}
public class fileDetail
{
public string FileType;
public int FileNumber;
}
public class MyForm
{
MyForm()
{
IAFT.FileDetail.FileType = "test";
//IAFT.FileDetail
}
}
I hope that it works, I haven't tested it.
I have error
Cannot access a non-static member of outer type 'Project.Neuro' via
nested type 'Project.Neuro.Net'
with code like this (simplified):
class Neuro
{
public class Net
{
public void SomeMethod()
{
int x = OtherMethod(); // error is here
}
}
public int OtherMethod() // its outside Neuro.Net class
{
return 123;
}
}
I can move problematic method to Neuro.Net class, but I need this method outside.
Im kind of objective programming newbie.
Thanks in advance.
The problem is that nested classes are not derived classes, so the methods in the outer class are not inherited.
Some options are
Make the method static:
class Neuro
{
public class Net
{
public void SomeMethod()
{
int x = Neuro.OtherMethod();
}
}
public static int OtherMethod()
{
return 123;
}
}
Use inheritance instead of nesting classes:
public class Neuro // Neuro has to be public in order to have a public class inherit from it.
{
public static int OtherMethod()
{
return 123;
}
}
public class Net : Neuro
{
public void SomeMethod()
{
int x = OtherMethod();
}
}
Create an instance of Neuro:
class Neuro
{
public class Net
{
public void SomeMethod()
{
Neuro n = new Neuro();
int x = n.OtherMethod();
}
}
public int OtherMethod()
{
return 123;
}
}
you need to instantiate an object of type Neuro somewhere in your code and call OtherMethod on it, since OtherMethod is not a static method. Whether you create this object inside of SomeMethod, or pass it as an argument to it is up to you. Something like:
// somewhere in the code
var neuroObject = new Neuro();
// inside SomeMethod()
int x = neuroObject.OtherMethod();
alternatively, you can make OtherMethod static, which will allow you to call it from SomeMethod as you currently are.
Even though class is nested within another class, it is still not obvious which instance of outer class talks to which instance of inner class. I could create an instance of inner class and pass it to the another instance of outer class.
Therefore, you need specific instance to call this OtherMethod().
You can pass the instance on creation:
class Neuro
{
public class Net
{
private Neuro _parent;
public Net(Neuro parent)
{
_parent = parent;
}
public void SomeMethod()
{
_parent.OtherMethod();
}
}
public int OtherMethod()
{
return 123;
}
}
I think making an instance of outer class in inner class is not a good option because you may executing business logic on outer class constructor. Making static methods or properties is better option. If you insist making an instance of outer class than you should add another parameter to outer class contructor that not to execute business logic.
I have the following code:
class EmployeeFactory
{
public enum EmployeeType
{
ManagerType,
ProgrammerType,
DBAType
}
}
I want to access this in MAIN class (Program). I have written the following code. IT WORKS. But I want to know how I can access the ENUM without instantiating the class -- Means ENUM is like a static variable (Class Level Variable) ? Any help ?
class Program
{
static void Main(string[] args)
{
Console.WriteLine(EmployeeFactory.EmployeeType.ProgrammerType); // WORKS WELL
}
}
or do I need to write it this way?
EmployeeFactory ef = new EmployeeFactory();
ef.EmployeeType.ProgrammerType
You can access it simply using the class.
EmployeeFactory.EmployeeType.ProgrammerType
The enum is part of the class, not part of a class instance.
But I want to know how I can access the ENUM without instantiating the class
The original way you're accessing this enum
Console.WriteLine(EmployeeFactory.EmployeeType.ProgrammerType);
already accomplishes that; you are accessing the enum without instantiating the class.
try something like this ...
public interface IEnums
{
public enum Mode { New, Selected };
}
public class MyClass1
{
public IEnums.Mode ModeProperty { get; set; }
}
public class MyClass2
{
public MyClass2()
{
var myClass1 = new MyClass1();
//this will work
myClass1.ModeProperty = IEnums.Mode.New;
}
}
or
you can directly access like this....
EmployeeFactory.EmployeeType.ProgrammerType
i hope it will helps you