Data Types

fredriko

Active member
Joined
Jun 3, 2004
Messages
35
Programming Experience
10+
Does anybody know how I can create a data type in VB.Net.
I want to be able to create a data type called "EmailAddress" and then assign it as such:
VB.NET:
dim Email as EmailAddress
Once, declared I want to be able to use it just as I would a string or integer data type:
VB.NET:
Email = "[email="mike@someplace.com"]mike@someplace.com[/email]"
The data type would validate the email and set it assuming the syntax was correct. As pretty much everything in .Net is an object anyway I thought this might be possible?
 
I think you can do something very close to that but you cannot make a new data type. In that way you want to change the language and it's impossible.
Otherwise, you can make custom data type through an object (class).
E.g.
PHP:
 Public Class Email 
 
Public Sub send(ByVal str As String)
 
System.Diagnostics.Process.Start("mailto:" & str)
 
EndSub
 
End Class
and later you can call wherever you need like follows:

PHP:
 Dim newVar As Email = New Email 
 
newVar.send("kulromblahblah@yahoo.com")


Cheers ;)
 
to follow up on kulrom:

VB.NET:
Option Explicit On 
Option Strict On

Public Class Email
	Private mRegEx As New System.Text.RegularExpressions.Regex("^[\w-]+(?:\.[\w-]+)*@(?:[\w-]+\.)+[a-zA-Z]{2,7}$")
	Private mstrAddress As String

	Public Property Address() As String
		Get
			Return mstrAddress
		End Get
		Set(ByVal Value As String)
			If mRegEx.IsMatch(Value.Trim()) = True Then
				mstrAddress = Value
			Else
			 Throw New FormatException("Invalid Email Address Format")
			End If
		End Set
	End Property

	Public Overloads Sub Send(ByVal Address As String)
		If mRegEx.IsMatch(Address.Trim()) = True Then
			System.Diagnostics.Process.Start("mailto:" & Address)
		Else
			Throw New FormatException("Invalid Email Address Format")
		End If
	End Sub

	Public Overloads Sub Send()
		If Me.Address.Length <> 0 Then
			System.Diagnostics.Process.Start("mailto:" & mstrAddress)
		Else
			Throw New Exception("No email address present")
		End If
	End Sub
End Class
 
Last edited:
i figured if she/he is going to be dealing with a specific format of string (email address) then it should at least be only storing strings that are of valid email format otherwise why bother having the class at all
 
When you are talking about data types, you are talking about "value" types. Given that classes are "reference" types, creating a structure rather than a class would be closer to creating a new data type. You can still do (most of) the same things with a structure as you can with a class. The fact that a structure is a value type, though, means that declaring a variable without instantiating or setting a variable to Nothing would have the effect of making it an empty String, rather than a null reference. This may or may not be desirable behaviour, but that is how an actual in-built data type would behave.
 
Actually, what you're looking for is not a Type but an Enumeration.

VB.NET:
 Private Enum MyConstants 
thisValue = 0 
thatValue = 1 
End Enum

After this if you type one of your variables As MyConstants (or whatever name you choose for your enumeration...), then you'll get the Intellisense support when typing out your code.

Cheers ;)
 
Back
Top