Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
thank you many times for increasing my knowledge over property
so sir can i say that a function residing in one class can't act in other class ?

What I have tried:

many many thanks to sir for increasing my knowledge over property. furthermore i would be highly honoured if in this way sir increase my knowledge....
Posted
Updated 5-Feb-21 22:14pm

No, a class can be inherited for example, see: C# Inheritance[^]
Also a Static class method (function) can be used without problem from another static class.

Another important concept are access modifiers, see: C# Access Modifiers[^]
 
Share this answer
 
v2
I get the definite feeling that you have no idea what you are doing: you seem not to understand classes at all.

Quote:
can i say that a function residing in one class can't act in other class ?
Means nothing, nothing at all.

When you create a class, you are describing an object - the first 'O' in OOPs - and saying what it can do.
You define interfaces to that class in the form of properties and methods (and others, but we'll ignore them for the moment), and you say what other classes can "see" them via access modifiers:
private means that only this class can see them.
public means that any class can see them.
There are other modifiers (internal, protected, protected internal, and private protected ) but for the moment, just work with these two.

If you declare a method as private, then you can't call it from outside the class. If you declare it as public, you can. Except ... unless the method is static you can only call it if you have an instance of the class to work with. If it is static then you can just call it via the class name, but it can only access static information within the class, never anything that is specific to an instance.

Think of it like cars: I have a car, you have a car: "my car", "your car". If I put my mobile in the glove box of "my car", you don't expect to be able to open the glove box on "your car" and find my mobile there - the glove box of a car is specific to that car and no other: it is an instance property.
"My car" is black. "Your car" is green - again, an instance property (one that is not declared as static) because you have to specify which car you are talking about when you ask about the colour. Asking "what colour is a car?" is a silly question - it can't be answered!
On the other hand, all cars - including yours, including mine - have the same number of wheels. So you can ask "how many wheels has a car?" and expect to get the same answer: four (because if it had two, it would be a motorbike!)

"NumberOfWheels" is a static property of a Car; "Colour" is an instance property; "GloveBox" is an instance property.
To get the number of wheels, you just ask the class:
C#
Console.WriteLine($"A car has {Car.NumberOfWheels} wheels");

But to ask what colour it is, you need to specify the specific instance of a car:
C#
Console.WriteLine($"My car is {myCar.Colour}");
Console.WriteLine($"Your car is {yourCar.Colour}");
foreach (Car car in universityCarParkA.Cars)
   {
   Console.WriteLine($"The car with registration {car.RegNo} is {car.Colour}");
   }

The same thing is needed to look in the glovebox:
C#
Container gloveBox = myCar.GloveBox;
gloveBox.Open();
glovebox.Add(myMobile);
gloveBox.Close();

So your question is meaningless:
Quote:
can i say that a function residing in one class can't act in other class ?
Because functions (which are called methods in C#) can be called from anywhere provided the access is allowed by the function definition, and the right instance is used for non-static methods.

Does that make sense?
 
Share this answer
 
Comments
Member 12712527 6-Feb-21 5:04am    
yes sir cleared...
Member 12712527 6-Feb-21 5:05am    
confused a bit
OriginalGriff 6-Feb-21 5:43am    
About what?

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