473,808 Members | 2,775 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Re: Trick needed - passing a large number of parameters to a class

On Jul 21, 1:27*pm, eran <kale...@gmail. comwrote:
- Use a params struct
... that has a constructor to set all the members.
What bothers me is this: I'd really like to minimize the chance that
the caller will miss setting one of the params.
That problem is solved... :)

Ali
Jul 21 '08 #1
3 1899
On Jul 21, 11:58*pm, acehr...@gmail. com wrote:
On Jul 21, 1:27*pm, eran <kale...@gmail. comwrote:
- Use a params struct

... that has a constructor to set all the members.
But then, values will have to be copied into the struct and then again
into the class.

I'd recommend using getters/setters the way they are implemented in
FLTK, for example, using parameter-based method overriding:

instance.varNam e (); /* returns the value */
instance.varNam e (value); /* sets the value */

As for checking if all the parameters are set, you should set default
values in the constructor, and before beginning to use the parameters
check if they have been set by the class's user.

Hope that helps
Jul 21 '08 #2
On Jul 21, 2:31*pm, khalid...@gmail .com wrote:
On Jul 21, 11:58*pm, acehr...@gmail. com wrote:
On Jul 21, 1:27*pm, eran <kale...@gmail. comwrote:
- Use a params struct
... that has a constructor to set all the members.

But then, values will have to be copied into the struct and then again
into the class.
Not "have to" for two reasons:

- struct members may be references to existing objects

- the compiler may optimize away the copy
I'd recommend using getters/setters the way they are implemented in
FLTK, for example, using parameter-based method overriding:

instance.varNam e (); /* returns the value */
instance.varNam e (value); /* sets the value */
[This is not related to this topic, and completely subjective, but I
strongly recommend against using the same name for getters and
setters. It looks good on paper but very confusing.]

Now, what happened to the concern of copying? Wouldn't the setter copy
the value?
As for checking if all the parameters are set, you should set default
values in the constructor, and before beginning to use the parameters
check if they have been set by the class's user.
That method would first default construct and then assign. Since
assignment involves destructing and copying, I don't think you are
really concerned about the cost of the method I proposed. Maybe I
misunderstood you. Why did you object using a constructor to set the
parameters?

Additionally, you method requires that every member must be
assignable, which not every type are.
Hope that helps
Sure, but that is inferior to using a constructor. Besides, your
method does not address the OP's requirement at all: eran said "I'd
really like to minimize the chance that the caller will miss setting
one of the params."

Ali
Jul 21 '08 #3
kh*******@gmail .com wrote:
On Jul 21, 11:58 pm, acehr...@gmail. com wrote:
>On Jul 21, 1:27 pm, eran <kale...@gmail. comwrote:
>>- Use a params struct
... that has a constructor to set all the members.

But then, values will have to be copied into the struct and then again
into the class.
Who copy? The class could work directly on the data.
I'd recommend using getters/setters the way they are implemented in
FLTK, for example, using parameter-based method overriding:
Which is always the sign of a bad design. How do you make sure they are
all called or which ones upset the state of the class?

--
Ian Collins.
Jul 22 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
1917
by: Mac | last post by:
Is there a way to mimic the behaviour of C/C++'s preprocessor for macros? The problem: a lot of code like this: def foo(): # .... do some stuff if debug: emit_dbg_obj(DbgObjFoo(a,b,c)) # .... do more stuff if debug:
3
14962
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
7
2872
by: Harolds | last post by:
The code below worked in VS 2003 & dotnet framework 1.1 but now in VS 2005 the pmID is evaluated to "" instead of what the value is set to: .... xmlItems.Document = pmXML // Add the pmID parameter to the XSLT stylesheet XsltArgumentList xsltArgList = new XsltArgumentList(); xsltArgList.AddParam("pmID", "", pmID); xmlItems.TransformArgumentList = xsltArgList;
1
3149
by: Darsin | last post by:
Hi all, I am a new programmer to C# and i am having a following problem. I want to make a single method which takes a variable length array and display it contents. i have defined the method as: public void DisplayVals(params object objArray) { foreach (object o in objArray) Console.WriteLine(o.ToString()); }
8
1411
by: Brett Romero | last post by:
I'd like to know when exactly the this. keyword is needed. For example: public class Combination { private long n = 0; private long k = 0; private long data = null; public Combination(long n, long k)
2
1482
by: Colin McGuire | last post by:
Hi everyone. This is a question and something new and neat I have found. But to recap - a few months back I learnt about overloading, how to create the same method with a different signature. With some help from people in this newsgroup I created a new class with overloaded constructors that accepted one or two parameters. The code is below. Private Class test Public Sub New(ByVal param1 As Integer)
7
9572
by: Richard Grant | last post by:
Hi. In c/C++ i can pass the address of a subroutine to another subroutine as an actual parameter How do I do that in VB .NET What should be the syntax for a parameter to receive the address of a subroutine Let's say theres a sub that creates buttons and I want it to receive as a parameter the address of the sub that handles the OnClick event for the button being created How do I pass such a parameter Thanks in advance Richar
11
4148
by: Macca | last post by:
Hi, I'm writing an application that will pass a large amount of data between classes/functions. In C++ it was more efficient to send a pointer to the object, e.g structure rather than passing the actual structure itself. Is this true of C# also?
4
4015
by: MicroMoth | last post by:
Hi, I'm trying to write a update method, in which when the user clicks the update button the update method is passed 10 form fields. Then a update SQL is run to update the database. My question is whats the best way to pass large numbers of parameters into a method. Ten seems a large number to be passing into and out of a method. Stephen
0
902
by: eran | last post by:
Thank you all for answering. I'm afraid relying on setters does not solve the problem of omitting the init of one of the params. Using default values and validation might help, but not when using types like bool, in which true and false are valid, and there could be no default value. Also, this approach does the validation at run time. If possible, I'd like check to be done at compile time. After reading your suggestions and giving it...
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9600
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10631
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10374
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10114
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9196
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7651
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6880
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
3
3011
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.