Solved

Return Null Nothing for validation purpose

Posted on 2003-12-08
8
265 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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 50

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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

810 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