EDN Admin
Well-known member
using the IConvertible interface:
i basically want to convert a "TypeA" to boolean<br/>TypeA is very simple, all it is is this
struct TypeA <br/> {<br/> public int value;<br/> }
<br/>so i gave it the IConvertible interface,<br/>and visual studio added a bunch of "IConvertible Members"
among them "ToBoolean"
struct TypeA : IConvertible <br/> {<br/> public int value;<br/> <br/> public bool ToBoolean(IFormatProvider provider)<br/> {<br/> }<br/> . . .<br/> }
= = = = = = = = = = = = = = = =
<br/>My problem is i dont know what to put in the Toboolean method!
I have this:<br/>TypeA a; a.value = 42;
i now just want to go like this:<br/>bool b;<br/>b = Convert.ToBoolean(a);
<br/>I want ToBoolean to do this:<br/>if value of a > 0, return true<br/>else return false
<br/>thats it!
<br/>i tried things, but nothings working<br/>(i guess i have to stick to this signature)<br/> public bool ToBoolean(IFormatProvider provider)<br/> {<br/> }
i tried if a.value > 0 return true.<br/>But it doesnt recognize a
Heres the whole code:
<pre lang=x-aspx>//**********************************
using System;
using System.Collections.Generic;
using System.Text;
namespace myChap01Less04Proj01
{
class Program
{
static void Main(string[] args)
{
TypeA a;
a = 42;
bool b;
//convert using ToBoolean
b = Convert.ToBoolean(a);
Console.WriteLine("b took in a TypeA, and is now: {0}", b.ToString());
}
}
// i think this can go on a separate file
struct TypeA : IConvertible
{
public int value;
//allows implicit conv from an int
public static implicit operator TypeA(int _i)
{
TypeA a;
a.value = _i;
return a;
}
//allows explicit conv to an int
//so without this you cant do explicit??
public static explicit operator int(TypeA _a)
{
return _a.value;
}
//provides string conversion (the ToString) to avoid boxing
public override string ToString()
{
return this.value.ToString();
}
//***********************************************************
//***********************************************************
// automagically added stuff by vs
//***********************************************************
//***********************************************************
#region IConvertible Members
public TypeCode GetTypeCode()
{
throw new Exception("The method or operation is not implemented.");
}
//public bool ToBoolean(IFormatProvider provider)
public bool ToBoolean(IFormatProvider provider)
{
//throw new Exception("The method or operation is not implemented.");
if (a.value > 0)
return true;
return false;
}
public byte ToByte(IFormatProvider provider)
{
throw new Exception("The method or operation is not implemented.");
}
public char ToChar(IFormatProvider provider)
{
throw new Exception("The method or operation is not implemented.");
}
public DateTime ToDateTime(IFormatProvider provider)
{
throw new Exception("The method or operation is not implemented.");
}
public decimal ToDecimal(IFormatProvider provider)
{
throw new Exception("The method or operation is not implemented.");
}
public double ToDouble(IFormatProvider provider)
{
throw new Exception("The method or operation is not implemented.");
}
public short ToInt16(IFormatProvider provider)
{
throw new Exception("The method or operation is not implemented.");
}
public int ToInt32(IFormatProvider provider)
{
throw new Exception("The method or operation is not implemented.");
}
public long ToInt64(IFormatProvider provider)
{
throw new Exception("The method or operation is not implemented.");
}
public sbyte ToSByte(IFormatProvider provider)
{
throw new Exception("The method or operation is not implemented.");
}
public float ToSingle(IFormatProvider provider)
{
throw new Exception("The method or operation is not implemented.");
}
public string ToString(IFormatProvider provider)
{
throw new Exception("The method or operation is not implemented.");
}
public object ToType(Type conversionType, IFormatProvider provider)
{
throw new Exception("The method or operation is not implemented.");
}
public ushort ToUInt16(IFormatProvider provider)
{
throw new Exception("The method or operation is not implemented.");
}
public uint ToUInt32(IFormatProvider provider)
{
throw new Exception("The method or operation is not implemented.");
}
public ulong ToUInt64(IFormatProvider provider)
{
throw new Exception("The method or operation is not implemented.");
}
#endregion
}
}
[/code]
View the full article
i basically want to convert a "TypeA" to boolean<br/>TypeA is very simple, all it is is this
struct TypeA <br/> {<br/> public int value;<br/> }
<br/>so i gave it the IConvertible interface,<br/>and visual studio added a bunch of "IConvertible Members"
among them "ToBoolean"
struct TypeA : IConvertible <br/> {<br/> public int value;<br/> <br/> public bool ToBoolean(IFormatProvider provider)<br/> {<br/> }<br/> . . .<br/> }
= = = = = = = = = = = = = = = =
<br/>My problem is i dont know what to put in the Toboolean method!
I have this:<br/>TypeA a; a.value = 42;
i now just want to go like this:<br/>bool b;<br/>b = Convert.ToBoolean(a);
<br/>I want ToBoolean to do this:<br/>if value of a > 0, return true<br/>else return false
<br/>thats it!
<br/>i tried things, but nothings working<br/>(i guess i have to stick to this signature)<br/> public bool ToBoolean(IFormatProvider provider)<br/> {<br/> }
i tried if a.value > 0 return true.<br/>But it doesnt recognize a
Heres the whole code:
<pre lang=x-aspx>//**********************************
using System;
using System.Collections.Generic;
using System.Text;
namespace myChap01Less04Proj01
{
class Program
{
static void Main(string[] args)
{
TypeA a;
a = 42;
bool b;
//convert using ToBoolean
b = Convert.ToBoolean(a);
Console.WriteLine("b took in a TypeA, and is now: {0}", b.ToString());
}
}
// i think this can go on a separate file
struct TypeA : IConvertible
{
public int value;
//allows implicit conv from an int
public static implicit operator TypeA(int _i)
{
TypeA a;
a.value = _i;
return a;
}
//allows explicit conv to an int
//so without this you cant do explicit??
public static explicit operator int(TypeA _a)
{
return _a.value;
}
//provides string conversion (the ToString) to avoid boxing
public override string ToString()
{
return this.value.ToString();
}
//***********************************************************
//***********************************************************
// automagically added stuff by vs
//***********************************************************
//***********************************************************
#region IConvertible Members
public TypeCode GetTypeCode()
{
throw new Exception("The method or operation is not implemented.");
}
//public bool ToBoolean(IFormatProvider provider)
public bool ToBoolean(IFormatProvider provider)
{
//throw new Exception("The method or operation is not implemented.");
if (a.value > 0)
return true;
return false;
}
public byte ToByte(IFormatProvider provider)
{
throw new Exception("The method or operation is not implemented.");
}
public char ToChar(IFormatProvider provider)
{
throw new Exception("The method or operation is not implemented.");
}
public DateTime ToDateTime(IFormatProvider provider)
{
throw new Exception("The method or operation is not implemented.");
}
public decimal ToDecimal(IFormatProvider provider)
{
throw new Exception("The method or operation is not implemented.");
}
public double ToDouble(IFormatProvider provider)
{
throw new Exception("The method or operation is not implemented.");
}
public short ToInt16(IFormatProvider provider)
{
throw new Exception("The method or operation is not implemented.");
}
public int ToInt32(IFormatProvider provider)
{
throw new Exception("The method or operation is not implemented.");
}
public long ToInt64(IFormatProvider provider)
{
throw new Exception("The method or operation is not implemented.");
}
public sbyte ToSByte(IFormatProvider provider)
{
throw new Exception("The method or operation is not implemented.");
}
public float ToSingle(IFormatProvider provider)
{
throw new Exception("The method or operation is not implemented.");
}
public string ToString(IFormatProvider provider)
{
throw new Exception("The method or operation is not implemented.");
}
public object ToType(Type conversionType, IFormatProvider provider)
{
throw new Exception("The method or operation is not implemented.");
}
public ushort ToUInt16(IFormatProvider provider)
{
throw new Exception("The method or operation is not implemented.");
}
public uint ToUInt32(IFormatProvider provider)
{
throw new Exception("The method or operation is not implemented.");
}
public ulong ToUInt64(IFormatProvider provider)
{
throw new Exception("The method or operation is not implemented.");
}
#endregion
}
}
[/code]
View the full article