help designing a method, should I use out or ref or return the type?

Posted by Blankman on Stack Overflow See other posts from Stack Overflow or by Blankman
Published on 2010-04-08T15:56:57Z Indexed on 2010/04/08 16:03 UTC
Read the original article Hit count: 242

Filed under:
|

I have a method that I will use in the following contexts:

1.

User user = null;

if(...)
{
      user = defaultUser;
      SetUser(a,b,user);
}
else
{
      SetUser(a,b,user);
}

SaveUser(user);

So some cases are where user may be null, while in other cases it will already be initialized.

How should I design the SetUser method?

I currently have it like so, but this causes an error when user is null.

public void SetUser(object a, object b, User user)
{
     if(user == null)
         user = new User();

     user.Security = a.security;
     user.Blah = b.type;
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET