?
Solved

How to hide method so you can't access?

Posted on 2008-11-04
8
Medium Priority
?
881 Views
Last Modified: 2008-11-04
I want to hide a method so that it is not accessible from an outside of the class.
public class MyList : List<object>
{
	//optional method
	public new void Insert( int index, object item )
	{
	}
}
 
public class MyClass
{
	public MyClass()
	{
		MyList ml = new MyList();
 
		//I want Insert to be hidden, so I can't access it
		//ml.Insert();
	}
}

Open in new window

0
Comment
Question by:HOVE
  • 6
  • 2
8 Comments
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22881325
declare the method as private.
0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22881357
If though you are overriding a method, the method must be declared as public.  To overcome this, you can put another wrapper class around the current class, and label the inner class as private

Example:
//Shell class has access to the MyList class. Doing this will hide the Insert method from non Shell objects
public class Shell{
   private class MyList : List<object>
   {
	   //optional method
	   public new void Insert( int index, object item )
	   {
   	   }
   }
}

Open in new window

0
 

Author Comment

by:HOVE
ID: 22881382
Declaring method as private does not solve the problem, base Insert is still accessible.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 6

Expert Comment

by:RishadanPort
ID: 22881386

public class Shell{
   private class MyList : List<object>
   {
      //define all hidden methods here
   }
 
   private MyList list;
 
   public Shell(){
      list = new MyList();
   }
 
   //Define public methods here that the user can use. Do not
   //show the MyList class
}

Open in new window

0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22881401
In the example above the Shell class will have access to the list variable, that is a MyList. However, when you initialize the Shell class, you cannot however use the Insert function, since the MyList class is hidden
0
 
LVL 6

Accepted Solution

by:
RishadanPort earned 1000 total points
ID: 22881478
You could actually do something like this to make it even better:


public class Shell{
   private class MyList : List<object>, IAllowableFunctions{
      //define functions here
   }
   
   public interface IAllowableFunctions{
      //define functions u want to be declared public here
      void Add();
 
      void Remove();
   }
 
   private MyList list;
 
   public Shell(){
      list = new MyList();
   }
 
   //returns back a IAllowableFunctions interface that only
   //has access to the listed functions
   public IAllowableFunctions List{
      get{ return list; }
   }
}

Open in new window

0
 

Author Comment

by:HOVE
ID: 22881685
Is there no other way, maybe with an attribute, to hide a method? Now I need to call Shell.List to access methods, otherwise I need to specify all methods in Shell class if I what them to bee accessed directly.
0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22882037
That's a good question. I have come across this type of thing, but I don't no of another way around it. I will put something up if I figure something out
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month15 days, 6 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question