views:

1544

answers:

11

Hi,

A while back I asked about instantiating a HttpContext object. Now that I have learnt what I didn't know, what confuses me is that you cannot say HttpContext ctx = new HttpContext(); because the object does not have a constructor.

But doesn't every class need a constructor? In C#, if you don't provide one, the compiler automatically provides a default cstr for you.

Also, if I have a string (example: "Hello There!") and I say Convert.ToBoolean("Hello"), or any string, how does this work? What happens behind the scenes? I guess a book like CLR Via C# would be handy in this case.

What am I missing?

+18  A: 

Constructor can be private or protected.
Also you can't create instance of abstract class, even if that class has public constructor.

Michał Piaskowski
Or internal; or protected internal
Marc Gravell
+1  A: 

I believe the HttpContext constructor has been marked private. That means that you cannot instantiate it yourself. The .net framework creates one for you behind the scenes...

casademora
Actually, there are 2 public ctors for HttpContext
Marc Gravell
sure - they take SimpleWorkerRequest or HttpRequest, HttpResponse
JohnIdol
+1  A: 

In one word : static.

Otherwise, a class might be instantiated internally or privately (Factory or Singleton)

Signleton :

Class A{
public static readonly A Instance = new A();

private A()
{
}

}
A: 

This was actually meant to be a C# question, I pressed C# by accident and can't edit the tags now. :(

Blade
These additional posts are considered answers. This is not a forum. If you have additional information I would suggest editing your original question. Voted down.
Jason Jackson
A: 

If you make the constructor private, you cannot extarnaly instatiate a class. But within the class it is possible. So you can provide a static method that returns a instance of the class. The singleton pattern is based on this.

Ikke
+3  A: 

Singletons, for example, do not have constructors, or at least, no public constructors. So if your class is a singleton, instead of writing

MyClass c = new MyClass();

You would write instead

MyClass c = MyClass.getInstance();
Elie
singletons constructors should be made private
DanJ
Singletons do have constructors, but it should only be called once.
Steve g
not only should they be made private, they have to be made private or they no longer are guaranteed to function properly as singletons
Elie
A: 

Thanks for the heads up guys.

Blade
These additional posts are considered answers. This is not a forum. If you have additional information I would suggest editing your original question. Voted down.
Jason Jackson
A: 

MyClass c = MyClass.getInstance();

What would the getInstance method look like?

Blade
public static MyClass getInstance(){MyClass.instance = new MyClass(); //calling private constructor inside MyClass;return instance;}
Michał Piaskowski
don't forget the check to see if it has been instantiated yet:public static MyClass getInstance() { if (MyClass.instance == null) MyClass.instance = new MyClass(); return instance; }
Elie
These additional posts are considered answers. This is not a forum. If you have additional information I would suggest editing your original question. Voted down.
Jason Jackson
+1  A: 

Take a look at the Singleton Design Pattern.

Leandro López
+4  A: 

HttpContext has a public constructor with two overloads but it's not the default (no params) one.

As an example, you need to pass in a SimpleWorkerRequest instance in order to instatiate an HttpContext instance and assign it to HttpContext.Current:

//Initialize this stuff with some crap
string appVirtualDir = "/"; 
string appPhysicalDir = @"C:\Documents and Settings\"; 
string page = @"localhost"; 
string query = string.Empty; 
TextWriter output = null;    
//Create a SimpleWorkerRequest object passing down the crap
SimpleWorkerRequest workerRequest = new SimpleWorkerRequest(appVirtualDir, appPhysicalDir, page, query, output);
//Create your fake HttpContext instance 
HttpContext.Current = new HttpContext(workerRequest);

See this link for details.

Anyway some classes don't have public constructors - think of a singleton class for example, constructor is private (and you can call the static getInstance method to get current instance or create it if it is null).

Cheers

JohnIdol
+2  A: 

You have 3 questions there...

HttpContext; it actually has two public constructors - but in reality you aren't expected to use them. In more general terms, you can use non-default constructors like so: MyType foo = new MyType("abc");.

Missing constructor Fairly well covered already, but no: abstract / static are the simplest, but it also isn't necessary to have a public constructor.

ToBoolean Behing the scenes, this will do the moral equivalent of bool.Parse("Hello"), which simply checks for known strings - in particular "True" and "False" (using OrdinalIgnoreCase, having dealt with null/trimming/etc).

Marc Gravell