Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / August 2007

Tip: Looking for answers? Try searching our database.

Pass values in properties, or in parameters?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Larry Bud - 31 Aug 2007 14:49 GMT
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?
Kuldeep - 31 Aug 2007 15:07 GMT
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

> 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?
Alex Meleta - 31 Aug 2007 15:12 GMT
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>
sloan - 31 Aug 2007 16:30 GMT
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.

> 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?
Larry Bud - 31 Aug 2007 16:52 GMT
> There is a third solution.
>
[quoted text clipped - 63 lines]
>
> - 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
sloan - 31 Aug 2007 17:22 GMT
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.

>> There is a third solution.
>>
[quoted text clipped - 68 lines]
> Thanks to all.  This is a pretty elegant solution.  Turns out there's
> *51* fields.  This beats writing 51 Set Property statements
Larry Bud - 31 Aug 2007 17:49 GMT
> If you have some MANDATORY properties, then you can expose the constructor
> to the wrapper arg.
[quoted text clipped - 18 lines]
>
> This way, you're forcing lname and fname.

I'm not understanding how this forces lname and fname.  Shouldn't
EmployeeArg be "New"??
sloan - 31 Aug 2007 20:32 GMT
//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.

>> If you have some MANDATORY properties, then you can expose the
>> constructor
[quoted text clipped - 22 lines]
> I'm not understanding how this forces lname and fname.  Shouldn't
> EmployeeArg be "New"??
Larry Bud - 31 Aug 2007 20:55 GMT
> //hide the default constructor
> private EmployeeArg ( /* no default constructor */ ) {}
[quoted text clipped - 50 lines]
>
> - Show quoted text -

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

Thx!
sloan - 31 Aug 2007 21:51 GMT
Oh yeah, now I see that.  Sorry.

I was just typing away from pseudo code ............

>> //hide the default constructor
>> private EmployeeArg ( /* no default constructor */ ) {}
[quoted text clipped - 56 lines]
>
> Thx!

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.