How to hide method so you can't access?

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

HOVEAsked:
Who is Participating?
 
RishadanPortCommented:
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
 
RishadanPortCommented:
declare the method as private.
0
 
RishadanPortCommented:
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
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
HOVEAuthor Commented:
Declaring method as private does not solve the problem, base Insert is still accessible.
0
 
RishadanPortCommented:

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
 
RishadanPortCommented:
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
 
HOVEAuthor Commented:
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
 
RishadanPortCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.