VBForums >
.NET >
C# > [2.0] What modifier to use in class definition?
Click to See Complete Forum and Search --> : [2.0] What modifier to use in class definition?
bnathvbdotnet
Aug 14th, 2007, 01:37 PM
I have a class name "exportfilestatus" which currently has public scope.
I want to create an object of this class in some method and set the properties of this object.
If I don't want to return this object and if I still want to use the properties of the object, what modifier (public/shared/internal) should I use in the definition of "exportfilestatus" class?
Code:
public class exportfilestatus
{
private double _currGroupBalance;
public double CurrGroupBalance
{
get { return _currGroupBalance; }
set { _currGroupBalance = value; }
}
}
thanks
nath
jmcilhinney
Aug 14th, 2007, 07:23 PM
I'm afraid that your question doesn't make a lot of sense. Either you have access to a class or you don't. You can't create an instance of a class and set its properties if you don't have access to the class. If you do have access to the class then you can create an instance and do whatever you want with that instance.
A class is either public or internal. If its internal then only the assembly in which its declared has access to it. If its public then its accessible anywhere. A class can also be declared private but ONLY if it's declared within another class. The nested class is then only accessible within its parent class. There's no such thing as 'shared' in C#. A class can be declared static but that has nothing whatsoever to do with access. If you declare a class static then you must also declare all its members static and you cannot create an instance of that class. This is equivalent to a VB module.
Now, you can mix and match access levels. For instance, you may have a public class with a public constructor to which you pass values that get assigned to private fields. Those fields might then get exposed via properties declared internal. That way you can create an instance and set the initial property values anywhere, but you can only access the properties within the same assembly. A little known fact is that you can also mix access levels within the same property. For instance, you might declare a property public but declare its setter internal, so within the same assembly the property is read/write but outside the assembly it is read-only.
If this doesn't answer your question then please try to rephrase it a bit more clearly.
BramVandenbon
Aug 14th, 2007, 09:17 PM
I have a class name "exportfilestatus" which currently has public scope.
I want to create an object of this class in some method and set the properties of this object.
If I don't want to return this object and if I still want to use the properties of the object, what modifier (public/shared/internal) should I use in the definition of "exportfilestatus" class?
I have to agree with jmcilhinney that your question is confusing.
If I understand you correctly then you want this class to be used inside another class which is part of the same project/assembly. But you don't want this class to be accessible from future projects/assemblies which will use your current project/assembly. Is that the case?
In that case, it's indeed a good idea to make it internal. It will still be accessible from within your project/assembly. But it will not be visible for other projects/assemblies.
Greetings!
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.