Solved

Return Null Nothing for validation purpose

Posted on 2003-12-08
8
263 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
 

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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 49

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

708 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

21 Experts available now in Live!

Get 1:1 Help Now