473,387 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Converting a Type to an object

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.
--
Chris Douglas
Software Engineer
SRS Technologies, Inc.
christopher "dot" douglas "at" srs "dot" com
Jul 21 '05 #1
2 1756
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

"Christopher W. Douglas" <no****@please.com> wrote in message
news:eZ**************@tk2msftngp13.phx.gbl...
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.
--
Chris Douglas
Software Engineer
SRS Technologies, Inc.
christopher "dot" douglas "at" srs "dot" com

Jul 21 '05 #2
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

"Christopher W. Douglas" <no****@please.com> wrote in
news:eZ**************@tk2msftngp13.phx.gbl...
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.
--
Chris Douglas
Software Engineer
SRS Technologies, Inc.
christopher "dot" douglas "at" srs "dot" com

Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Joseph Suprenant | last post by:
I have an array of unsigned chars and i would like them converted to an array of ints. What is the best way to do this? Using RedHat 7.3 on an Intel Pentium 4 machine. Having trouble here, hope...
5
by: amitbadgi | last post by:
Hi guys, I am getting the following error in teh insert statement , I am converting this asp application to asp.net, here is teh error, Exception Details:...
0
by: ChrisWoodruff | last post by:
I have a C++ function in a COM object that I am trying to implement in VB.NET (the functionality, NOT the COM object, I want to remove the requirement for the COM DLL) I am an experienced VB...
3
by: Brian Henry | last post by:
If i have a structure type and i place the structures data into a tag property (which is an object) it will go in obviously, but when i pull an object back into a structrured type it will error.....
9
by: Terry | last post by:
I am converting (attempting) some vb6 code that makes vast use of interfaces. One of the major uses is to be able to split out Read-only access to an obect. Let me give you a simple (contrived)...
2
by: shenanwei | last post by:
DB2 V8.2 on AIX, type II index is created. I see this from deadlock event monitor. 5) Deadlocked Connection ... Participant no.: 2 Lock wait start time: 09/18/2006 23:04:09.911774 .........
2
by: TheLongshot | last post by:
Ok, let's try this again. I have an ASP.NET 1.1 application that I'm working to convert to 2.0, but I've run into a snag. The problem is with this line: return...
5
by: vtjumper | last post by:
I'm building a C# interface to an existing messaging system. The messaging system allows values of several types to be sent/recieved over the interface. What I want to do is use a generic...
2
by: CoreyWhite | last post by:
Problem: You have numbers in string format, but you need to convert them to a numeric type, such as an int or float. Solution: You can do this with the standard library functions. The...
3
by: Dhananjay | last post by:
Hi All, I am facing problem when i am converting C#.net code(Delegate concept) into vb.net. I am unable to do that . Can someone help me to solve the problem. I am providing my C#.net code. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.