Solved

Validation Checkking VS 2008 C#

Posted on 2011-03-10
8
277 Views
Last Modified: 2012-05-11
I am looking for tips on how to perform better validation checking than what I did in this code snippet. What was happening, rarely, but it did happen was the value of DocumentStatus was null which is an invalid state but bad data happens. What is the cleanest and most efficient way to rewrite this and account for nulls? My entire form just crashed and locked up because of this. Is the only answer to add DocumentStatus != null to all the if statements? Does C# have an equivalent to the SQL coalesce function?
if (PmTransactionScroll.DocumentStatus == 3)
                {
                    if (ViewPayablesDistributionZoomFormHIST == null || ViewPayablesDistributionZoomFormHIST.Created == false)
                    {
                     ViewPayablesDistributionZoomFormHIST = new frmViewJobLinkerHIST();
                     ViewPayablesDistributionZoomFormHIST.Show();
                        ViewPayablesDistributionZoomFormHIST.Activate();
                    ViewPayablesDistributionZoomFormHIST.Focus();
                    }
                }

Open in new window

0
Comment
Question by:rwheeler23
  • 3
  • 3
  • 2
8 Comments
 
LVL 23

Expert Comment

by:wdosanjos
ID: 35105885
What's the DocumentStatus data type?
0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 333 total points
ID: 35107754
Either that or, if PmTransactionScroll is a custom object of your own, then you could alter it to return a default value if it is null.
0
 

Author Comment

by:rwheeler23
ID: 35108791
I have no control over the DocumentStatus object. It is defined as an integer.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35108905
If it is an integer then it cannot hold a null value. So it must either be a Nullable<int>, in which case null would be a valid value, or it is complaining about something else.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 23

Accepted Solution

by:
wdosanjos earned 167 total points
ID: 35108960
That's interesting, because integer types do not take null, unless they are defined as int? or long? (nullable integer types).  Is it possible that the problem is with PmTransactionScroll instead?  Or, is DocumentStatus a property that throws an exception in certain cases?  
0
 

Author Comment

by:rwheeler23
ID: 35108994
The problem is with the scrolling object. This object is on a form. What I have discovered is that occasionally the form is empty so probably what I should be checking is for an empty form not a null value of the integer.
0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 333 total points
ID: 35109224
Yes. If PmTransactionScroll could possibly be null then you need to check for that.
0
 

Author Comment

by:rwheeler23
ID: 35112390
I am relaetively new to VS C# and .Net Programming and one thing I have noticed is that if you have any kind of conditional statement but neglect to account for a condition, when the program runs into that condition only heaven knows what it will do. It appears to just fall off the cliff. I tried putting breakpoints in the code but since this condition was not accounting for none of them would get hit. I guess that is a clue in itself. Thanks for your help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Data Saving 5 56
index Out OF Range Exception error 4 53
Handle null when using linq in this line 1 26
Access properties in nested observable collections 8 23
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now