-
July 13th, 2008, 09:31 PM
#1
Parameter Arrays and a few other questions...
Hey.. first post here, but I've been reading a lot and I'm not quite grasping the use of parameter arrays. I mean I understand the core concept, being able to pass an unknown amount of parameters but I suppose what perked my interests was in the book I'm reading, the author(s) mention using parameter arrays for "optional" arguments. I'm not sure if I'm explaining this clearly, so let me give the example the author gave:
This final point makes parameter arrays particularly useful for specifying additional information for
functions to use in their processing. For example, suppose you have a function called GetWord() that
takes a string value as its first parameter and returns the first word in the string:
string firstWord = GetWord(“This is a sentence.”);
Here, firstWord will be assigned the string This .
You might add a params parameter to GetWord() , enabling you to optionally select an alternative word
to return by its index:
string firstWord = GetWord(“This is a sentence.”, 2);
How would this be done in the function it calls? Do you use if statements to test for a second variable and is it indexed just like a normal array? I.E.
Code:
static string GetWord(string[] myString, params int[] vals)
{
char[] seperator = (' ');
string[] myWords;
myWords = myString.Split(seperator);
if (vals == null)
return myWords[0];
else
return myWords[vals[0]];
}
*edited the code. Eureka and such, but still have a question. See below*
Is this correct? Obviously I could just use an int without the params, but, it would also allow me to check for vals[1] to possibly return, say, only a certain number of characters in word vals[0]. I would just keep testing for every possible parameter I would want the function to take? Why not just specify ...(string[] myString, int index, int numberOfCharacters)? Why use params at all??
Also.. I'm kind of stuck on the differences between structs and functions. I've read on the forums how structs are a value type and classes are a reference type, but what exactly does this mean? I suppose I just don't see the need for structs in OOP (and I may be way off and misunderstanding structs completely.. classes I understand).
Thanks for the help!
Last edited by dizuane; July 13th, 2008 at 09:41 PM.
-
July 13th, 2008, 09:37 PM
#2
Re: Parameter Arrays and a few other questions...
I would NEVER use that approach. An overloaded method is much more defined.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
July 13th, 2008, 09:42 PM
#3
Re: Parameter Arrays and a few other questions...
An overloaded method? Can you give an example?
-
July 14th, 2008, 01:52 AM
#4
Re: Parameter Arrays and a few other questions...
I mean that TheCPUWizard has thought something like this:
Code:
static string GetWord(string myString)
{
return GetWord(myStrings, 0);
}
static string GetWord(string myString, int wordIndex)
{
char[] seperator = (' ');
string[] myWords = myString.Split(seperator);
if (wordIndex > myWords.Length - 1) throw new ArgumentOutOfRangeException("wordIndex");
return myWords(wordIndex);
}
static string GetWord(string myString, int wordIndex, int chars)
{
string word = GetWord(myString, wordIndex);
return word.Substring(0, chars);
}
I've also change your string[] myString to string myString (just string, not array of strings), because it seems that it is what you need. Array class haven't Split() method.
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|