ToString and FromString

S

shapper

Hello,

I created a TypeConverter to convert a List<Tag> to a from String.

For example, List<Tag> to String all I need to do is:

String tags = MyListTags.ToString();

But how to make the inverse conversion? From String to List<Tag>?
I would need something like FromString ...

I am just not sure how to apply my converter in this case.

This is my Type Converter code:

public class TagsConverter : TypeConverter {

// CanConvertFrom
public override Boolean CanConvertFrom(ITypeDescriptorContext
context, Type sourceType) {
return sourceType == typeof(IList<Tag>) ? true :
base.CanConvertFrom(context, sourceType);
} // CanConvertFrom

// CanConvertTo
public override Boolean CanConvertTo(ITypeDescriptorContext
context, Type destinationType) {
return destinationType == typeof(String) ? true :
base.CanConvertTo(context, destinationType);
} // CanConvertTo

// ConvertFrom
public override Object ConvertFrom(ITypeDescriptorContext context,
CultureInfo culture, Object value) {
if (value is String) {
String tags = (String)value;
return tags.Split(new char[] { ',' },
StringSplitOptions.RemoveEmptyEntries).Select(t => new Tag() { Name =
t.Trim() }).ToList();
}
return base.ConvertFrom(context, culture, value);
}

// ConvertTo
public override Object ConvertTo(ITypeDescriptorContext context,
CultureInfo culture, Object value, Type destinationType) {
if (value is List<Tag>) {
List<Tag> tags = (List<Tag>)value;
return String.Join(", ", tags.Select(t => t.Name).ToArray());
}
return base.ConvertTo(context, culture, value, destinationType);
} // ConvertTo

} // TagsConverter

And I am registering it as follows:
TypeDescriptor.AddAttributes(typeof(List<Tag>), new
TypeConverterAttribute(typeof(TagsConverter)));

Thanks,
Miguel
 
S

Stanimir Stoyanov \(C# MVP\)

Hello Miguel,

If you are compiling against C# 3.5 or later, it is easy to write an
extension method called FromString accessible from all List<Tag> instances,
populating the given instance.

internal static class Extensions
{
internal static void FromString(this List<Tag> list, string
tags)
{
List<Tag> newTags = (List<Tag>)new
TagsConverter().ConvertFromString(tags);

list.AddRange(newTags);
}
}

The above will make it possible to use the following syntax:

List<Tag> tags = new List<Tags>();

tags.FromString("tag, another, test"); // Note that FromString does
not return anything, but populates the list.

You might consider clearing the existing list in the FromString method body
or avoid adding duplicates.

For every other .NET versions you can create a helper class with a static
method similar to the extension one.

HTH,
 
S

Stanimir Stoyanov \(C# MVP\)

Hello Miguel,

If you are compiling against C# 3.5 or later, it is easy to write an
extension method called FromString accessible from all List<Tag> instances,
populating the given instance.

internal static class Extensions
{
internal static void FromString(this List<Tag> list, string
tags)
{
List<Tag> newTags = (List<Tag>)new
TagsConverter().ConvertFromString(tags);

list.AddRange(newTags);
}
}

The above will make it possible to use the following syntax:

List<Tag> tags = new List<Tags>();

tags.FromString("tag, another, test"); // Note that FromString does
not return anything, but populates the list.

You might consider clearing the existing list in the FromString method body
or avoid adding duplicates.

For every other .NET versions you can create a helper class with a static
method similar to the extension one.

HTH,
 
S

shapper

Hello Miguel,

If you are compiling against C# 3.5 or later, it is easy to write an
extension method called FromString accessible from all List<Tag> instances,
populating the given instance.

        internal static class Extensions
        {
            internal static void FromString(this List<Tag> list, string
tags)
            {
                List<Tag> newTags = (List<Tag>)new
TagsConverter().ConvertFromString(tags);

                list.AddRange(newTags);
            }
        }

The above will make it possible to use the following syntax:

        List<Tag> tags = new List<Tags>();

        tags.FromString("tag, another, test"); // Note that FromString does
not return anything, but populates the list.

You might consider clearing the existing list in the FromString method body
or avoid adding duplicates.

For every other .NET versions you can create a helper class with a static
method similar to the extension one.

HTH,
--
Stanimir Stoyanov
Microsoft MVP -- Visual C#http://stoyanoff.info


I created a TypeConverter to convert a List<Tag> to a from String.
For example, List<Tag> to String all I need to do is:
String tags = MyListTags.ToString();
But how to make the inverse conversion? From String to List<Tag>?
I would need something like FromString ...
I am just not sure how to apply my converter in this case.
This is my Type Converter code:
 public class TagsConverter : TypeConverter {
   // CanConvertFrom
   public override Boolean CanConvertFrom(ITypeDescriptorContext
context, Type sourceType) {
     return sourceType == typeof(IList<Tag>) ? true :
base.CanConvertFrom(context, sourceType);
   } // CanConvertFrom
   // CanConvertTo
   public override Boolean CanConvertTo(ITypeDescriptorContext
context, Type destinationType) {
     return destinationType == typeof(String) ? true :
base.CanConvertTo(context, destinationType);
   } // CanConvertTo
   // ConvertFrom
   public override Object ConvertFrom(ITypeDescriptorContext context,
CultureInfo culture, Object value) {
     if (value is String) {
       String tags = (String)value;
       return tags.Split(new char[] { ',' },
StringSplitOptions.RemoveEmptyEntries).Select(t => new Tag() { Name =
t.Trim() }).ToList();
     }
     return base.ConvertFrom(context, culture, value);
   }
   // ConvertTo
   public override Object ConvertTo(ITypeDescriptorContext context,
CultureInfo culture, Object value, Type destinationType) {
     if (value is List<Tag>) {
       List<Tag> tags = (List<Tag>)value;
       return String.Join(", ", tags.Select(t => t.Name).ToArray());
     }
     return base.ConvertTo(context, culture, value, destinationType);
   } // ConvertTo
 } // TagsConverter
And I am registering it as follows:
TypeDescriptor.AddAttributes(typeof(List<Tag>), new
TypeConverterAttribute(typeof(TagsConverter)));
Thanks,
Miguel

Thank You Stanimir,

I am using NET 3.5 and I was looking exactly for an extension but I
was not being able to create it.
I was trying to create a generic extension List<T> to convert any list
to a String.
This way any list I have and that I apply ToString I would fire the
correspondent convert since there is not ToString in List.

Is this possible?

Thanks,
Miguel
 
S

shapper

Hello Miguel,

If you are compiling against C# 3.5 or later, it is easy to write an
extension method called FromString accessible from all List<Tag> instances,
populating the given instance.

        internal static class Extensions
        {
            internal static void FromString(this List<Tag> list, string
tags)
            {
                List<Tag> newTags = (List<Tag>)new
TagsConverter().ConvertFromString(tags);

                list.AddRange(newTags);
            }
        }

The above will make it possible to use the following syntax:

        List<Tag> tags = new List<Tags>();

        tags.FromString("tag, another, test"); // Note that FromString does
not return anything, but populates the list.

You might consider clearing the existing list in the FromString method body
or avoid adding duplicates.

For every other .NET versions you can create a helper class with a static
method similar to the extension one.

HTH,
--
Stanimir Stoyanov
Microsoft MVP -- Visual C#http://stoyanoff.info


I created a TypeConverter to convert a List<Tag> to a from String.
For example, List<Tag> to String all I need to do is:
String tags = MyListTags.ToString();
But how to make the inverse conversion? From String to List<Tag>?
I would need something like FromString ...
I am just not sure how to apply my converter in this case.
This is my Type Converter code:
 public class TagsConverter : TypeConverter {
   // CanConvertFrom
   public override Boolean CanConvertFrom(ITypeDescriptorContext
context, Type sourceType) {
     return sourceType == typeof(IList<Tag>) ? true :
base.CanConvertFrom(context, sourceType);
   } // CanConvertFrom
   // CanConvertTo
   public override Boolean CanConvertTo(ITypeDescriptorContext
context, Type destinationType) {
     return destinationType == typeof(String) ? true :
base.CanConvertTo(context, destinationType);
   } // CanConvertTo
   // ConvertFrom
   public override Object ConvertFrom(ITypeDescriptorContext context,
CultureInfo culture, Object value) {
     if (value is String) {
       String tags = (String)value;
       return tags.Split(new char[] { ',' },
StringSplitOptions.RemoveEmptyEntries).Select(t => new Tag() { Name =
t.Trim() }).ToList();
     }
     return base.ConvertFrom(context, culture, value);
   }
   // ConvertTo
   public override Object ConvertTo(ITypeDescriptorContext context,
CultureInfo culture, Object value, Type destinationType) {
     if (value is List<Tag>) {
       List<Tag> tags = (List<Tag>)value;
       return String.Join(", ", tags.Select(t => t.Name).ToArray());
     }
     return base.ConvertTo(context, culture, value, destinationType);
   } // ConvertTo
 } // TagsConverter
And I am registering it as follows:
TypeDescriptor.AddAttributes(typeof(List<Tag>), new
TypeConverterAttribute(typeof(TagsConverter)));
Thanks,
Miguel

Thank You Stanimir,

I am using NET 3.5 and I was looking exactly for an extension but I
was not being able to create it.
I was trying to create a generic extension List<T> to convert any list
to a String.
This way any list I have and that I apply ToString I would fire the
correspondent convert since there is not ToString in List.

Is this possible?

Thanks,
Miguel
 
S

Stanimir Stoyanov \(C# MVP\)

I was trying to create a generic extension List said:
to a String.
This way any list I have and that I apply ToString I would fire the
correspondent convert since there is not ToString in List.

Is this possible?

If I understand correctly, you want to write a ToString() extension method
as with FromString(string). In this case, you have to use another name (e.g.
ToTagString()) *or* method signature because the List<Tag> class already has
a parameterless ToString() method inherited from System.Object.

It would still use TagsConverter to perform the conversion but this will
this help you writer cleaner code, avoiding redundancies.

--
Stanimir Stoyanov
Microsoft MVP -- Visual C#
http://stoyanoff.info

Hello Miguel,

If you are compiling against C# 3.5 or later, it is easy to write an
extension method called FromString accessible from all List<Tag>
instances,
populating the given instance.

internal static class Extensions
{
internal static void FromString(this List<Tag> list, string
tags)
{
List<Tag> newTags = (List<Tag>)new
TagsConverter().ConvertFromString(tags);

list.AddRange(newTags);
}
}

The above will make it possible to use the following syntax:

List<Tag> tags = new List<Tags>();

tags.FromString("tag, another, test"); // Note that FromString does
not return anything, but populates the list.

You might consider clearing the existing list in the FromString method
body
or avoid adding duplicates.

For every other .NET versions you can create a helper class with a static
method similar to the extension one.

HTH,
--
Stanimir Stoyanov
Microsoft MVP -- Visual C#http://stoyanoff.info


I created a TypeConverter to convert a List<Tag> to a from String.
For example, List<Tag> to String all I need to do is:
String tags = MyListTags.ToString();
But how to make the inverse conversion? From String to List<Tag>?
I would need something like FromString ...
I am just not sure how to apply my converter in this case.
This is my Type Converter code:
public class TagsConverter : TypeConverter {
// CanConvertFrom
public override Boolean CanConvertFrom(ITypeDescriptorContext
context, Type sourceType) {
return sourceType == typeof(IList<Tag>) ? true :
base.CanConvertFrom(context, sourceType);
} // CanConvertFrom
// CanConvertTo
public override Boolean CanConvertTo(ITypeDescriptorContext
context, Type destinationType) {
return destinationType == typeof(String) ? true :
base.CanConvertTo(context, destinationType);
} // CanConvertTo
// ConvertFrom
public override Object ConvertFrom(ITypeDescriptorContext context,
CultureInfo culture, Object value) {
if (value is String) {
String tags = (String)value;
return tags.Split(new char[] { ',' },
StringSplitOptions.RemoveEmptyEntries).Select(t => new Tag() { Name =
t.Trim() }).ToList();
}
return base.ConvertFrom(context, culture, value);
}
// ConvertTo
public override Object ConvertTo(ITypeDescriptorContext context,
CultureInfo culture, Object value, Type destinationType) {
if (value is List<Tag>) {
List<Tag> tags = (List<Tag>)value;
return String.Join(", ", tags.Select(t => t.Name).ToArray());
}
return base.ConvertTo(context, culture, value, destinationType);
} // ConvertTo
} // TagsConverter
And I am registering it as follows:
TypeDescriptor.AddAttributes(typeof(List<Tag>), new
TypeConverterAttribute(typeof(TagsConverter)));
Thanks,
Miguel

Thank You Stanimir,

I am using NET 3.5 and I was looking exactly for an extension but I
was not being able to create it.
I was trying to create a generic extension List<T> to convert any list
to a String.
This way any list I have and that I apply ToString I would fire the
correspondent convert since there is not ToString in List.

Is this possible?

Thanks,
Miguel
 
S

Stanimir Stoyanov \(C# MVP\)

I was trying to create a generic extension List said:
to a String.
This way any list I have and that I apply ToString I would fire the
correspondent convert since there is not ToString in List.

Is this possible?

If I understand correctly, you want to write a ToString() extension method
as with FromString(string). In this case, you have to use another name (e.g.
ToTagString()) *or* method signature because the List<Tag> class already has
a parameterless ToString() method inherited from System.Object.

It would still use TagsConverter to perform the conversion but this will
this help you writer cleaner code, avoiding redundancies.

--
Stanimir Stoyanov
Microsoft MVP -- Visual C#
http://stoyanoff.info

Hello Miguel,

If you are compiling against C# 3.5 or later, it is easy to write an
extension method called FromString accessible from all List<Tag>
instances,
populating the given instance.

internal static class Extensions
{
internal static void FromString(this List<Tag> list, string
tags)
{
List<Tag> newTags = (List<Tag>)new
TagsConverter().ConvertFromString(tags);

list.AddRange(newTags);
}
}

The above will make it possible to use the following syntax:

List<Tag> tags = new List<Tags>();

tags.FromString("tag, another, test"); // Note that FromString does
not return anything, but populates the list.

You might consider clearing the existing list in the FromString method
body
or avoid adding duplicates.

For every other .NET versions you can create a helper class with a static
method similar to the extension one.

HTH,
--
Stanimir Stoyanov
Microsoft MVP -- Visual C#http://stoyanoff.info


I created a TypeConverter to convert a List<Tag> to a from String.
For example, List<Tag> to String all I need to do is:
String tags = MyListTags.ToString();
But how to make the inverse conversion? From String to List<Tag>?
I would need something like FromString ...
I am just not sure how to apply my converter in this case.
This is my Type Converter code:
public class TagsConverter : TypeConverter {
// CanConvertFrom
public override Boolean CanConvertFrom(ITypeDescriptorContext
context, Type sourceType) {
return sourceType == typeof(IList<Tag>) ? true :
base.CanConvertFrom(context, sourceType);
} // CanConvertFrom
// CanConvertTo
public override Boolean CanConvertTo(ITypeDescriptorContext
context, Type destinationType) {
return destinationType == typeof(String) ? true :
base.CanConvertTo(context, destinationType);
} // CanConvertTo
// ConvertFrom
public override Object ConvertFrom(ITypeDescriptorContext context,
CultureInfo culture, Object value) {
if (value is String) {
String tags = (String)value;
return tags.Split(new char[] { ',' },
StringSplitOptions.RemoveEmptyEntries).Select(t => new Tag() { Name =
t.Trim() }).ToList();
}
return base.ConvertFrom(context, culture, value);
}
// ConvertTo
public override Object ConvertTo(ITypeDescriptorContext context,
CultureInfo culture, Object value, Type destinationType) {
if (value is List<Tag>) {
List<Tag> tags = (List<Tag>)value;
return String.Join(", ", tags.Select(t => t.Name).ToArray());
}
return base.ConvertTo(context, culture, value, destinationType);
} // ConvertTo
} // TagsConverter
And I am registering it as follows:
TypeDescriptor.AddAttributes(typeof(List<Tag>), new
TypeConverterAttribute(typeof(TagsConverter)));
Thanks,
Miguel

Thank You Stanimir,

I am using NET 3.5 and I was looking exactly for an extension but I
was not being able to create it.
I was trying to create a generic extension List<T> to convert any list
to a String.
This way any list I have and that I apply ToString I would fire the
correspondent convert since there is not ToString in List.

Is this possible?

Thanks,
Miguel
 
S

Stanimir Stoyanov \(C# MVP\)

Thanks, Pete, this is a valid point.

At first I was contemplating suggesting a string extension method, for which
FromString() would be a suitable name. However, I eventually decided that
extending the string class solely for use with List<Tag> would be a bad
design.

--
Stanimir Stoyanov
Microsoft MVP -- Visual C#
http://stoyanoff.info

Peter Duniho said:
[...]
tags.FromString("tag, another, test"); // Note that FromString
does not return anything, but populates the list.

IMHO, the fact that the method doesn't return something is a very good
reason to not call it "FromString()". For the method you posted, I would
name it "AddFromString()", since that's what it does. If you clear the
List<T> first, maybe "CopyFromString()" would be better.

The name "FromString()" strongly implies that the method will return a new
List<T> instance.

Pete
 
S

Stanimir Stoyanov \(C# MVP\)

Thanks, Pete, this is a valid point.

At first I was contemplating suggesting a string extension method, for which
FromString() would be a suitable name. However, I eventually decided that
extending the string class solely for use with List<Tag> would be a bad
design.

--
Stanimir Stoyanov
Microsoft MVP -- Visual C#
http://stoyanoff.info

Peter Duniho said:
[...]
tags.FromString("tag, another, test"); // Note that FromString
does not return anything, but populates the list.

IMHO, the fact that the method doesn't return something is a very good
reason to not call it "FromString()". For the method you posted, I would
name it "AddFromString()", since that's what it does. If you clear the
List<T> first, maybe "CopyFromString()" would be better.

The name "FromString()" strongly implies that the method will return a new
List<T> instance.

Pete
 
C

Cor Ligthert[MVP]

Miquel,

Be aware that ToString() is not direct a type converter, it is a property
which returns the name of the Type as it is not overridden. With all value
types it is overridden to return the value as a string, but it can be
overridden in any other way.

Cor
 
C

Cor Ligthert[MVP]

Miquel,

Be aware that ToString() is not direct a type converter, it is a property
which returns the name of the Type as it is not overridden. With all value
types it is overridden to return the value as a string, but it can be
overridden in any other way.

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top