Null DbNull Nothing vbnullstring ?

JohnOb

Newcomer
Joined
Mar 22, 2006
Messages
11
Hi all,

I am coming over from VB 6, learning VB.NET.

I have read and have in front of me, the language reference from
the msdn, on; Behavior of Null has changed, Isdbnull function and isnothing function.

However, I am still at a loss to know excatly what to do.

When I check a control for a value, (before I send it to a field on a record
in the database), and the user has not entered anything into that control, What do I check for?
IsDBNULL, Isnothing, or vbnullstring, Len()=0, or all four?

What do I put in a field that is going to the datbase, if the user didn't enter anything into the control?

system.dbnull.value, or Nothing, or vbnullstring or ""

When I get a field back from the database, what do I check for?
IsDBNull, Isnothing, vbnullstring, Len()=zero?

Thanks in Advance,

John

:confused:
 
First thing is first, if any function you call is the in the Microsoft.VisualBasic namespace do not use it, this includes:

IsNull
IsDbNull
CInt
CDbl
CSng
CLng
CStr
Redim
Preserve
Len
UCase
LCase
Trim
UBound
LBound
On Error Goto
Resume

The only exception is CType which works differently than VB6 CType, use this only when trying to cast an unknown object to a specific type, if it cannot cast it will return nothing. If you know the type of the object you should use DirectCast as it has better performance, but it will throw an InvalidCastException if it tries to cast to a type that the object isn't.

You want to use System.DbNull.Value to test for null coming from a Database

You want to use String.Empty for "", however "" is acceptable.

If you want to see if an object is set you should:

If Not myObject Is Nothing Then
myObject.PerformAction()
End If

There are is another String function in .NET 2.0, but it is just encapsulating above methods into a single method (check for Empty or Null (Nothing))

As far as what to check in the control depends on the control type, if it's a TextBox then it will at minimum be String.Empty - a TextBox cannot have it's Text property set to Nothing. In fact most controls that your going to use coming from VB6 won't have a 'null' value if it's not filled in, it will either have String.Empty, -1, or 0 as values when not set depending on the control type.
 
Bri, you are a bit too quick to shoot down Visual Basic functions. You don't even give a reason why. Don't take this post personally, but I hate to see misinformation so I am making a few corrections.

As far as Visual Basic functions and classes go, John, what you need to do is put the effort in and research and understand Microsoft.VisualBasic functions and Visual Basic keywords, consider your needs, and do what works for you.

For example, to make the learning process easier, you might want to stick with the VB functions you know, and use the "more .Net" functions as you learn them, but you should also consider that if you are looking to work with or get help from other .Net programmers, you should be wary of features and functions unique to Visual Basic because they make it difficult for C#/managed C++/etc users to work with you. Also understand that Microsoft considers all functions and classes in the VisualBasic namespace to be part of the Visual Basic language, granted that while some programmers agree with this, most disagree.

Also, you need to understand what is going on under the hood when you use VB commands. For example, for conversions between numeric types, CInt, CLng, CBool, and CShort are all inlined. This is neater and more succinct in terms of code, and more efficient at runtime, and the compiled code is completely CLS compliant and language neutral. ReDim is merely an abbreviated syntax for array creation. And then many VB functions merely wrap other .Net functions to provide VB6 users a familiar alias, and have no significant effect of performance.

Also, Bri's understanding of CType/DirectCast is not entirely accurate. CType will try to cast a value if possible, and if that is not possible it will try to convert the value (i.e. convert a numeric value to its string representation), and if that fails, and exception will be thrown. DirectCast will only cast and will not convert, which means it performs very slightly faster but throws exceptions when a conversion may be available. The TryCast keyword, on the other hand, will return null when a cast fails rather than throwing an exception.

Also, you can set a TextBox's Text property to null. In fact, most properties of most classes will treat a null string exactly the same as an empty string. The Visual Basic = operator considers the two (null and empty string) to be equal.

As far as your question, what to compare to, well, what have you tried? Whip up a test project and see what works and what doesn't.
 
If you run the following in C# you will see that only the "Was Empty" is shown:

C#:
if(this.textBox1.Text == null)
	MessageBox.Show("Was null");
if(this.textBox1.Text == String.Empty)
	MessageBox.Show("Was Empty");
this.textBox1.Text = null;
if(this.textBox1.Text == null)
	MessageBox.Show("Was null");
if(this.textBox1.Text == String.Empty)
	MessageBox.Show("Was Empty");

I think your reasons for saying you can set a TextBox's Text property to null is because it allow you to pass it, but internally it say's it's still String.Empty, not null. Since VB treats String.Empty = Nothing (or is it Nothing = String.Empty? or both?) then I believe the similiar above in VB would show both message boxes before and after the click. I don't know CLR though so I'm admitedly making an educated guess on that. And thanks for the further detail of CType/DirectCast...I always forget that it does converting too, I soley use it as an equivelant to:

C#:
  MyNameObject mo = SingletonClass.GetObject() as MyNameObject

The reason I shoot down VB6 functions is because they are VB6 functions. They are deprecated in my book even if not officially. Why? Well to give an example I work with a lot of RPG programmers. They have a few old dogs who are still using RPG 2 and 3, they currently for the most part do RPG 4 and are trying to move to ILE (and switch over to Java on new programs concurrently). Unfortunately IBM supports syntax all the way back to RPG 1. And it's really funny to watch these new programmer's complain about the code some old dog wrote in RPG 2 that could be done much cleaner in PRG Free Form. Then to watch these old dogs totally lost when trying to debug a program written in the 'newer' language (because they never learned RPG 3 or 4 or ILE because since it was always backwards compatable they never had to). See anything similiar? He can learn it now, or he can learn it later...the longer he takes to learn it, the worse off he'll be IMNSHO. Backwards compatiability on a DLL is great, on a language I think it's asking for trouble if it's any more than 1 generation...and that one generation that is backwards compatable should have everything marked as deprecated.

Finally, I'm sorry but thinking of 'your' needs, and 'what works best for you' is a sure way to have something that isn't supportable and isn't maintainable in a corporate environment. Supportability and how easy it will be to make future enhancements by people who aren't familiar with the program have to be at the foremost of your mind (and yes documentation is a large part of that), and in a corporate envrionment you have about a 10% chance of supporting something you wrote. VB6 isn't taught in colleges around here, .NET and Java are...and they don't teach CType, CDbl, or CSng...so when these kids come in they maybe able to figure out what it means, but they're going to wonder why they're using it (kind of that whole RPG 2 thing)...go 5 years into the future and this could be a big problem.

If your sole desire in life is to write your own personal programs as a hobby; do what you like...use GoTo Line 12 if the language supports it and poor some Ragu over that spagetti, but if you want to work in a corporate enviornment - IS or ISV then you need to lose that 'what works best for you' attitude real quick and follow the corporate standards.

If your corporate standards are to use CSng and such...then go for it...but at my corp that's against the standards and I, on a personal level, agree with those standards. Sure, it does cause me to write extra code when working with arrays and a lot of other things when I'm doing VB.NET that I could be lazy and use old VB6 functions for, but then I don't mind because I know a Java or C# programmer can look at it and support it and modify it without have to figure out the VB6 specifics. This means when it comes to code reviews and technical design documents I don't get asked any questions, which means I get a out of meetings quicker, which means I'm a happier person.
 
bri189a said:
Finally, I'm sorry but thinking of 'your' needs, and 'what works best for you' is a sure way to have something that isn't supportable and isn't maintainable in a corporate environment. Supportability and how easy it will be to make future enhancements by people who aren't familiar with the program have to be at the foremost of your mind
We could discuss this all day, but this isn't our thread. If you want to discuss it more, I'll be more than happy to on a new thread.

I just think you need to realize that supportability and maintainability fall under what would be considered one's own needs. Complying with the standards that apply to you and your collagues should be considered one's own needs. Balancing productivity and time spend learning should also be considered a need. Interaction with Java or C# programmers may or may not be a need. I am not saying that the VB6 functions should be used, and I'm not saying they shouldn't be. All that I am saying is to consider all the circumstances and make the choice that makes the most sense. There is a difference between being "lazy" and working intelligently. Suppose you're new to VB.Net and you remove the Microsoft.VisualBasic import from your projects because it is "bad", and now you have to learn a whole new set of string manipulation functions, different array creation syntax, new conversion functions and on and on. You will spend more time browsing MSDN and google than writing code, and that isn't very smart. Maybe writing .Net code that will still make sense years down the road is a need, but so is being productive now.
 
Thank You both for your help.

I have no problem with using the new syntax.

Where can I find the new reserved words for the list you gave,

IsNull
IsDBNull
etc....

Do I just put a 'system.' in front of it, and selected one of the
options when I hit the period.

My main problem right now is, I am using a Component One text box.
c1.win.c1input.c1textbox
and It sometimes returns "IsDBNull" in the value property and
sometimes returns "Nothing" in the value property and
sometimes returns something that causes an error no matter what.
(An unhandled exception of type c1.win. etc....

If the user doesn't enter anything in the textbox, the value property
comes back as "IsDBNull". So I can check for that.

If the user enters a number, and then tabs away from the control,
and then tabs back to the control and clears the contents,
then sometimes I get "Nothing" or something else that causes
the exception error.

I have checks for both IsNothing and IsDbNull but I still get the
program crashes.

I would use the microsoft text box, but I need an edit mask.

I will call component One Tomorrow.

By the way I don't recommend the componnet One controls,
they are very tricky, and the documentation is quite cryptic.

Thank You for your help,

John
 
John,

One thing that you probably already have done, but worth checking...when you do your check do you do it as:

Visual Basic:
If Object.Text Is Nothing OrElse IsDbNull(Object.Text) Then
    'Handle condition
End If

'Or is it:
If Object.Text Is Nothing Or IsDbNull(Object.Text) Then
   'Handle condition
End If

The Or and OrElse have very different behaviors, you probably already know this, but I wanted to check to see what you were using first (you should be using the top). I definitely don't understand why a control would return DBNull
 
I tried what you said, but the control still causes the program to crash,
it seems it returns something that is neither DBNull or Nothing.
I will call component one.

Thank You for your help,
 
Just out of curiousity, what is displayed if you do something like this?
Visual Basic:
MessageBox.Show(MyControl.Text.GetType().ToString())
 
I get: Sytem.String

However what I really need is the value property.
As I want the numeric part, (the field has an edit mask
for Money) and I don't want the $ , and . characters.

When I do this:

MessageBox.Show(c1txtfee.Value.GetType().ToString())

the message box line crashes,

An Inhandled exception of type "C1.Win.C1Input.ValidationException occured in C1.Win.C1Input.dll

The entered string cannot be parsed etc.......



marble_eater said:
Just out of curiousity, what is displayed if you do something like this?
Visual Basic:
MessageBox.Show(MyControl.Text.GetType().ToString())
 
Back
Top