Click here to Skip to main content
15,922,533 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I am creating a class in .aspx.cs file, which has properties and a method. I call this method when the page is posted for the first time and set the propery values. Now i want to use these properties in a file which is present in App_Code. Can this be done. if so how can it be done.

Waiting for the reply.

1 solution

There is no such concept as calling an object. You can only call method; well, property too, as reading or assignment can be done via getter or setter methods, respectively.

A method can be a static method (in some languages called "class method" as it related to the class itself and not to any instance in particular) or instance method. There is only one difference: instance method require an instance to call, and the body of instance method receives a reference to the instance passed during a call; this reference is passed via a "hidden" parameter called "this".

Now I have to explain the syntax. It's was not nice not to tag the language in the question. How can I explain the syntax? As it's usually C# which is assumed by default for .NET, I'll show it in C#:

The syntax is a bit different, too:
class MyClass {
    internal static void ClassMethod() { //cannot address members of any instance, only other static members
    } //ClassMethod
    internal void InstanceMethod() { /* ... */ this.Field = /* ... */ }
    int Field;

MyClass ClassMethod(); //no instance needed, no "this" reference passed

MyClass instance = new MyClass( /* ... */ );
instance.InstanceMethod(); // during execution of the body of the method this == instance

Remaining aspect is access modifiers. By default, access in private. The access modifier internal (see the sample above) opens up access from any other class, any code placed in the same assembly. This assembly can be references by other assemblies, but they can get access to the methods shown above is internal is replaced with public.

Note, that access modifiers does not provide any protection (but very helpful to avoid accidental mistakes and isolate parts of code, so it's the best not to "over-publish" anything providing minimal possible access). With Reflection, every member can be accessed regardless access modifier.

A call also can be done with Reflection, but this is quite a different story.

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900