Monday, March 26, 2012

PLEASE help - simple problem

The problem I am having is actually easier to explain if you look at my code, so I'll start there (note: this is coded in whidbey - but I thought this question would be more appropriate here than the 2.0 forum)

<cod>
Function GetValue(ByVal field As String, ByVal type As String) As String

Dim value As String = ""

If (type = "Text") Then
value = CType(MyFormView.FindControl("HomeWidthText"), TextBox).Text.ToString
'value = CType(MyFormView.FindControl(field), TextBox).Text.ToString
End If
...
Return value
End Function

Sub validateAllFields()
If (GetValue("HomeWidthText", "Text") = "") Then
...
End If
...
End Sub
</code
So, what's not working about that code you might ask? Well, absolutely nothing if you run the code as is. Now, replace the green with the red and you get the following error:
"Object reference not set to an instance of an object."

What? That doesn't make any sense at all to me as field is set to a string and is exactly that - "HomeWidthText". I just don't get it.

Aaroninstead of:
Function GetValue(ByVal field As String,

make field a control, in the function instead of a string
That would makes sense but that ignores the whole intention of that function...to reduce code!

You see, I didn't want to consistently have to write the following...


Dim homeWidth As String = GetValue(CType(MyForm.FindControl("HomeWidthText"), Control), "Text")

There are literally hundreds of these and I want to be able to do something like

Dim homeWidth As String = GetValue("HomeWidthText", "Text")

And, it should take the "Text" parameter and determine that it is dealing with a text field and do that appropriately.

Can this not be done in ASP.Net 2.0?

Aaron

0 comments:

Post a Comment