Results 1 to 11 of 11

Thread: how can you get the default value of a property?

  1. #1

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    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!!

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Did you check this method in Attribute class !!
    VB Code:
    1. DefaultValueAttribute

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I think you'd have to create a new instance of the container of the property and then find its value.

  4. #4

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    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!!

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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:
    1. Public Class Test
    2.  
    3.     Private _name As String = "Test"
    4.  
    5.     Public Property Name() As String
    6.         Get
    7.             Return _name
    8.         End Get
    9.         Set(ByVal Value As String)
    10.             _name = Value
    11.         End Set
    12.     End Property
    13.  
    14.     'this doesn't have to by in the class I just didn't know where to put it for the example
    15.     Public Shared Function GetDefault(ByVal type As Type, ByVal member As String) As Object
    16.         'get new instance
    17.         Dim newInst As Object = Activator.CreateInstance(type)
    18.         'get member
    19.         Dim bf As Reflection.BindingFlags = Reflection.BindingFlags.IgnoreCase Or _
    20.             Reflection.BindingFlags.Instance Or Reflection.BindingFlags.Public Or _
    21.             Reflection.BindingFlags.NonPublic
    22.         Dim minfo As Reflection.MemberInfo() = type.GetMember(member, bf)
    23.         If minfo.Length > 0 Then
    24.             'now get the value
    25.             If minfo(0).MemberType = Reflection.MemberTypes.Field Then
    26.                 Return CType(minfo(0), Reflection.FieldInfo).GetValue(newInst)
    27.             ElseIf minfo(0).MemberType = Reflection.MemberTypes.Property Then
    28.                 Return CType(minfo(0), Reflection.PropertyInfo).GetValue(newInst, Nothing)
    29.             Else
    30.                 Throw New ArgumentException("Member is the wrong type!")
    31.             End If
    32.         Else
    33.             Throw New ArgumentException("Member not found!")
    34.         End If
    35.  
    36.     End Function
    37.  
    38. End Class
    39.  
    40. 'syntax
    41.         Dim t As New Test
    42.         t.Name = "Ed"
    43.  
    44.         MsgBox(String.Format("Original: {1}{0}Default: {2}", ControlChars.NewLine, t.Name, Test.GetDefault(t.GetType, "_name").ToString))

  6. #6

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    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!!

  7. #7
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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());

  8. #8

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    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!!

  9. #9
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by MrPolite
    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
    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:
    1. Sub TypeOfEx()
    2.         Dim c As New TextBox
    3.         If TypeOf c Is TextBox Then
    4.             MessageBox.Show("textbox")
    5.         ElseIf TypeOf c Is ComboBox Then
    6.             MessageBox.Show("whatever")
    7.         Else
    8.             MessageBox.Show("go home")
    9.         End If
    10.  
    11.     End Sub

  10. #10

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    thanks... I know I shouldnt be making posts like THIS one but its hard to resist: you have 6000 posts now congrats
    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!!

  11. #11
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by MrPolite
    thanks... I know I shouldnt be making posts like THIS one but its hard to resist:
    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 .


    you have 6000 posts now congrats
    ----------

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width