473,386 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Using a structure in the same way as a primitive type

I'd like to create a structure such as:

Public Structure MyType
Private m_variable As String

Public Property Variable() As String
Get()
return m_variable
End Get
Set (ByVal Value As String)
If validateVariable(Value) Then
m_variable = Value
Else
Throw New ArgumentException("Variable invalid")
End If
End Set
End Property

Private Function validateVariable(ByVal theVar as String)
'Do validation here
End Function
End Structure

and be able to use it like

Dim mt as MyType
Try
mt = "123abc"
Catch
'Handle exception
End Try

instead of

Dim mt As MyType
Try
mt.Variable = "123abc"
Catch
'Handle exception
End Try

Is this possible, and if so, what changes do I need to make?

Thanks

-- Chris

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>TSAD/yXWZ0qaGm1FxUbJXA==</Id>
Nov 22 '05 #1
4 1313
Chris,
Is this possible, and if so, what changes do I need to make?


Currently no. In the next version (VS 2005) yes, thanks to operator
overloading.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 22 '05 #2
Chris,
Is this possible, and if so, what changes do I need to make?


Currently no. In the next version (VS 2005) yes, thanks to operator
overloading.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 22 '05 #3
Chris,
In addition to the other's comments,

I would define a constructor, then you can do:
mt = New MyType("123abc")
Public Structure MyType
Public Sub New(ByVal value As String)
validateVariable(value)
m_variable = value
End Sub

Hope this helps
Jay
"Chris Bernholt via .NET 247" <an*******@dotnet247.com> wrote in message
news:OK*************@TK2MSFTNGP10.phx.gbl... I'd like to create a structure such as:

Public Structure MyType
Private m_variable As String

Public Property Variable() As String
Get()
return m_variable
End Get
Set (ByVal Value As String)
If validateVariable(Value) Then
m_variable = Value
Else
Throw New ArgumentException("Variable invalid")
End If
End Set
End Property

Private Function validateVariable(ByVal theVar as String)
'Do validation here
End Function
End Structure

and be able to use it like

Dim mt as MyType
Try
mt = "123abc"
Catch
'Handle exception
End Try

instead of

Dim mt As MyType
Try
mt.Variable = "123abc"
Catch
'Handle exception
End Try

Is this possible, and if so, what changes do I need to make?

Thanks

-- Chris

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>TSAD/yXWZ0qaGm1FxUbJXA==</Id>

Nov 22 '05 #4
Chris,
In addition to the other's comments,

I would define a constructor, then you can do:
mt = New MyType("123abc")
Public Structure MyType
Public Sub New(ByVal value As String)
validateVariable(value)
m_variable = value
End Sub

Hope this helps
Jay
"Chris Bernholt via .NET 247" <an*******@dotnet247.com> wrote in message
news:OK*************@TK2MSFTNGP10.phx.gbl... I'd like to create a structure such as:

Public Structure MyType
Private m_variable As String

Public Property Variable() As String
Get()
return m_variable
End Get
Set (ByVal Value As String)
If validateVariable(Value) Then
m_variable = Value
Else
Throw New ArgumentException("Variable invalid")
End If
End Set
End Property

Private Function validateVariable(ByVal theVar as String)
'Do validation here
End Function
End Structure

and be able to use it like

Dim mt as MyType
Try
mt = "123abc"
Catch
'Handle exception
End Try

instead of

Dim mt As MyType
Try
mt.Variable = "123abc"
Catch
'Handle exception
End Try

Is this possible, and if so, what changes do I need to make?

Thanks

-- Chris

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>TSAD/yXWZ0qaGm1FxUbJXA==</Id>

Nov 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: BCC | last post by:
Hi, This is kind of a followup to a previous question... I have a container class that has a load of variables (~100). For example, I have groupings like this: class CContainer {...
4
by: Pram | last post by:
I am curious why did Microsoft write System.DateTime as a structure instead of a class? Maybe for backward compatibilty with any existing old code? FYI, one of the differences between...
4
by: Chris Bernholt via .NET 247 | last post by:
I'd like to create a structure such as: Public Structure MyType Private m_variable As String Public Property Variable() As String Get() return m_variable End Get Set (ByVal Value As String)
3
by: case2005 | last post by:
Can anyone help with the following, I don't know if it's possible, but I'm certain there must be a standard way of dealing with this. I have the following: template<typename FooBar, typename...
15
by: Charles Law | last post by:
I have adapted the following code from the MSDN help for PropertyInfo SetValue. In the original code, the structure MyStructure is defined as a class MyProperty, and it works as expected. There is...
1
by: Jonathan Amend | last post by:
I'm trying to port some C++ code to VB.NET but I have hit a snag. I need to add the pointer of a structure which includes an array to an array and then pass it to an API. Here are the two code...
10
by: Qwert | last post by:
Hello, is it correct that if a type (System.Type) is a value (.IsValueType=True) and not primitive (.IsPrimitive=False), that type is a structure? Thanks.
12
by: Sam Kong | last post by:
Hi, JavaScript hides its memory structure. I know that numbers, booleans, null and undefined are value types (value is directed saved in a variable). I want to know: - How JavaScript...
11
by: JerryWEC | last post by:
I want to be able to create a structure like I did in VB6, that have variables Name As String * 10 Age As String * 3 for the size of the strings. Can I do this some way in VB.net? I'm...
65
by: Arjen | last post by:
Hi, Form a performance perspective, is it wise to use the ref statement as much as possible? Thanks! Arjen
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.