Hey is there a way to convert a string variable to the datatype Size?
开发者_StackOverflow中文版for example, if I had this variable
string tempSize;
how could I convert that to a dataType Size for reading purposes??
Which Size are you referring to?
If you mean System.Windows.Size, there is a Size.Parse(string) method.
If you mean System.Drawing.Size, there's no built in parse method, you'll have to parse the string yourself.
We're talking about the System.Drawing.Size struct as used in Winforms, yes?
As noted above, this struct doesn't have a Parse() function. So you gotta do it yourself. One option is just to format and parse a string of your own devising:
// format a custom string to encode your size
string mySizeString = string.Format("{0}:{1}", mySize.Width, mySize.Height);
// parse your custom string and make a new size
string[] sizeParts = mySizeString.Split(':');
int height = int.Parse(sizeParts[0]);
int width = int.Parse(sizeParts[1]);
Size mySize = new Size(height, width);
Another alternative is to use the TypeConverter class to do to formatting and parsing for you:
// convert a Size to a string using a type converter
TypeConverter tc = TypeDescriptor.GetConverter(typeof(Size));
string mySizeString = tc.ConvertToString(mySize);
// convert a string to a Size using a type converter
TypeConverter tc = TypeDescriptor.GetConverter(typeof(Size));
Size mySize = (Size)tc.ConvertFromString(mySizeString);
Perhaps this last solution is just the right size for you (har har). In any case I'm sure some clever person has written extension functions to extend the Size struct in this way. That would be nifty!
I've been successful calling System.Windows.Size.Parse on strings in format similar to "1100,700".
Nowadays, you can use an extension method to create a Parse method for the string class:
public static Size Parse(this string str) ...
Let me show some code that I had to write because a Size was supposed to be passed in through an URL parameter in the form WxH, e.g. 1024x768:
namespace MySize
{
public static class Extensions
{
public static Size Parse(this string str)
{
try {
var a = str.Split(new char[] { 'x' });
return new Size() {
Width = int.Parse(a[0]),
Height = int.Parse(a[1])
};
}
catch(Exception) { }
return Size.nosize;
}
}
public class Size
{
public int Width { get; set; }
public int Height { get; set; }
public override string ToString()
{
return Width.ToString() + "x" + Height.ToString();
}
public Size(System.Drawing.Size from)
{
Width = from.Width;
Height = from.Height;
}
public Size()
{
}
public static Size nosize = new Size(new System.Drawing.Size(-1, -1));
}
//[..]
}
With this, you can use the ToString() method for your class and the corresponding Parse method for string (or String). Here is a test case:
[TestMethod]
public void TestSizeParse()
{
var s1 = new Size(1024, 768);
var s1Str = s1.ToString();
Size s2 = s1Str.Parse();
Assert.AreEqual(s1.Width, s2.Width);
Assert.AreEqual(s1.Height, s2.Height);
Assert.AreEqual(s1, s2, "s1 and s2 are supposed to be equal");
}
Size is a structure with Width and Height.
You need two integers to generate it, with Size(width, height) constructor.
You should find a way to extract integers from your string using Int32.parse or some other means. Use the extracted integers to construct the Size.
maybe:
int size=Convert.ToInt32(tempSize);
updated
you have to parse tempSize in two int then call:
Size = New Drawing.Size(xSize, ySize)
I guess you have a string in the format "123; 456", as that's the format the Size struct uses.
To parse that into a System.Drawing.Size, you could do something like this:
string s = "123; 456";
int width, height;
string[] dims = s.Split(';');
int.TryParse(dims[0], out width);
int.TryParse(dims[1], out height);
System.Drawing.Size size = new System.Drawing.Size(width, height);
This will fail if the string does not have a ;
, and will return 0's for the numbers, if the strings are unparsable.
IMHO the best readable and parsable format at the moment is JSON.
So you could use the JavaScriptSerializer
to convert a Size object from and to JSON. And since the default implementation of Size.ToString() also gives a rather nice output, you can use:
var json = new JavaScriptSerializer();
var s1 = new Size(5, 125);
var serialized = s1.ToString().Replace("=",":");
Console.WriteLine(serialized);
var s2 = json.Deserialize<Size>(serialized);
Console.WriteLine(s2);
This renders the following output:
{Width:5, Height:125}
{Width=5, Height=125}
精彩评论