Converting a Type to an object

  • Thread starter Christopher W. Douglas
  • Start date
C

Christopher W. Douglas

I am working on a project in VB.NET using Visual Studio.NET 2003. I use
dialog windows to get user information, and throw exceptions when there are
user errors, such as leaving a field blank or entering invalid information.
My exception handler reads the error message and handles it accordingly
based on the message.

My problem is, I am trying to find a way to avoid closing the dialog window
when it is a minor error. I can do this by setting DialogResult to None,
but the problem is in determining which form threw the exception. Using
Exception.TargetSite.DeclaringType I can get the Type of the window, and
thus get the Name. For example, if the class of the dialog window is
"frmFilter", the DeclaringType's Name is "frmFilter". I built a case
statement based on the name, but I would rather have a more dynamic answer.
What I really want to do is to somehow convert the Type object into the
actual Object. I know you can go the other direction with GetType, but I
can't seem to find in help how to go the other way.

I also saw the SetValue property, but as far as I can tell I need to have
the actual object (the dialog form) in order to make SetValue work. If I
HAD the actual object available, I could just set the property from the
object and be done with it.

Here is an example/pseudo-code:

I have a form class, frmFilter.

I have an object of frmFilter, called FilterForm.

There is an exception thrown from function cmdOK_Click in FilterForm.

In the exception handler, I determine that the exception does not warrant
closing the form, so I want to set FilterForm.DialogResult =
DialogResult.None.

I declare MyType as System.Reflection.Type

Exception.TargetSite gives me cmdOK_Click, which is the offending method.

Exception.TargetSite.DeclaringType gives me the Type "frmFilter", which I
assign to MyType

Now, I need to find a way to convert (?) MyType into the actual object,
FilterForm. Or find an easy way to set the property of MyType.DialogResult
to DialogResult.None. CType doesn't work, since MyType is a Type, which I
understand is similar to a Class, not the actual Object FilterForm.

I appreciate any help you guys can provide.
 
J

Jay B. Harlow [MVP - Outlook]

Christopher,
I would not throw exceptions for user errors. I would only throw exceptions
of programming (programmer) errors...

Within the Dialog I would use the Control.Validating & Control.Validated
event and either ErrorProvider or MessageBox objects to handle user errors.

Note a Domain object may throw an exception for an invalid property, however
the UI would/should not allow the invalid value to make it to the domain
object...

Rockford Lhotka's book "Expert One-on-One Visual Basic .NET Business
Objects" from A! Press provides a BrokenRules pattern for validation.
http://www.lhotka.net/

However I like the concept of David West's idea of Self-Evaluating Rule
more. Self-Evaluating Rule's are defined in David West's book "Object
Thinking" from MS Press.

To me: Broken Rules seem to be more passive, while Self-Evaluating Rules
seem more active, hence I like Self-Evaluating rules... However! I consider
both to be valid approaches, with BrokenRules having an existing
implementation!

Hope this helps
Jay
 
N

Niki Estner

You can look at a tree and say "this is a maple". But there's no way to
answer the question "it's a maple - where is it?". Type-Object relation is a
one-many relation; You can find out what type an object belongs to (although
this is not a conversion), but the other way round is not possible.

I think somebody else already wrote this: don't use exceptions for that
purpose. If you wouldn't throw an exception, you'd still have a fine "Me"
reference.

Suggestion: Get a good book about OOP.

Niki
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top