views:

122

answers:

5

Can you give advice if this is right or wrong?

public class DAL 
{
    Members member;

    public DAL(){ }

    public DAL(Members mmber)
    {
        member = mmber;
    }
}
A: 

This is fine, as long as you aren't going to try to call a method on members when it is null (with the first constructor).

Without more context it is difficult to say anything else. I can only assume that you have a reason to provide a public, no-arg constructor, and that the member field can be set by some other mechanism.

Jay
Members is a Class object only. I use this to pass a object as a parameter in my query. is this right
lord
+1  A: 

You're on the right track -- there's nothing really wrong with it. Consider giving your "Members" variable name something that helps identify it as a member field in your function, such as m_members. That way you'll be able to use variable names like member or members in the methods in this class.

Warren
m_ is a terrible convention and if it improves readability the code has worse problems
Josh Smeaton
Warren
Apologies how that came off sounding. In my defense, I wrote that from my mobile or I probably would have prefixed it and dialed down the tone. The people at my work preceding me left me with prefixes such as UFSFunctionName. "User Function returns String FunctionName". I'm now adverse to ALL prefixes! But yes, this is definitely in my opinion.
Josh Smeaton
+1  A: 

I would change the overloaded constructor to this:

public DAL(Members member)
{
    this.member = member;
}

The this in the constructor allows you to use the member field in the class and this way you don't have to rename your parameters to something that doesn't look right.

Waleed Al-Balooshi
+1  A: 

Yeah, it's fine.

I would add private to the attribute and remove the no arg constructor:

public class DAL 
{
     private Members member;

     // public DAL(){ } forces DAL always have a member value

    public DAL(Members member)
    {
        this.member = member;
    }
}

But I wouldn't say it is "wrong" if you don't do it.

OscarRyz
A: 

One tip - you should chain your constructors together:

public DAL()
{
}

public DAL(Members mmber)
    : this()
{
    member = mmber;
}

No impact on the current code, but chaining is important when you get some more logic in your constructors.

Bevan