from / where Can i call static class methods in a class? - c#

I have the below code in the same namespace.
public static class usercls
{
public static int testc()
{
int s=1;
}
}
public class User : Page
{
private static User user;
int s=usercls.testc();//why not accessible here?
}
I'm unable to access the static class outside of the class. Can someone help me to identify this?

Hi the function testc() doesnt return any value.
It should look like this
public static int testc()
{
int s=1;
return s;
}
or like this
public static int testc()
{
return 1;
}
After that your code should compile.
Other classes cannot access the class functions of usercls because the compiler did not compiled it, because there was an error, once you fix that error, it will be accessible from all the other classes.
Also
You are trying to call it directly in your other class, the call should be within a function like so:
public class User : Page
{
private static User user;
public User()
{
int s=usercls.testc();//why not accessible here?
}
}

Related

is it possible to use a method from a different class within the same assembly?

I am new to c#. I am curious if I can do something like this:
namespace Test {
public class SomeClass
{
public static double someMethod()
{
//do something
}
}
public class AnotherClass
{
someMethod();
}
}
I tried something similar to this and it gave me a "does not exist in current context" error. I tried referring it as SomeClass.someMethod() but still gives me the same error.
if the method is static you are able to call it the following way SomeClass.someMethod()
public class AnotherClass
{
public void anotherMethod ()
{
SomeClass.someMethod();
}
}
If you want to class contains methods and fields of other class you should use inheritance
public class SomeClass
{
public static double someMethod()
{
//do something
}
}
public class AnotherClass : SomeClass
{
}

C# Give access to specific unrelated class

Is there a way to modify the access of some attribute to a specific class? More specifically, I want to create a property that has a public get, but can only be set by a certain class.
Example:
public Class1
{
Class2.SomeInt = 5;
}
public static Class2
{
private static int someInt;
public static int SomeInt
{
get { return someInt; }
(give access to Class1 only somehow?) set { someInt = value; }
}
}
Update (more info):
I'm doing this in xna, I want the main type (Game1) to be the only thing that can modify a static helper class. It's for a group project in school, we're using SVN (not sure how that'd be relevant), I could just tell everyone in my group to avoid setting the values, but I was wondering if there was a better way.
This sounds like the friend access modifier, which C# doesn't have. The closest I've seen to this in C# is to have the "unrelated" class be an interface and have a private implementation within a class. Something like this:
public interface IWidget
{
void DoSomethingPublic();
}
public class SomeObject
{
private ObjectWidget _myWidget = new ObjectWidget();
public IWidget MyWidget
{
get { return _myWidget; }
}
private class ObjectWidget
{
public void DoSomethingPublic()
{
// implement the interface
}
public void DoSomethingPrivate()
{
// this method can only be called from within SomeObject
}
}
}
Code external to SomeObject can interact with MyWidget and sees anything that's on the IWidget interface, but code internal to SomeObject can also non-interface public members on MyWidget.
It seems to be impossible in C#. You can only use public, protected, protected internal, internal and private access modifiers.
But you can, for instance, make an assembly that contains only these two classes and set the internal modifier for the SomeInt setter or nest one class into another.
If you want to just hide a setter from the IntelliSense, you can define this setter in some interface and implement it explicitly:
public interface IHidden<T>
{
T HiddenPropery { set; }
}
public class SomeClass : IHidden<int>
{
private int someInt;
public int HiddenPropery
{
get { return someInt; }
}
int IHidden<int>.HiddenPropery
{
set { someInt = value; }
}
}
Usage:
// This works:
((IHidden<int>)new SomeClass()).HiddenPropery = 1;
// This doesn't:
new SomeClass().HiddenPropery = 1;

Does accessing a static member invoke the base class constructor?

Even though all common sense says no, I still am asking this question just to get a second opinion and become sure.
If I have a class hierarchy like so:
public class IntermediateObjectContext : System.Data.Objects.ObjectContext
{
public static Action<string> PrintHello { get; set; }
}
public class MyDatabaseContext : IntermediateObjectContext
{
public ObjectSet<Foo> Foos
{
get { // ... }
}
}
Then from a third, unrelated to Entity Framework object, if I access the static member of the IntermediateObjectContext class, in this case, if I subscribe to the delegate of the class, will that somehow instantiate a new ObjectContext?
class SomeClass
{
public void SomeMethod()
{
IntermediateObjectContext.PrintHello += SayHello;
}
public void SayHello(string s)
{
Debug.Print(s);
}
}
All reason says no, common sense says it won't, but I just want to make sure. I am trying to track down a memory hogger object.
What happens if
What happens to the memory situation if I have a static collection for SomeClass types like so:
public class SomeClassCollection
{
private static Collection<SomeClass> _col =
new Collection<SomeClass>();
public void Add(SomeClass c) { _col.Add(c); }
public void Remove(SomeClass c) { _col.Remove(c); }
}
And then some code adds SomeClass instances to SomeClassCollection like so:
public SomeClassCollectionConfig
{
public static RegisterSomeClasses()
{
SomeClassCollection.Add(new SomeClass());
SomeClassCollection.Add(new DerivesClassOfSomeClass());
}
}
(1) No, it won't instantiate an object.
(2) What happens if:
There it will allocate the empty collection col the first time any member of SomeClassCollection is accessed.
From the code, that's all it will do. You aren't using _col anywhere in the code presented.

C# Cannot Reach Class Elements

It seems like a very simple error, but I could not solve it.
I have created a Class named User in User.cs, and when I instantiate it in another .cs file, it does it, but I cannot either change its properties or reach its properties.
User user = new User();
I create a new instance like this, but then I cannot reach. For example:
user.name
The content of User class is the following:
public class User
{
public static string name;
public static int age;
public static int height;
public static int weight;
}
What is the reason and how can I solve it?
Thanks
You have created a static object, don't instantiate the class to use it, just do
User.name
Alternativly remove the static keyword.
The word static means you don't need to make a new insance of the class to access something so for a class with
public Class {
public static object myAttribute;
}
Class.myAttribute
But if you don't use the static key word
public Class {
public object myAttribute;
}
Class myClass = new Class();
myClass.myAttribute;
You want to use a static value when your value does not depend on any other variables in the same class. When they do depend on varibles (or manipulations of variables) in the same class then use non static.
remove keyword static from fields
Is name public member? Set it public or create a public property to access it...
public string Name { get { return name; } }
EDIT: as name is a static member, you cannot write myUser.name. It's User.name. I think you should remove static (and learn some basis...).
What is the error message that occurs during build? It should be clear enough.
You have to declare name as public in the user class. For example:
public string name { get; set; }
You cannot access static fields from the instance of a class. You can either not declare the fields static:
public class User
{
public string name;
public int age;
public int height;
public int weight;
...
Or you can access the class statically:
User.name

Attributes: Access Designation

While I was refering to a book , I got the following statements:
When a data type or method is defined as public , Other Objects can directly access it. When a data type or method is defined as private , only the specific object can access it.
Now this is really confusing. Public and Private are Access Specifiers which only define the scope of a attribute or method.
Why object is mixed with access specifiers? Does object has to do any thing with public , private or protected apart from the fact that if some thing is defined as public then objects too will be able to access irespective of the scope
This is not a scope question but an access limitation modifier. If you declare a function as private that means only that class can call that function.
Public:
Any one can call these functions
Private:
Only that class and the refection engine
Protected:
Only that class and its derived member
Internal:
Public to all the classes in that assembly
A small ex
public class A
{
public int x;
protected int y;
private int z;
}
public class B : A
{
public int CallPro()
{
return y;
}
public int CallPriv()
{
return z; //error
}
}
static void Main()
{
A oa;
oa.x; //Fine
oa.y; //Error
oa.z; //Error
}
Public and Private are Access Specifiers which only define the scope
of a attribute or method.
And that defines the behaviour of the object. Hence the access specifiers are important for the object. Imagine if you have a facade kind of object, you don't want to expose all the details of the operation, rather you want a simple public interface (eg, Save() method) to be exposed for the consumers. That's why you have to consider the specifiers for objects.
public class CustomerFacade()
{
public bool Save(Customer c)
{
VerifyCustomer(c);
// lots of other steps which the caller does not need to know
SaveorUpdateCustomer(c);
}
private void VerifyCustomer(Customer c)
{
}
private void SaveorUpdateCustomer(Customer c)
{
}
}
public class CustomerController()
{
public bool Save(Customer c)
{
return new CustomerFacade().Save(c);
}
}

Categories

Resources