|
Hi all
I have to convert a string into a System.Decimal instance, using the System.convert.ChangeType method.
The problem is that I would like to make the conversion parameterizable, because the inputs come from different places and formats change with the source
I have configuration files telling how to parse a given source. For dates, I use the CultureInfo specified as strings in the config files (en-US, for instance).
This works fine with the majority of numbers, but some numbers come with parenthesis surrounding them (to indicate negative values). This is not understandable to Convert.ChangeType method.
So, I need a way to continue using the same method, but I will need to provide my own IFormatProvider implementation. I looked at the documentation, but I can only see an example returning an ICustomFormatter instance, which is used to convert an object to a string.
What I need is an implementation that would return something usable by Convert.ChangeType . Any article, tutorial on how to do this?
Thank you,
Michel
-----
If atheism is a religion, then not collecting stamps is a hobby. -- Unknown
God is the only being who, to rule, does not need to exist. -- Charles Baudelaire
|
|
|
|
|
|
Hehe I expected an answer like this one. My problem is that I don't want to be limited to a given type. Also, it could be specified as an integer, a long, etc.
I don't want to have an ugly switch handling all the cases.
-----
If atheism is a religion, then not collecting stamps is a hobby. -- Unknown
God is the only being who, to rule, does not need to exist. -- Charles Baudelaire
|
|
|
|
|
Hmmm...don't know of any way to do what you want using ChangeType . I assume you're using the Convert.ChangeType Method (Object, Type, IFormatProvider) overload of the method? Have you tried passing in a NumberFormatInfo object as the IFormatProvider ?
|
|
|
|
|
Yes, I tried that. It does not work with numbers surrounded with parenthesis and that class does not take a NumbersStyles argument or property.
-----
If atheism is a religion, then not collecting stamps is a hobby. -- Unknown
God is the only being who, to rule, does not need to exist. -- Charles Baudelaire
|
|
|
|
|
Those are the only things I know that might work. You may end up needing to create your own version of a ChangeType class that knows how to handle your special cases.
|
|
|
|
|
I think you can do this with TypeConverter s, but I am not sure...
[ My Blog] "Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
|
|
|
|
|
Hi!
I want to create a list box and its items should include a text and a picture (icon).
Has anybody an idea how to realize that?
regards
Gernot
|
|
|
|
|
You would need to subclass the ListBox and provide your own drawing routines. Have you looked[^] here on CP?
|
|
|
|
|
hi
here is my code
first My Listbox1 has a name panellist
second make sure to set PanelList.DrawMode = DrawMode.OwnerDrawVariable;
third fill the list with with 5 items
NOTE : this code is not full example it's just and right idea
private void PanelList_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)<br />
{<br />
SolidBrush br = new SolidBrush(Color.Navy );<br />
Image img = PicFavorites.Image;<br />
StringFormat sf = new StringFormat();<br />
sf.Alignment = StringAlignment.Far ;<br />
sf.LineAlignment = StringAlignment.Center ;<br />
e.DrawBackground();<br />
br.Color = Color.Navy ;<br />
e.Graphics.DrawString(PanelList.Items [e.Index].ToString () ,e.Font ,br,e.Bounds ,sf );<br />
switch (e.Index )<br />
{<br />
case 1:<br />
{<br />
img = PicGeneral.Image;<br />
}<br />
break;<br />
case 2:<br />
{<br />
img = PicEditView.Image;<br />
}<br />
break;<br />
case 3:<br />
{<br />
img = PicIncomingCalls.Image;<br />
}<br />
break;<br />
case 4:<br />
{<br />
img = PicServices.Image;<br />
}<br />
break;<br />
case 5:<br />
{<br />
img = PicSetting.Image;<br />
}<br />
break;<br />
default:<br />
break;<br />
}<br />
e.Graphics.DrawImage (img ,e.Bounds.X + 10, e.Bounds.Y );<br />
e.DrawFocusRectangle();<br />
<br />
}<br />
<br />
private void PanelList_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e)<br />
{<br />
e.ItemHeight = 48;<br />
}
I know nothing , I know nothing
|
|
|
|
|
Does anyone know how to set the readonly attribute of an class property at runtime.
Code example:
Class Myclass
{
private int x=0;
private int y=0;
[readonly(true),Description("Set X value")]
public int X
{
get{return x;}
set{x=value;}
}
[readonly(false),Description("Set Y value")]
public int Y
{
get{return y;}
set{y=value;}
}
}
|
|
|
|
|
By definition you cant. The nature of Attributes means that they rely on being just metadata.
I think you are also confusing the meaning of the ReadOnly attribute - it is used to tell the IDE that the property should not appear in the Properties Grid.
|
|
|
|
|
Confusing with the meaning!!! Well, not really...
by doing some searching on the site. i found what i was looking for.
http://www.codeproject.com/useritems/dynamicattributes.asp[^]
But you are right, "by definition" its only an metadata.
What i needed was, an simple function for the propertyGrid to enable and disable an field...
So all in all, its manageable...:-> :-> :->
|
|
|
|
|
PawJershauge wrote: What i needed was, an simple function for the propertyGrid to enable and disable an field...
PawJershauge wrote: Confusing with the meaning!!! Well, not really...
Yes really, your original post makes no mention of the PropertyGrid. The code I posted works and directly answered the question from your original post.
This is CodeProject.com not Prognostication.com.
|
|
|
|
|
well im not going to argue with you, but i did write the attributes in my class, and thats what i wanted to change...
case closed...
|
|
|
|
|
PawJershauge wrote: Does anyone know how to set the readonly attribute of an class property at runtime.
Is this a trick question?
public class Myclass
{
private int x = 0;
private int y = 0;
[ReadOnly(true), Description("Set X value")]
public int X
{
get { return x; }
set { x = value; }
}
[ReadOnly(false), Description("Set Y value")]
public int Y
{
get { return y; }
set { y = value; }
}
}
Myclass mc = new Myclass();
mc.X = 10;
Debug.WriteLine(String.Format("Myclass.X: {0}", mc.X));
|
|
|
|
|
No its not.
Please look at my reply above...
|
|
|
|
|
I'm remaking a game I made in C#, it was originally in C++ using Borland BGI Graphics. I made an array of picture boxes but the picture boxes won't show up (using Visual Studio 2005 Pro):
Code:
<br />
<br />
const Byte MAX_ROWS = 20;<br />
const Byte MAX_COLS = 20;<br />
PictureBox[,] PicBoxes = new System.Windows.Forms.PictureBox[MAX_ROWS, MAX_COLS];<br />
<br />
private void draw_image(Byte type, int row, int col)<br />
{<br />
PicBoxes[0, 0].Name = "pictureBox1";<br />
PicBoxes[0, 0].Image = Bulldozer_2._0.Properties.Resources.bulldozer_down;<br />
PicBoxes[0, 0].Size = new System.Drawing.Size(32, 32);<br />
PicBoxes[0, 0].Visible = true;<br />
PicBoxes[0, 0].Enabled = true;<br />
PicBoxes[0, 0].IsAccessible = true;<br />
PicBoxes[0, 0].Show();<br />
PicBoxes[0, 0].Location = new System.Drawing.Point(5, 5);<br />
}<br />
When I put a breakpoint on the first line in the "draw_image" function the PicBoxes array shows up as locked. How do I unlock it?
-- modified at 21:15 Tuesday 28th August, 2007
|
|
|
|
|
Jordanwb wrote: the picture boxes won't show up
Where would they show up? You haven't added them to a form.
Jordanwb wrote: the PicBoxes array shows up as locked
What do you mean by that? Where does it show up? In what way is it "locked"?
---
single minded; short sighted; long gone;
|
|
|
|
|
|
Jordanwb wrote: Well the first part you'd find out by looking at the code.
What do you mean by that?
I explained why the controls doesn't show up. Is it something about it that you don't understand?
Jordanwb wrote: The second part is hard to explain.
Well, could you try? I can't really help you if I have no idea what you are talking about...
Jordanwb wrote: The Image property is set a few lines up but it's still set to null.
Then obviously you set it to null. What does Picture_Refs contain?
The code that is visible in the picture is quite different from the code in your original post. What does the code look like really?
---
single minded; short sighted; long gone;
|
|
|
|
|
I gave a link in my previous post where you can download the project and see for yourself.
|
|
|
|
|
Jordanwb wrote: I gave a link in my previous post where you can download the project and see for yourself.
See what? I have no idea what you are talking about. There is no such thing as a "locked" array, so you have to explain what you mean by that. I could try out your project, but if I don't know what to look for, how do you expect me to see it?
---
single minded; short sighted; long gone;
|
|
|
|
|
Hi
I am new to cSharp and i would like to know how to generate a random number?? what code would i need to use??
I am aware of a method 'public virtual int Next();' However is there a easier way round this?
Any help is much appreciated.
Cheers
|
|
|
|
|
...you seem to be new to more than just C# ..a simple search of "C# random" on the MSDN returns this at the top of their results..quite ironic I might add. I went to the MSDN to answer your question and Microsoft sent me back here lol. The solution provided in that article is probably overkill to your original question, but it is a great source for learning about random number generation
"I need build Skynet. Plz send code"
|
|
|
|