Solved

Return Null Nothing for validation purpose

Posted on 2003-12-08
8
268 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

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 52

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

Independent Software Vendors: 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!

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

751 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