Structure default members

  • Thread starter Thread starter Mario
  • Start date Start date
M

Mario

Hi,

I know Framework doesn't support default members (unless member takes
arguments).
However, for instance, Int32 is a Structure; and we can initialize it like
this:

Dim myInt As Int32
myInt = 123456

Isn't this a default member? There are other members (Int32.MaxValue, etc.).

My trouble is: I want to create a custom data type, as a structure, and with
a default member.
But I dont want to use it like this: structMyType.Value = 123456
So, why some structures are so lucky and I can't make my own?

Please explain if you can help me.
Thank you very much.

Mario
 
Hi,

I know Framework doesn't support default members (unless member takes
arguments).
However, for instance, Int32 is a Structure; and we can initialize it like
this:

Dim myInt As Int32
myInt = 123456

Isn't this a default member? There are other members (Int32.MaxValue, etc.).

My trouble is: I want to create a custom data type, as a structure, and with
a default member.
But I dont want to use it like this: structMyType.Value = 123456
So, why some structures are so lucky and I can't make my own?

Please explain if you can help me.
Thank you very much.

Mario

The concept you are describing is called operator overloading, and it is
not supported at all in VB.NET. Though, the fact is the operator you
are trying to overload (assignment) is not overloadable in C#, which
does support overloading :) So basically, I don't think you can get to
where your trying to go...
 
Hi Mario,

With
Dim myInt As Int32
myInt = 123456
there are two things happening.

Dim myInt As Int32 assigns the default value to the Int32 which is zero.
myInt = 123456 is <your> assignment and therefore has nothing to do with
defaults.

Even if you did
Dim myInt As Int32 = 123456
it's still the two stage process given above and is effectively the same
as
Dim myInt As New Int32 (123456)
which is a constructor with arguments.

Unfortunately, like Tom says, there are no default members for structures.
:-(

Values like Int32.MaxValue are actually constants so they are not part of
any instances at all.

What you could do, if it is really important to you, is fiddle it with a
private flag and a property.

Public Structure DefaultedFoo
Private Const _DefaultForFoo As SomeType = SomeFoo
Private FooIsInitialised As Boolean
Private _Foo As SomeType

Public Property Foo As SomeType
Get
Return IIF (FooIsInitialised, _Foo, _DefaultForFoo)
End Get
Set
Foo = Value: FooIsInitialised = True
End Set
End Property
End Structure

Regards,
Fergus
 
Mario, As others have said, VB.Net doesn't support Default members anymore.
This is becasue of the language change that they have affected. You no
longer have to use 'Set' for assigning References and hence the compiler can
no longer distinguish between a Reference assignment and Default property.

Like for Ex.

Dim O1 As CObject, Dim O2 As CObject
' Let us "assume" CObject has Default Property called Text
O1 = O2 ' Can mean both Set Ref O1= Ref O2 and O1.Text = O2.Text
The Compiler cannot resolve this assignment.
 
Hi Nice Chap,

ROFL. Bark, bark, bark.

I reckon I was looking up the wrong tree there!! ;-)

My apologies, Mario.

Regards,
Fergus
 
Hi again Mario,

But I was correct with this bit
myInt = 123456
<is> effectively shorthand for
myInt = New Int32 (123456)

So Int32 isn't lucky in having a default member after all.

Regards,
Fergus
 
Mario,
myInt = 123456
Isn't this a default member? There are other members (Int32.MaxValue,
etc.).
No! As fergus stated you are creating a new integer structure (123456) and
then assigning that structure to the myInt variable.
My trouble is: I want to create a custom data type, as a structure, and with
a default member.
But I dont want to use it like this: structMyType.Value = 123456
You will need to do what integer is doing under the covers. Define a
constructor, which you may not like any better.

structMyType = new MyType(123456)

I believe (at least I hope) when we get to VS.NET 2004 (Whidbey) with
operator overloading we will be able to define the Explicit & Implicit
operators which will allow you to define conversions that says if I am
assign an integer to MyType, use this function which under the covers calls
the MyType constructor that accepts an integer.

http://msdn.microsoft.com/vstudio/productinfo/roadmap.aspx

The Explicit & Implicit operators are effectively how you overload
assignment, as you are defining how to convert one object to another object.

Hope this helps
Jay
 
Thank you for your answer.
You are right, I will use a .Value property member for now.

Best regards,
Mario
 
I see...
Thank you for your answer, Fergus.
You are right, I will use a .Value property member for now.

Best regards,
Mario
 
Back
Top