Passing DateTime

G

Greg

I have a method that accepts some DateTime types. For example:

UserDetails(int userID, string userName, datetime hireDate)
{
this.UserID = userID;
this.UserName = userName;
this.HireDate = hireDate;
}

Now, I'm creating a UserDetails Object by passing the properties as.

UserDetails user;
user = new UserDetails(UserID.Text, UserName.Text, HireDate.Text)

Here's my problem.

When i start all of these fields may or may not contain null values. What I
do for the UserName.Text is check to see if it equals System.DBNull.Value and
if it does I set a temp variable to empty.string.

Now, I need to check the value of the HireDate. If the HireDate.Text equals
System.DBNull.Value then I set a DateTime variable to null. Now, this is the
problem i'm having. I can't set my temp dteHireDate variable to null or
actually empty. I can't send a null value to my method.

This type of situation is actually a very frustrating thing for me because I
have methods that I pass 20 values to and I don't know if the value in my
controls will be null or not. Thus, I must check for each variable to make
sure my method doesn't crash. So, I have two questions.

1. How can I set a DateTime variable to an empty string or some value that I
can pass to a method that isn't equal to null.
2. Is there some better way for me to handle the whole problem of passing
null values to a method. Right now, I'm setting the default value to most of
my fields in SQL Server to Space(1) or one space. This doesn't seem right to
me, but it's the only way I can get moving along without having errors all
the time. I'd prefer to save the NULL values instead.

Thanks.
 
S

sloan

DateTime.MinValue has kinda been the trick in the past.

//member variable.
private DateTime _hireDate = DateTime.MinValue;



You can make a nullable property.



.........

If you create a proper DataLayer, then you can have if logic in the
DataLayer.

Here my theUUID is a nullable value. However, you could just as easily
check for
if ( DateTime.MinValue == myDateTimeVariable )

if (theUUID .HasValue) // or if ( DateTime.MinValue !=
myDateTimeVariable )

{

db.AddInParameter(dbc, "@TheUUID" , System.Data.DbType.Guid, theUUID );

}

else

{

db.AddInParameter(dbc, "@TheUUID", System.Data.DbType.Guid, null);

}
 
P

proxyuser

Greg said:
Now, I need to check the value of the HireDate. If the HireDate.Text
equals
System.DBNull.Value then I set a DateTime variable to null. Now, this is
the
problem i'm having. I can't set my temp dteHireDate variable to null or
actually empty. I can't send a null value to my method.

This is what nullable was invented for. What version of .NET do you have?
e.gl.
DateTime? date = null;
 
I

Ignacio Machin ( .NET/ C# MVP )

I have a method that accepts some DateTime types. For example:

UserDetails(int userID, string userName, datetime hireDate)
{
this.UserID = userID;
this.UserName = userName;
this.HireDate = hireDate;

}

Now, I'm creating a UserDetails Object by passing the properties as.

UserDetails user;
user = new UserDetails(UserID.Text, UserName.Text, HireDate.Text)

Here's my problem.

When i start all of these fields may or may not contain null values. What I
do for the UserName.Text is check to see if it equals System.DBNull.Value and
if it does I set a temp variable to empty.string.

Don't you think that a USer should ALWAYS have a UserName? (or a
HireDate for that matter)

maybe you have an error in the way you create/handle instances.

Of course , you can always have the case where a null value is
permisible (like a TerminationDate). In this case you can use a
DateTime?. This is a nullable type.
This type of situation is actually a very frustrating thing for me because I
have methods that I pass 20 values to and I don't know if the value in my
controls will be null or not.

A method with 20 parameters is a ripe candidate for refactor, who are
you passing that many parameters????
 
G

Greg

I have a method called "SaveUser" which I pass an object called UserDetails();

UserDetails contains all of my Property Members which contains more than 20
members. When I create a UserDetails() object I pass it all of the Property
Members I've created. Then I have a UserDetails object that I can pass to the
"SaveUser(userDetails)" method.

This seems like a lot of work to save a record to me. Maybe I'm not
utilizing this approach properly.

I don't know anything about Refactor, so I'll have to take a look into this.
 
G

Greg

I just wanted to add one more thing. Your final statement about "What am I
passing this many paramters to?" made a light bulb go off in my head. You
made me realize I was approacing my issue in completely the wrong way.

Thanks for making me see that my approach was wrong. I should have much more
success now going forward.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top