|
-
Jun 24th, 2003, 12:01 PM
#1
Thread Starter
Lively Member
Null, Nulls, Everywhere except my constructor
Ok, here is what I want to do, it is asp.net but that really shouldn't matter as my question is more on a class constructor.
I am trying to make my page load as slim as possible. So I have an Assembly(dll) that handles a lot of things for me. One such thing is the Cookie on user preferences.
which is easy enough to do.
My constructor is like this
public UserInfo(System.Web.HttpCookie UserCookie)
{
// code that reads the cookie info and populates the class
}
Anyway I can simply new this up and pass the cookie to it all in one line Like So
UserObject = new WebControls.CoreControls.UserInfo(Request.Cookies["UserPreference"]);
Ok so here is my problem
The Cookie doesn't exist on all visitors computers, if it doesn't exist you get a null reference execption.
Ok, So I know I can do a check to see if the cookie is null, if it is I call a different constructor, if it isn;t then I call the constructor you see above. What I want to know is how I can get rid of the if statement so that if it tries to pass a null it call another constructor.
public UserInfo(null UserCookie)
{
// this doesn't work since Null is not a valid datatype.
}
I am really just trying to get to a one line constructor. and one that can handle a null.
BTW Cookies are simply a collection object. so hopefully someone has dealt with passing a null to a constructor before.
The secret to creativity is knowing how to hide your sources.
-- Albert Einstein
-
Jun 24th, 2003, 12:10 PM
#2
That's what overloading is for 
Code:
// -- Your webform.
UserInfo myObject;
if (Request.Cookies["UserPreference"] == null) {
myObject = new WebControls.CoreControls.UserInfo();
} else {
myObject = new WebControls.CoreControls.UserInfo(Request.Cookies["UserPreference"]);
}
// -- Your class.
public UserInfo() {
// Code.
}
public UserInfo(System.Web.HttpCookie UserCookie) {
// Code.
}
-
Jun 24th, 2003, 12:24 PM
#3
Thread Starter
Lively Member
Thanks, Axion but that is what I am currently doing I am wondering if there is anyway to do it without the if null check, like overloading the constructor to take a null type. I don't think it can but done but I am hoping so.
The secret to creativity is knowing how to hide your sources.
-- Albert Einstein
-
Jun 24th, 2003, 01:42 PM
#4
You'll have to use an if block anyway :shrug:
Code:
UserInfo myObject = new WebControls.CoreControls.UserInfo(Request.Cookies["UserPreference"]);
// -- Your class.
public UserInfo(Object UserCookie) {
// Code.
if (UserCookie == null) {
// No such luck.
} else {
// Cookie exists.
UserCookie = (WebControls.CoreControls.UserInfo) UserCookie;
}
}
-
Jun 24th, 2003, 03:37 PM
#5
Thread Starter
Lively Member
Thats what I was looking for, thanks.
I do not mind using a if block inside the class itself, I just didn't want to have to do it in the web form. Other programmers (coops and consultants) will be working with the web form so I am trying to take as much possable problems out of the equation for them and just let them reference the assembly. Cause I know one of them would be messing with the if statment then bugging me, why doesn't this work.
The secret to creativity is knowing how to hide your sources.
-- Albert Einstein
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|