Questions tagged [iconvertible]

Interface, which defines methods that convert the value of the implementing reference or value type to a common language runtime type that has an equivalent value.

31 questions
9
votes
1 answer

The type or namespace name 'IConvertible' could not be found

I am trying to implement IConvertible for custom transform. I am using .NET portable and it seems that it is not available there. But the MSDN documentation says: Portable Class Library Supported in: Portable Class Library I am a bit worried…
NotAgain
  • 1,927
  • 3
  • 26
  • 42
8
votes
1 answer

Why does Convert.ChangeType take an object parameter?

The Convert class has been in existence since .NET 1.0. The IConvertible interface has also existed since this time. The Convert.ChangeType method only works on objects of types that implement IConvertible (in fact, unless I'm mistaken, all of the…
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
7
votes
1 answer

How to check if two types can be compared, summed etc.?

if given two types (Type a, Type b), is there any "nice" way to find out if those two can be compared, summed etc.? I was thinking if the types implement IConvertible, one could convert both to lets say decimal and perform a "Convert.ToDecimal(a) >…
Marcus
  • 1,866
  • 1
  • 20
  • 33
6
votes
6 answers

Error : Object must implement IConvertible

I am trying to insert Listbox Items and a Textbox value for each of the listbox items to the database when I get the below error. IF i try to insert the list box items only I am successful but when i try to insert the textbox value I get this error.…
Aditya
  • 71
  • 1
  • 1
  • 4
6
votes
1 answer

How to cast an object to a generic return type, when the return type is a generic interface?

I have an astoundingly ugly method that's supposed to pull assorted data from a database, and use it construct an object of a requested type. The goal is to pull the assorted ugliness into a single place, so that classes that derive from the class…
Jeff Dege
  • 11,190
  • 22
  • 96
  • 165
5
votes
1 answer

Serializing a dictionary with IConvertible values using Json.NET with the TypeNameHandling-flag

I have the following dictionary that I'd very much like to serialize using Json.Net. The dictionary contains items of the IConvertible interface allowing me to add whatever primitive type I need to the dictionary. var dic = new…
user1531921
  • 1,372
  • 4
  • 18
  • 36
5
votes
1 answer

Valid GUID string gives Invalid cast from 'System.String' to 'System.Guid'

I'm running the following code: var guidStr = "C105534D-E001-46F1-874A-322E5E0E132C"; var guid1 = Guid.Parse(guidStr); var guid2 = Convert.ChangeType(guidStr, typeof(Guid)); Console.WriteLine(guid1 + " " + guid2); And while guid1 is getting a value…
Mugen
  • 8,301
  • 10
  • 62
  • 140
4
votes
1 answer

How does the IConvertible interface work with DataRow?

I'm just wondering how the Convert class and IConvertible interface works with a DataRow. If I have this code: string s="25"; int x= Convert.ToInt32(s); The call to Convert.ToInt32(s) will run the following: ((IConvertible)s).ToInt32() So how does…
Roman
  • 565
  • 1
  • 10
  • 27
4
votes
1 answer

LinqToExcel - InvalidCastException leading to Object must implement Iconvertible error

I'm using the following code to query an excel file in LinqToExcel: var excelFile = new LinqToExcel.ExcelQueryFactory(@"\"+txtFileName.Text.Replace(@"\\",@"\")); var properties = from p in excelFile.Worksheet() where…
Rolan
  • 2,924
  • 7
  • 34
  • 46
4
votes
2 answers

Why does .Net Framework base types do not contain implementations of IConvertible methods?

.Net Framework base types such as Int32, Int64, Boolean etc,. implement IConvertible interface but the metadata of these types do not contain the implementations of the methods defined in IConvertible interface such as ToByte, ToBoolean etc,. I am…
Raji
  • 59
  • 3
4
votes
1 answer

Extension method : how to make it work with GUID also

i'm using this extension method for converting objects in my project. But its unable to convert GUID as its doesn't implements IConvertible Interface but for conversion i always have to use new Guid(fooobject) but i want that i could use this method…
Mayank Pathak
  • 3,621
  • 5
  • 39
  • 67
3
votes
1 answer

Implementing IConvertible like functionality for non modifiable types

I have an issue with IConvertible, in short: If DateTimeOffset implemented IConvertible I would not have an issue. You cannot use extension methods to implement an interface, so that road is closed. The struct DateTimeOffset is not partial so it…
flindeberg
  • 4,887
  • 1
  • 24
  • 37
2
votes
1 answer

Implicit Operators and Assignments to SqlParameter.Value with IConvertible Error

I have a class member type as Int32 but the requirement is that the .ToString() method of this member needs to return a special formatted version of the Int32. So I created a struct with implicit operators which preserves the simple value assignment…
1
vote
0 answers

Xamarin: object must implement IConvertible

I get the following Exception System.InvalidCastException: 'Object must implement IConvertible.' on line 2, or any line after the start of the function. public int SaveItem(T item) where T : IBusinessEntity, new() { if…
1
vote
1 answer

Employee and ProductionWorker Classes, getting info from classes

I'm not completely sure the title was good for this. I'm stuck on an assignment for school there is suppose to be a video to shows how to do this but for the life of me I can't figure out how to download the student files from the pearson website.…
Lyght
  • 49
  • 8
1
2 3