lets say you declare a property and set its defaultValue attribute to something. Is there a way to get this value using code?:rolleyes:
Printable View
lets say you declare a property and set its defaultValue attribute to something. Is there a way to get this value using code?:rolleyes:
Did you check this method in Attribute class !!
VB Code:
DefaultValueAttribute
I think you'd have to create a new instance of the container of the property and then find its value.
hmm what's the container of the property?:DQuote:
Originally posted by Edneeis
I think you'd have to create a new instance of the container of the property and then find its value.
Pirate how can you get the value using that thing?:confused:
What is the container of the property? What is housing it?
Is it in a Class? Form? Module? Or it doesn't really matter as long as you have a reference to the container or know the strong type of the container.
Here is an example using a private field in a class:
VB Code:
Public Class Test Private _name As String = "Test" Public Property Name() As String Get Return _name End Get Set(ByVal Value As String) _name = Value End Set End Property 'this doesn't have to by in the class I just didn't know where to put it for the example Public Shared Function GetDefault(ByVal type As Type, ByVal member As String) As Object 'get new instance Dim newInst As Object = Activator.CreateInstance(type) 'get member Dim bf As Reflection.BindingFlags = Reflection.BindingFlags.IgnoreCase Or _ Reflection.BindingFlags.Instance Or Reflection.BindingFlags.Public Or _ Reflection.BindingFlags.NonPublic Dim minfo As Reflection.MemberInfo() = type.GetMember(member, bf) If minfo.Length > 0 Then 'now get the value If minfo(0).MemberType = Reflection.MemberTypes.Field Then Return CType(minfo(0), Reflection.FieldInfo).GetValue(newInst) ElseIf minfo(0).MemberType = Reflection.MemberTypes.Property Then Return CType(minfo(0), Reflection.PropertyInfo).GetValue(newInst, Nothing) Else Throw New ArgumentException("Member is the wrong type!") End If Else Throw New ArgumentException("Member not found!") End If End Function End Class 'syntax Dim t As New Test t.Name = "Ed" MsgBox(String.Format("Original: {1}{0}Default: {2}", ControlChars.NewLine, t.Name, Test.GetDefault(t.GetType, "_name").ToString))
thanks a lot Edneeis.... I'll have to think on that one. its in a class btw. I dont have a good understanding of all these reflections and such:(
Quote:
Originally posted by MrPolite
Pirate how can you get the value using that thing?:confused:
Code:private bool myVal=false;
[DefaultValue(false)]
public bool MyProperty {
get {
return myVal;
}
set {
myVal=value;
}
}
// Gets the attributes for the property.
AttributeCollection attributes =
TypeDescriptor.GetProperties(this)["MyProperty"].Attributes;
/* Prints the default value by retrieving the DefaultValueAttribute
* from the AttributeCollection. */
DefaultValueAttribute myAttribute =
(DefaultValueAttribute)attributes[typeof(DefaultValueAttribute)];
Console.WriteLine("The default value is: " + myAttribute.Value.ToString());
w00t works:D a bit simpler than Edneeis'version but still what a mess both of them are:D I dont understand any of it. Where did you learn all that ha?;) heheQuote:
Originally posted by Pirate
Code:private bool myVal=false;
[DefaultValue(false)]
public bool MyProperty {
get {
return myVal;
}
set {
myVal=value;
}
}
// Gets the attributes for the property.
AttributeCollection attributes =
TypeDescriptor.GetProperties(this)["MyProperty"].Attributes;
/* Prints the default value by retrieving the DefaultValueAttribute
* from the AttributeCollection. */
DefaultValueAttribute myAttribute =
(DefaultValueAttribute)attributes[typeof(DefaultValueAttribute)];
Console.WriteLine("The default value is: " + myAttribute.Value.ToString());
edit: btw what does typeof do in vb.net? I know you're supposed to use getType instead of the C# typeOf() but still if you type "typeOf" in VB it highlights it in blue so I'm wondering if it really means anything in vb or if its just reserved
Always consult your MSDN in your area . :)Quote:
Originally posted by MrPolite
w00t works:D a bit simpler than Edneeis'version but still what a mess both of them are:D I dont understand any of it. Where did you learn all that ha?;) hehe
It seems to be only for comparsion issues. Ex .Quote:
edit: btw what does typeof do in vb.net? I know you're supposed to use getType instead of the C# typeOf() but still if you type "typeOf" in VB it highlights it in blue so I'm wondering if it really means anything in vb or if its just reserved
VB Code:
Sub TypeOfEx() Dim c As New TextBox If TypeOf c Is TextBox Then MessageBox.Show("textbox") ElseIf TypeOf c Is ComboBox Then MessageBox.Show("whatever") Else MessageBox.Show("go home") End If End Sub
:) thanks... I know I shouldnt be making posts like THIS one but its hard to resist: you have 6000 posts now:D:D:D congrats;)
If it didn't help you , then I'm sure it would be useful for some other guys who ever wanted to do something like this .Quote:
Originally posted by MrPolite
:) thanks... I know I shouldnt be making posts like THIS one but its hard to resist:
:D ----------:pQuote:
you have 6000 posts now:D:D:D congrats;)