|
-
February 9th, 2012, 07:24 AM
#6
Re: Exposing Array parameters in User Control
Oh, I totally missed that!
It's simple: just delete the [] in the get & set blocks. flavor is the name of a variable, and it's used the same way as any other variable - the fact that it's an array doesn't change anything.
So:
Code:
get { return flavor; }
set { flavor = value; }
I'll try to make things somewhat more clear. The flavor array is the backing field of the Flavor property. That is, the flavor array stores the actual data, and the Flavor property is just the way to get it from outside the class, without exposing internal implementation details. To beginners this often seems strange, as it appears that such a property doesn't do anything really useful, and that it only adds extra code in between. However, this is one of the most well known programming practices, and it has several benefits - it abstracts away implementation details, so that you can change them without affecting the rest of the program using your class; it enables you to insert various error checking or data transformation logic into the getter and/or setter; it's also a debugging aid, as it provides a centralized point for the get/set operations on the internal data, which enables you to quickly identify any such operation by simply setting a breakpoint on the property.
So, you see, the backing field doesn't have to have a 1-to-1 mapping to a property, or the same name, and the property can have additional logic, aside from just the basic retrieve/assign functionality. The point is: the backing field is just some variable that stores data, and the property is just a way for external objects to manipulate that data.
Same goes for arrays. The only difference is that they support the [] operator, which is just a syntactic sugar: flavor[i] is just a way for you to say "get me the i-th element of the flavor array".
Now, there might be some confusion because the [] symbol is used in various context with slightly different meanings.
Code:
string[] myStringArray; // declares a string array (string[]) variable, initialized to null.
myStringArray = new string[10]; // initializes the array to an actual string array with 10 elements
// or, in condensed form:
string[] myStringArray = new string[10];
// (Note: The array is now not null anymore, but the
// elements are - null is the default value for reference types.
// Each element should be added later on in code.)
So, here, the [] symbols are used to denote that you want an array, as opposed to just a string, and then again to inform the compiler how big the array should be.
Note that in this case the [] is never written with the variable name (myStringArray), but always as a part of the type name.
Other than that, when working with the whole array, and not just the elements, you would use the variable as any other:
Code:
myStringArray = someOtherStringArray;
// or in a method which returns a string array (string[])
public string[] MyMethod()
{
// do some processing...
return myStringArray;
}
// NOTE: same goes for properties, as they are just methods in disguise...
If you wanted to access and manipulate individual elements however, then you would use the [] operator as an indexer, on your variable.
Code:
myStringArray[2] = "third element";
myStringArray[i] = i.ToString() + "-th element";
With all that in mind, the compiler errors you got are pretty straightforward:
> 'In the get part, an error on the last square bracket is: "Syntax error; value expected"'
Compiler interprets "flavor[]" as a type name, but a type name cannot be used there - a variable name is expected (just "flavor").
> 'and in the set part, flavor has an error: "...is a 'field', but used like a 'type'"',
Along the same lines.
The rest of the errors are related to these two, and will disappear when these are fixed.
> 'the = sign: "Identifier expected"'
The compiler thinks "flavor[]" is a start of a variable declaration, so it expects the variable name before the "=", but none is there.
> 'and value has:"cannot convert string[] to flavor[]".'
Again, the compiler thinks "flavor[]" is a type, and that you're trying to assign the value to some variable you forgot to name, and as but the type of the property is string[] and not flavor[], the compiler tells you it cannot be done.
Hope that clears things up a bit for you.
P.S. You also should check out this quick guide.
Last edited by TheGreatCthulhu; February 9th, 2012 at 07:33 AM.
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
|