How to define a new variabel type in VB.NET

Posted on 2011-10-10
Last Modified: 2012-05-12
Let me explain with an example.

By variabel type I mean f. ex. Boolean, String, Integer etc....

A variabel defined as Boolean can only hold the values True or False. Asigning anything else to it would result in a runtime error and/or a syntax error.

I would like to define a variabel type that we could call BolleanExt. A variabel defined as BooleanExt can only hold the values True, False or None. Asigning any other value will result in an error.

Another example could be a variable type that can only hold the three values Abc, Def or Ghi.

Can someone give me an exaple code on how this new variabel type could be defined so I can declare variabels from it in my code the same way I declare any other ordinary variabel like: Dim var1 As String.
Question by:daghoff
    LVL 28

    Assisted Solution

    Fist off, there is a thing called a nullable Boolean. This will have values of True, False, and Null.

    Secondly, anything else can be achieved by defining a class (MyClass.vb) and implementing constructors, properties, etc that constrict the requirements of the values held by that class.
    LVL 83

    Accepted Solution

    >Another example could be a variable type that can only hold the three values Abc, Def or Ghi.

    You can use an Enum for that.

    Author Comment

    The Enum concept looks interesting, but I would like to see how it is being done in a class. I have searched Internet for hours trying to find good examples of that without luck. I know it is out there somewhere, but the problem is to construct searches that gives me what I want. That’s way I turn to EE.

    Please give me a good example code or a link to something that explains this in a simple way.
    LVL 83

    Expert Comment

    > but I would like to see how it is being done in a class.
    Not sure what you mean there. Using Enum within a class?

    Author Comment

    Sorry for not expressing myself in a clear way.  Enum or not Enum is not relevant.

    My question is how to define a new variabel type within a class in the simplest way using whatever that is needed.

    I have no ide how this is to be done. This is a black box for me that I try to get inside.

    LVL 83

    Assisted Solution

    There are two types of objects/variables

    Primitives such as Integer, String, Boolean etc

    Class Objects.  You can just define any class and then creates its variable. With regards to limiting the value that a variable can hold, you can use Enum if the range is limited, or you would have to implement some sort of logic. An example below

    Public Class Person
    Private _Age As Integer

    Public Property Age
       Return _Age
    End Get
    Set(value as Integer)
        If value > 18 and value <100 Then
           _Age = value
        End If
    End Set
    End Class
    LVL 28

    Assisted Solution

    If you want simple, you can use a struct, but that limits your abilities.

    Public Structure MyVariable
        Public myValue As String
        Public myValue2 As Integer
    End Structure

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    729 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

    16 Experts available now in Live!

    Get 1:1 Help Now