473,387 Members | 1,606 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,387 software developers and data experts.

Creatiing a Custom Int32 // ValueType

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

Sep 5 '06 #1
5 1492
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]
- mv*@spam.guard.caspershouse.com

<ca********@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
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

Sep 5 '06 #2
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]
- mv*@spam.guard.caspershouse.com

<ca********@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
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
Sep 5 '06 #3
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
Sep 5 '06 #4
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]
- mv*@spam.guard.caspershouse.com

<ca********@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
>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]
- mv*@spam.guard.caspershouse.com

<ca********@gmail.comwrote in message
news:11**********************@i42g2000cwa.googleg roups.com...
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

Sep 5 '06 #5
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
Sep 5 '06 #6

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

Similar topics

0
by: Stephen | last post by:
This is a real brain-teaser and i'd really appreciate it if someone can try and understand what im trying to do and give me a few pointers or ideas to help me work out my problem. Im basically...
4
by: Shawn B. | last post by:
Greetings, Is it possible to create a custom ValueType object in C#? Or must I use managed C++ for that? Thanks, Shawn
4
by: Shawn B. | last post by:
Greetings, I know I can create class that "implicit"ly accepts an 8-bit to 64-bit value. Without using double, or single, I would like to create my own "unsigned" 128-bit valuetype (to be used...
4
by: Brian Brane | last post by:
I have properties that wrap DataRow columns as in: public int aNumber { get{ return m_DataRow; } set{ m_DataRow = value; } } If the column happens to contain DBNull, I get a cast exception...
3
by: Laura T. | last post by:
The following code is driving me so crazy, that I'm thinking it's a "feature" or a.. ....dare I say it... a BUG. I'm using VS 2003. I've created a new value type (struct) called MyInt. MyInt has...
0
by: supreeth.bhat | last post by:
I created a new Performance Counter Category and added three new counters. Used InstallUtil.exe to create them on remote production server. I can see the custom counter category and counters. ...
6
by: Peter Hartlén | last post by:
Hi! What's the correct syntax for the default value design time attribute, using classdiagram view and Custom Attributes dialog. For a boolean: DefaultValue(true) DefaultValue("true")...
2
by: Eric Renken | last post by:
I have a Windows .NET 2.0 application that has been working fine for last year, and now all of a sudden on one customers computer I am getting this error, "Binary format of the specified custom...
0
by: Pieter | last post by:
Hi, I'm using NHibernate 1.2 (CR1), and I'm using a custom list (inherited from BindingList(Of T) ) for all my lists. The NHibernate documentation told me that I had to implement...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.