- Loading...
- No images or files uploaded yet.
|
|
April-NETinDepth.NET in depth
.NET in Depth March April May June July August September October November December
Following up from last night's discussion on casting, here's an example of using "as" for safe casting. Note also that this is a case where the compiler could not catch an incorrect cast using "(type)object" syntax, since we're casting from "object" to "System.Web.UI.WebControls.Textbox". Casting from object to a specific class occurs in some very common situations in .NET.
The task here involves handling a TextChanged event for a textbox that has been dynamically added to a web page. The complications here are twitchy, since we don't have a compiled TextBox object to work with by name, and we might even have several TextBox controls that have been added dynamically, and they're all using this same TextChanged event handler to retrieve results. We have to assume there are multiple TextBox controls and that we have to accumulate the values of all of them.
void _TextBox_TextChanged(object sender, EventArgs e) { if (m_ParameterValues == null) { m_ParameterValues = new Dictionary<string, string>(); } TextBox _TextBox = sender as TextBox; // <-- safe cast using "as" if (_TextBox == null) { return; } string _ParamName = _TextBox.ID.Replace("txtID", ""); if (!string.IsNullOrEmpty(_ParamName)) { m_ParameterValues.Add(_ParamName, _TextBox.Text); } } Diane ------------------------------------------------------------------------------------------------ Here's another fun example. Please excuse the ugly stuff with reflection.... We talked about overloading methods, and that the method signature depends on parameter number and type, but doesn't consider the return type as part of the signature. I mentioned that you can use generics as a way around this problem, so here's a working example. First, the code to call the method:
private Control CreateChart(object value) { // pull data from the object, based on field descriptions from the chart // configuration file. Single[] _xData; Single[] _yData; ushort? _count; _xData = GetPropertyByReflection<Single[]>(m_ImageInfo.Elements["X-axis"].Name, value); _yData = GetPropertyByReflection<Single[]>(m_ImageInfo.Elements["Y-axis"].Name, value); _count = GetPropertyByReflection<ushort?>(m_ImageInfo.Elements["count"].Name, value); ....
Now a method that uses generics for a return type:
private T GetPropertyByReflection<T>(string _name, object _obj) { Type _objType = _obj.GetType(); try { PropertyInfo _dataField = _objType.GetProperty(_name); return (T)_dataField.GetValue(_obj, null); } catch (Exception ex) { throw new Exception(string.Format("Failed to retrieve data for {0}.{1}: {2}", _objType.ToString(), _name, ex.Message)); } } Notes:
Diane ---------------------------------------------------------------------------------------------------- One more example, since we talked about constructor chaining. This one is pretty simple; the second constructor calls the first, and the first constructor calls the base constructor.
public CenticeEventUnhandledException(string message, object sender, Exception ex) : base(message, sender, CenticeWebEvents.UnhandledExceptionError) { m_Exception = ex; m_AdditionalData = true; }
public CenticeEventUnhandledException(string message, object sender, Exception ex, Dictionary<string, string> MoreInfo) : this(message, sender, ex) { m_MoreInfo = MoreInfo; } Diane ------------------------------------------------------------------------------------------------------
|
Comments (0)
You don't have permission to comment on this page.