|
-
Feb 25th, 2004, 02:41 AM
#1
how can you get the default value of a property?
lets say you declare a property and set its defaultValue attribute to something. Is there a way to get this value using code?
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Feb 25th, 2004, 12:13 PM
#2
Sleep mode
Did you check this method in Attribute class !!
-
Feb 25th, 2004, 01:06 PM
#3
I think you'd have to create a new instance of the container of the property and then find its value.
-
Feb 25th, 2004, 07:28 PM
#4
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.
hmm what's the container of the property?
Pirate how can you get the value using that thing?
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Feb 25th, 2004, 09:47 PM
#5
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))
-
Feb 25th, 2004, 10:04 PM
#6
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
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Feb 26th, 2004, 01:38 AM
#7
Sleep mode
Originally posted by MrPolite
Pirate how can you get the value using that thing?
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());
-
Feb 26th, 2004, 01:48 AM
#8
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());
w00t works a bit simpler than Edneeis'version but still what a mess both of them are I dont understand any of it. Where did you learn all that ha? hehe
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
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Feb 26th, 2004, 01:56 AM
#9
Sleep mode
Always consult your MSDN in your area .
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
It seems to be only for comparsion issues. Ex .
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
-
Feb 26th, 2004, 03:03 AM
#10
-
Feb 26th, 2004, 03:08 AM
#11
Sleep mode
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|