| |
A Method for Getting Form Variables | Calling the Method |
So let's step through this. On line 37 I'm creating my string variable
name and setting it equal to the result of
getVariable("name"). The
getVariable() part should look familiar: I'm calling a method. The part that reads
"name" passes "name" into the method.
On line 68 I've defined the method
private string getVariable(string input). It's a private method, it returns a string, it's called getVariable, and it takes a string called
input. Remember the part above that reads
getVariable("name")? The "name" part is automatically assigned to the string
input. This would be as if I'd written
string input = "name"; within the method.
From there I create a string called
variable. I then look for the form variable. However, rather than have
Request.Form.Get("name") or
Request.Form.Get("comment") I have
Request.Form.Get(input). If you remember, we set
input via our method call above (
getVariable("name")) so it equals "name". Thus, it's the equivalent of writing
Request.Form.Get("name"). We get the value of the form variable "name" and then return it.
On line 38 we do the same thing with
comment. Since we passed "comment" into the method, we're now looking for a form variable like
Request.Form.Get("comment"). What we've done is we've taken some repetitive code and pulled it out into its own method, making it easy to use and quick to write.
You may want to play with this on your own some before continuing. I realize this can be confusing at first: It took me a really long time to finally figure out objects after I'd first been introduced to them. However, after doing it a few times it should start to come naturally. Next, let's start on the database.