Pass values in properties, or in parameters?

  • Thread starter Thread starter Larry Bud
  • Start date Start date
L

Larry Bud

I'm writing a class to create a specifically formatted fixed width
file. It's 800 characters wide, consisting of approx 30 fields.

So I need to pass 30 variables, maybe 10 are required. Should I write
the function to accept these as parameters in a method, or should I
make them properties? Can I make certain properties required?
 
Hi Larry,

Having Properties for all those parameters which go to and fro is possibly
the best solution.
Moreover, it is not tedious enough to handle them as properties and at the
same time
comes in very handy for most of your functionalities

Kuldeep
 
Hi Larry,

It depents on how you store the value there and format of file. Anyway, setting
the params by properties is clear solution. If object is flexible you can
combine these too methods - props to accept neccessary params and method
for aditional params, but it depends on format.

Regards, Alex
[TechBlog] http://devkids.blogspot.com



LB> I'm writing a class to create a specifically formatted fixed width
LB> file. It's 800 characters wide, consisting of approx 30 fields.
LB>
LB> So I need to pass 30 variables, maybe 10 are required. Should I
LB> write the function to accept these as parameters in a method, or
LB> should I make them properties? Can I make certain properties
LB> required?
LB>
 
There is a third solution.

Write a wrapper object, containing all the parameters.


public class EmployeeController

public static void UpdateEmployee ( EmployeeArgs arg )
{

}


public class (or struct) EmployeeArgs
{
public Guid EmployeeUUID (property here)
public string LastName
public string FirstName
public DateTime CreateDate
public DateTIme HireDate

}





EmployeeArgs myArg = new EmployeeArgs ( ) ;
myArg.EmployeeUUID = Guid.NewGuid();
myArg.LastName = "Smith";
myArg.FirstName = "John";
myArg.CreateDate = DateTime.Now;


EmployeeController.UpdateEmployee ( myArg);


With 30 (and some optional) parameters, I'd write the wrapper arg object.

You'll notice I didnt' specify the HireDate, aka, it is optional. Your
controller class can determine what to do in an omitted HireDate.

ALSO.

If you have some MANDATORY properties, then you can expose the constructor
to the wrapper arg.


public class EmployeeArgs
{
//no default constructor
public EmployeeArg ( string lname, string fname)
{
this.LastName = lname;
this.FirstName = fname;
}

public Guid EmployeeUUID (property here)
public string LastName
public string FirstName
public DateTime CreateDate
public DateTIme HireDate

}


This way, you're forcing lname and fname.
 
There is a third solution.

Write a wrapper object, containing all the parameters.

public class EmployeeController

public static void UpdateEmployee ( EmployeeArgs arg )
{

}

public class (or struct) EmployeeArgs
{
public Guid EmployeeUUID (property here)
public string LastName
public string FirstName
public DateTime CreateDate
public DateTIme HireDate

}

EmployeeArgs myArg = new EmployeeArgs ( ) ;
myArg.EmployeeUUID = Guid.NewGuid();
myArg.LastName = "Smith";
myArg.FirstName = "John";
myArg.CreateDate = DateTime.Now;

EmployeeController.UpdateEmployee ( myArg);

With 30 (and some optional) parameters, I'd write the wrapper arg object.

You'll notice I didnt' specify the HireDate, aka, it is optional. Your
controller class can determine what to do in an omitted HireDate.

ALSO.

If you have some MANDATORY properties, then you can expose the constructor
to the wrapper arg.

public class EmployeeArgs
{
//no default constructor
public EmployeeArg ( string lname, string fname)
{
this.LastName = lname;
this.FirstName = fname;

}

public Guid EmployeeUUID (property here)
public string LastName
public string FirstName
public DateTime CreateDate
public DateTIme HireDate

}

This way, you're forcing lname and fname.







- Show quoted text -

Thanks to all. This is a pretty elegant solution. Turns out there's
*51* fields. This beats writing 51 Set Property statements
 
And when you need a new (extra) property, you don't change the signatures
anywhere.


Today:

public class (or struct) EmployeeArgs
{
public Guid EmployeeUUID (property here)
public string LastName
public string FirstName
public DateTime CreateDate
public DateTIme HireDate

}

1 Year from Now:


public class (or struct) EmployeeArgs
{
public Guid EmployeeUUID (property here)
public string LastName
public string FirstName
public DateTime CreateDate
public DateTIme HireDate

public List <Guid> JobFunctionUUIDs
public Guid CubicleLocationUUID

}


While your controller will handle the new values, you don't have change the
method calls.

EmployeeController.UpdateEmployee ( myArg );

will always be

EmployeeController.UpdateEmployee ( myArg) ;

forever more.
 
If you have some MANDATORY properties, then you can expose the constructor
to the wrapper arg.

public class EmployeeArgs
{
//no default constructor
public EmployeeArg ( string lname, string fname)
{
this.LastName = lname;
this.FirstName = fname;

}

public Guid EmployeeUUID (property here)
public string LastName
public string FirstName
public DateTime CreateDate
public DateTIme HireDate

}

This way, you're forcing lname and fname.

I'm not understanding how this forces lname and fname. Shouldn't
EmployeeArg be "New"??
 
//hide the default constructor
private EmployeeArg ( /* no default constructor */ ) {}


public EmployeeArg ( string lname, string fname)
{
this.LastName = lname;
this.FirstName = fname;

}



The syntax above IS THE CONSTRUCTOR in C#. (2 constuctors, 1 public , 1
private)


Aka, you can only do this:

EmployeeArg arg = new EmployeeArg ( "Jones" , "Mary") ;

and you can't do this:

EmployeeArg arg = new EmployeeArg (); // because the default constructor is
private, thus you can't get to it.


basically here you are saying that you MUST provide a lastname and
firstname, else you can't construct the object.
aka, this is good for mandatory values.
 
//hide the default constructor
private EmployeeArg ( /* no default constructor */ ) {}

public EmployeeArg ( string lname, string fname)
{
this.LastName = lname;
this.FirstName = fname;

}

The syntax above IS THE CONSTRUCTOR in C#. (2 constuctors, 1 public , 1
private)

Aka, you can only do this:

EmployeeArg arg = new EmployeeArg ( "Jones" , "Mary") ;

and you can't do this:

EmployeeArg arg = new EmployeeArg (); // because the default constructor is
private, thus you can't get to it.

basically here you are saying that you MUST provide a lastname and
firstname, else you can't construct the object.
aka, this is good for mandatory values.









- Show quoted text -

Ok, what was confusing me is that you wrote EmployeeArg in one place,
and EmployeeArgs in another place.

Thx!
 
Back
Top