1

I want to use on DefaultValue Attribute to define default value for custom class that I write in my App. the class gives in his constractor a string. I write the follow:

[DefaultValue(Type.GetType("MyClass"),"hello world")] 

but when I try to run this App. I give error:

"An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type".

can anyone explain me what the problem?

Javed Akram
  • 15,024
  • 26
  • 81
  • 118
RRR
  • 3,937
  • 13
  • 51
  • 75

3 Answers3

6

You're using Type.GetType("MyClass") where you should have typeof(MyClass).

Peter Taylor
  • 4,918
  • 1
  • 34
  • 59
  • it works, but now I can't success to give the value of this attribute. what sould I write to give it? – RRR Jan 17 '11 at 13:56
  • I think you need to explain in detail what you're trying to do. – Peter Taylor Jan 17 '11 at 14:01
  • I try to build an enum that the his values are not int or char. so I write before each value - [DefaultValue(Type.GetType("MyClass"),"11:00:00")] and in the ctor of the MyClass I parss the string. but now how can I recive for each value the value that I write in the DefaultValue Attribute? – RRR Jan 17 '11 at 14:16
  • In C# the enum values must be an integer type. http://stackoverflow.com/questions/469287/c-vs-java-enum-for-those-new-to-c might be helpful. – Peter Taylor Jan 17 '11 at 14:36
2

I suspect its the Type.GetType("MyClass");

can you try typeof(MyClass) instead, passing the type and not a string?

WraithNath
  • 17,658
  • 10
  • 55
  • 82
  • it works, but now I can't success to give the value of this attribute. what sould I write to give it? – RRR Jan 17 '11 at 13:43
  • Are you getting an error? i just read this: http://www.eggheadcafe.com/software/aspnet/32319410/defaultvalue-attribute-on-properties-of-user-controls.aspx it explains a little on when the value would be visible. I must admit I have not used DefaultValueAttributes before put I might be able to help if you can give a little more detail. The other guys on this thread may be able to help also if they see this comment – WraithNath Jan 17 '11 at 13:48
1

Type.GetType() is a method (i.e. not a constant expression), as the others said, use typeof.

[DefaultValue(typeof(MyClass),"Convertible String")]

Edit: To enable the conversion of the string to your custom class you need to associate a TypeConverter with it, see the examples-section of this documentation to get an idea of how to do so.

H.B.
  • 166,899
  • 29
  • 327
  • 400
  • it works, but now I can't success to give the value of this attribute. what sould I write to give it? – RRR Jan 17 '11 at 14:01
  • I haven't used the DefaultValue attribute much, but when i did, i never needed to give it a type, i just entered some value like this for a bool: [DefaultValue(false)], since you want to use a more complex type you should look into how it tries to convert the value, maybe your class needs to implement an interface for it to work. – H.B. Jan 17 '11 at 14:25