PC Review


Reply
Thread Tools Rate Thread

Creatiing a Custom Int32 // ValueType

 
 
carlospedr@gmail.com
Guest
Posts: n/a
 
      5th Sep 2006
Hello,

I trying to do something that I'm not sure is possible. I want to
create a class or struct where this is can be done:

public class myInt32
{
//TODO
}
......

public class b
{
public void myMethod()
{
myInt32 a = new myInt32();
a = 2;
}
}

I want a Int32 to with I want to add some special behaviour, it
possible to create a class or struct where one of it's properties can
be accessed directly by the object name, instead of
myobject.myproperty ?

Hope you can help,
Carlos Pedro

 
Reply With Quote
 
 
 
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      5th Sep 2006
Carlos,

You could add an operator for an implicit and explicit conversions.
However, you will also have to overload all the other operators for your
type if you want to perform things like addition, subtraction, etc, etc.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (E-Mail Removed)

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello,
>
> I trying to do something that I'm not sure is possible. I want to
> create a class or struct where this is can be done:
>
> public class myInt32
> {
> //TODO
> }
> .....
>
> public class b
> {
> public void myMethod()
> {
> myInt32 a = new myInt32();
> a = 2;
> }
> }
>
> I want a Int32 to with I want to add some special behaviour, it
> possible to create a class or struct where one of it's properties can
> be accessed directly by the object name, instead of
> myobject.myproperty ?
>
> Hope you can help,
> Carlos Pedro
>



 
Reply With Quote
 
carlospedr@gmail.com
Guest
Posts: n/a
 
      5th Sep 2006
I don't think that would work since when i do this:

myclass a = new myclass();
a = 2;

I need to convert the 2 (int) to myclass, not the other way arround.

Am I wrong?

Nicholas Paldino [.NET/C# MVP] wrote:
> Carlos,
>
> You could add an operator for an implicit and explicit conversions.
> However, you will also have to overload all the other operators for your
> type if you want to perform things like addition, subtraction, etc, etc.
>
> Hope this helps.
>
> --
> - Nicholas Paldino [.NET/C# MVP]
> - (E-Mail Removed)
>
> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > Hello,
> >
> > I trying to do something that I'm not sure is possible. I want to
> > create a class or struct where this is can be done:
> >
> > public class myInt32
> > {
> > //TODO
> > }
> > .....
> >
> > public class b
> > {
> > public void myMethod()
> > {
> > myInt32 a = new myInt32();
> > a = 2;
> > }
> > }
> >
> > I want a Int32 to with I want to add some special behaviour, it
> > possible to create a class or struct where one of it's properties can
> > be accessed directly by the object name, instead of
> > myobject.myproperty ?
> >
> > Hope you can help,
> > Carlos Pedro
> >


 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      5th Sep 2006
> where one of it's properties can
> be accessed directly by the object name, instead of
> myobject.myproperty ?


I didn't quite follow this; do you mean like the default methods of COM
fame? Then I don't think so. What usage were you looking for?

> myInt32 a = new myInt32();
> a = 2;


You can, however, use implicit cast operators to achive this, at least (not
sure if this was part of the question):

using System;
using System.ComponentModel;
class Program {
private static void Main() {
MyInt32 val = 2;
}
}
[ImmutableObject(true)]
struct MyInt32 {
public readonly int Value;
public MyInt32(int value) { Value = value; }
public static implicit operator MyInt32(int value) {
return new MyInt32(value);
}
public static implicit operator int(MyInt32 value) {
return value.Value;
}
}

Also note that 3.0 may offer some hope for adding extension methods to
existing classes; I can't recall off the top of my head whether this is 3.0
or 4.0 or canned... but it looked interesting...

Marc


 
Reply With Quote
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      5th Sep 2006
That's why you need an implicit operator. Take a look at the section of
the MSDN documentation titled "How to: Implement User-Defined Conversions
Between Structs (C# Programming Guide) ", located at (watch for line wrap):

http://msdn2.microsoft.com/en-us/library/zk2z37d3.aspx


--
- Nicholas Paldino [.NET/C# MVP]
- (E-Mail Removed)

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I don't think that would work since when i do this:
>
> myclass a = new myclass();
> a = 2;
>
> I need to convert the 2 (int) to myclass, not the other way arround.
>
> Am I wrong?
>
> Nicholas Paldino [.NET/C# MVP] wrote:
>> Carlos,
>>
>> You could add an operator for an implicit and explicit conversions.
>> However, you will also have to overload all the other operators for your
>> type if you want to perform things like addition, subtraction, etc, etc.
>>
>> Hope this helps.
>>
>> --
>> - Nicholas Paldino [.NET/C# MVP]
>> - (E-Mail Removed)
>>
>> <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>> > Hello,
>> >
>> > I trying to do something that I'm not sure is possible. I want to
>> > create a class or struct where this is can be done:
>> >
>> > public class myInt32
>> > {
>> > //TODO
>> > }
>> > .....
>> >
>> > public class b
>> > {
>> > public void myMethod()
>> > {
>> > myInt32 a = new myInt32();
>> > a = 2;
>> > }
>> > }
>> >
>> > I want a Int32 to with I want to add some special behaviour, it
>> > possible to create a class or struct where one of it's properties can
>> > be accessed directly by the object name, instead of
>> > myobject.myproperty ?
>> >
>> > Hope you can help,
>> > Carlos Pedro
>> >

>



 
Reply With Quote
 
carlospedr@gmail.com
Guest
Posts: n/a
 
      5th Sep 2006
Thanks it, Thankx to you all.

Marc Gravell wrote:
> > where one of it's properties can
> > be accessed directly by the object name, instead of
> > myobject.myproperty ?

>
> I didn't quite follow this; do you mean like the default methods of COM
> fame? Then I don't think so. What usage were you looking for?
>
> > myInt32 a = new myInt32();
> > a = 2;

>
> You can, however, use implicit cast operators to achive this, at least (not
> sure if this was part of the question):
>
> using System;
> using System.ComponentModel;
> class Program {
> private static void Main() {
> MyInt32 val = 2;
> }
> }
> [ImmutableObject(true)]
> struct MyInt32 {
> public readonly int Value;
> public MyInt32(int value) { Value = value; }
> public static implicit operator MyInt32(int value) {
> return new MyInt32(value);
> }
> public static implicit operator int(MyInt32 value) {
> return value.Value;
> }
> }
>
> Also note that 3.0 may offer some hope for adding extension methods to
> existing classes; I can't recall off the top of my head whether this is 3.0
> or 4.0 or canned... but it looked interesting...
>
> Marc


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Question regardig indexof(string, int32, int32) Jorge Rojas Microsoft Dot NET 1 15th Aug 2007 05:25 AM
Warning 1684 CA2214 : Microsoft.Usage : 'RandomShade..ctor(Int32, Int32, Int32, Int32, Int32)' contains a call chain that results in a call to a virtual method defined by the class. Review the following call stack for unintended consequences: steve bull Microsoft C# .NET 4 7th Jul 2005 05:54 PM
Re: Custom Formatter - ValueType ISerializable Deserialization Problem Lee Chapman Microsoft Dot NET Framework 0 13th Jan 2005 02:12 PM
Custom Mutable/Immutable ValueType Shawn B. Microsoft C# .NET 5 16th Oct 2003 07:42 PM
Custom ValueType to Accept 128-bits Shawn B. Microsoft C# .NET 4 16th Oct 2003 07:16 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:47 AM.