473,465 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

ValueType

Greetings,

Is it possible to create a custom ValueType object in C#? Or must I use
managed C++ for that?

Thanks,
Shawn
Nov 15 '05 #1
4 2811
"Shawn B." <le****@html.com> wrote in message
news:ey**************@TK2MSFTNGP10.phx.gbl...
Is it possible to create a custom ValueType object in C#? Or must I use
managed C++ for that?


Neither option exists. Further, it just is not possible in the CLR. You do
have the option of using structs, however, with certain restrictions.

See:
http://msdn.microsoft.com/msdnmag/issues/1200/dotnet/

-- Alan
Nov 15 '05 #2
Yes, just create a struct. Structs in C# are value types.

Richard

--
Veuillez m'excuser, mon Français est très pauvre. Cependant, si vous voyez
mauvais C #, c'est mon défaut!
"Shawn B." <le****@html.com> wrote in message
news:ey**************@TK2MSFTNGP10.phx.gbl...
Greetings,

Is it possible to create a custom ValueType object in C#? Or must I use
managed C++ for that?

Thanks,
Shawn

Nov 15 '05 #3
In the book "Essential Guide to Manged Extensions for C++" page 55 "In MC++,
class that are meant to be created on the stack are called value types. In
this capter you will learn how to create value types, convert them to gc
classes via boxing, and implent interfaces by deriving a value type from
them." On the next page it says "A value type is declared with the keyword
__value followed by class or stuct... for example:

"#using <mscorlib.dll>
__value class Fraction
{
....
}

The value type Fraction can then be created on the stack:

int main()
{
Fraction f(1,2);
}
....

So according to this book (written by people on the MC++ compiler team) it
indicates they can be created in MC++. But I take it not possible in C#
unless I create a struct?

As well, is it a compiler trick that I can say
int i = 10;
int j; j=10;

instead of
int i = new System.Int32();

?
Thanks,
Shawn

"Richard A. Lowe" <ch*****@yumspamyumYahoo.com> wrote in message
news:O4**************@TK2MSFTNGP12.phx.gbl...
Yes, just create a struct. Structs in C# are value types.

Richard

--
Veuillez m'excuser, mon Français est très pauvre. Cependant, si vous voyez mauvais C #, c'est mon défaut!
"Shawn B." <le****@html.com> wrote in message
news:ey**************@TK2MSFTNGP10.phx.gbl...
Greetings,

Is it possible to create a custom ValueType object in C#? Or must I use
managed C++ for that?

Thanks,
Shawn


Nov 15 '05 #4

<snip>
As well, is it a compiler trick that I can say
int i = 10;
int j; j=10;

instead of
int i = new System.Int32();

?
The native value types have special treatment due (at least) to their own IL
instructions for loading onto the stack. But if it's any consolation you
can do your own "compiler magic" with conversion operators:

using System;
namespace ConvertableValueType
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
MyValueType value = new byte[] {0xD0, 0x0F, 0x49, 0x40, 0x0, 0x0, 0x0, 0x1};
Console.WriteLine(value.MySingle);
Console.WriteLine(value.MyInt32);
}
}
public struct MyValueType
{
public MyValueType(float fValue, int iValue)
{
MySingle=fValue;
MyInt32=iValue;
}
public float MySingle;
public int MyInt32;
public static implicit operator MyValueType(Byte[] buffer)
{
return new MyValueType(BitConverter.ToSingle(buffer, 0),
BitConverter.ToInt32(buffer, 4));
}
}
}


Thanks,
Shawn

"Richard A. Lowe" <ch*****@yumspamyumYahoo.com> wrote in message
news:O4**************@TK2MSFTNGP12.phx.gbl...
Yes, just create a struct. Structs in C# are value types.

Richard

--
Veuillez m'excuser, mon Français est très pauvre. Cependant, si vous

voyez
mauvais C #, c'est mon défaut!
"Shawn B." <le****@html.com> wrote in message
news:ey**************@TK2MSFTNGP10.phx.gbl...
Greetings,

Is it possible to create a custom ValueType object in C#? Or must I use managed C++ for that?

Thanks,
Shawn



Nov 15 '05 #5

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

Similar topics

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...
2
by: Eric Newton | last post by:
Since String.Format has to box all value types to accomodate the params, and just for sheer efficiency, are there possibly any plans for a FormatValue method to minimize boxing? public static...
6
by: Sahil Malik | last post by:
Okay, I can't inherit from System.ValueType. Why this restriction?? What I am trying to acheive is, create my own ValueType called "Money". So if I have a decimal that has value 1.93991, when...
6
by: Aryeh Holzer | last post by:
Let me start with a quote from the C# Programmers Reference (where I learned the cool word "covariance"): "When a delegate method has a return type that is more derived than the delegate...
1
by: INeedADip | last post by:
PropertyInfo props = obj.GetType().GetProperties(); foreach (PropertyInfo p in props) if (p.PropertyType is ValueType) this._commonProperties.Add(p.Name, p.GetValue(request, null).ToString()); ...
6
by: Manikkoth | last post by:
Hello, Just curious to see why ValueType (which the base for all value types) is a class. I thought "class" would make a type a reference type. However, IsValueType for ValueType is returning...
1
by: Tony Johansson | last post by:
Hello! Everything not derived from System.ValueType is a reference type. I just wonder this ValueType type is derived from Object and it overrides the two methods Equals and GetHashCode which...
2
by: Veeranna Ronad | last post by:
Hello, Our application gets datetime from an interface function. This function returns "ValueType". How to copy datetime content from this "ValueType" to DateTime variable? Thanks in advance...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.