value return of cancel or close event?

Barry Maccaukner

New member
Joined
Jun 1, 2006
Messages
1
Location
Pacific Northwest
Programming Experience
Beginner
spacer.gif
If the user clicks the Cancel button in a dialog box or the Close button on the dialog box’s title bar, the value returned is __?.
spacer.gif
an empty string, a blank character ,the numeric value zero, or the numeric value one My question really is what is the difference between an empty string , a blank character , or the numeric value of zero ? My research shows me that it returns a null character ....How can there be so many things that mean "nothing"? What is the difference?

thanks for the help
 
Last edited:
An empty string is a String object with a Length of zero, i.e. it contains no characters. An empty string is not Nothing because it IS an object, however it is equal to Nothing because converting Nothing to a String creates an empty string. That is an unusual behaviour but it is a convenience created because of the fact that strings are so commonly used. Execute this code and you'll see that an empty string is nothing and something at the same time:
VB.NET:
Dim myString As String = String.Empty

'This tests whether myString is a null reference, i.e. whether the myString variable refers to no object.
If myString Is Nothing Then
    MessageBox.Show("myString Is Nothing")
Else
    MessageBox.Show("Not myString Is Nothing")
End If

'This tests whether myString is a String object that contains nothing, i.e. is an empty string.
If myString = Nothing Then
    MessageBox.Show("myString = Nothing")
Else
    MessageBox.Show("myString <> Nothing")
End If
This difference will become clearer as you gain a better understanding of the difference betweem value types and reference types.

A Char variable, which represents a single character, is a value type, which means that when you declare a variable of type Char it is initialised with zeros. In the case of the Char type this is interpreted as the null character.

When the user dismisses a dialogue what is actually returned is a member of the System.Windows.Forms.DialogResult enumeration. An enumeration is a type that uses identifiers as an easy way to refer to numerical values. If the user clicks the Cancel button or the Close button on the title bar then it is actually the value System.Windows.Forms.DialogResult.Cancel that is returned. The numerical value of that enumerated constant is 2, but it is not the fact that it is 2 that is important. It is the fact that it represents the Cancel action that is importatnt. This is why enumerations are used. Behind the scenes they use integer values for simplicitly, but in your code you can an identifier that has meaning. If you were to use the value 2 in your code you would have to use comments all the time to indicate what it meant, but if you use DialogResult.Cancel the meaning is plain. Another advantage of using enumerations over just integers or just strings is that it limits the number of valid values. Wherever a value of type DialogResult is expected you can only use Abort, Cancel, Ignore, No, None, OK, Retry or Yes, whereas if you were to use an Integer type or String type then you could use any value, not just the equivalents to those enumerated constants.
 
Try this code too:
VB.NET:
Dim result As DialogResult = MessageBox.Show("Please press the Cancel button.", _
                                             "DialogResult Test 1", _
                                             MessageBoxButtons.OKCancel, _
                                             MessageBoxIcon.Information, _
                                             MessageBoxDefaultButton.Button2)

MessageBox.Show(String.Format("You pressed the {0} button. Its numerical value is {1}.", _
                              result.ToString(), _
                              CInt(result)))

result = MessageBox.Show("Please press the Close button in the title bar.", _
                         "DialogResult Test 2", _
                         MessageBoxButtons.OKCancel, _
                         MessageBoxIcon.Information, _
                         MessageBoxDefaultButton.Button2)

MessageBox.Show(String.Format("You pressed the {0} button. Its numerical value is {1}.", _
                              result.ToString(), _
                              CInt(result)))
 
Back
Top