• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 273
  • Last Modified:

How to define a new variabel type in VB.NET

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.
  • 3
  • 2
  • 2
4 Solutions
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.
>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.

daghoffAuthor Commented:
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.
Technology Partners: 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!

> 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?
daghoffAuthor Commented:
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.

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
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now