Solved

Return Null Nothing for validation purpose

Posted on 2003-12-08
8
267 Views
Last Modified: 2010-05-01
Let say I have the following simple code.
        Dim newStudent

        newStudent = Nothing

        If Not IsNothing(newStudent) Then
            Console.WriteLine("They is something")  <-- This is printed
        Else
            Console.WriteLine("They is nothing")
        End If

I need to do the above so let say when the user enter an invalid data, the fuction will return nothing so that I know the function has detected an error e.g. newStudent = GetStudentDetails() in user input and therefore return nothing so that my calling program will reject the entry.

But the above does not work, even when newStudent is nothing which indicate that they is nothing it still say they is something, help, any alternative method is welcome.
0
Comment
Question by:CplusPlus
  • 4
  • 3
8 Comments
 
LVL 39

Expert Comment

by:appari
ID: 9901890

is this code is in VB.NET?
0
 

Author Comment

by:CplusPlus
ID: 9901898
To appari

Yes. sorry about forgetting,,,
0
 
LVL 39

Expert Comment

by:appari
ID: 9901917
what's the datatype of newStudent?

i have tested with type string, seems to work fine

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim x As String
        x = Nothing
        If Not IsNothing(x) Then
            MsgBox("Not Nothing")
        Else
            MsgBox("Nothing")
        End If
        x = "qwqw#"
        If Not IsNothing(x) Then
            MsgBox("Not Nothing")
        Else
            MsgBox("Nothing")
        End If
    End Sub
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:CplusPlus
ID: 9902066
its actually a struc as in Dim newStudent As Student
 
   Structure Student
        Public Name As String
        Public ID As String
        Public BirthYear As Integer
    End Structure
0
 
LVL 51

Expert Comment

by:Ryan Chong
ID: 9902078
Try use the isdbnull function?

If IsDBNull(x) Then
       msgbox "is null"
   Else
       msgbox "not is null"
   End If
0
 

Author Comment

by:CplusPlus
ID: 9902192
does not work ryancys, but thanks anyway.
0
 
LVL 39

Accepted Solution

by:
appari earned 50 total points
ID: 9902207
you can try something like this

      Dim x As student
        x = Nothing
        If Not IsNothing(x.id) Then
            MsgBox("Not Nothing")
        Else
            MsgBox("Nothing")
        End If
0
 

Author Comment

by:CplusPlus
ID: 9902325
Thanks appari, but it seem odd you have to do that to make it work...
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

685 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