[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Re-Usable Property values?

Posted on 2006-10-24
5
Medium Priority
?
162 Views
Last Modified: 2012-05-05
I swear I read about this at some point in time but haven't had a great use for it until now and I can't seem to find anything on it anymore.

I want to re-use a SaveFileDialog and an OpenFileDialog.  The initialization properties for both classes are nearly identical.  Is there a way to create a class or a structure or other that could be used to fill the properties?

for example
Public Struct FileDialog
    Filter = "XML Files (*.xml)|*.xml"
    DefaultExt = "XML"
    InitialDirectory = "c:\"
end Struct

Dim SaveFileDialog1 as new SaveFileDialog
Dim OpenFileDialog1 as new OpenFileDialog

SaveFileDialog.Properties = FileDialog 'I just made that up =)

OpenFileDialog.Properties = FileDialog 'The ability to re-use for different objects and to have multiple pre-sets for a single object

TIA
Corey2
0
Comment
Question by:Corey Scheich
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 9

Expert Comment

by:jrscherer
ID: 17801094
You can not create your own derived class from OpenFileDialog or SaveFileDialog since both are declared NonInheritable.
Otherwise it would have been easy to include a structure into the inherited class.

Jack.net
0
 
LVL 13

Accepted Solution

by:
newyuppie earned 1000 total points
ID: 17801143
why not make methods that will set it for you

Public Sub SaveFileDialog_XMLProperty(ByRef sfd as SaveFileDialog)
  With sfd
    .Filter = "XML Files (*.xml)|*.xml"
    .DefaultExt = "XML"
    .InitialDirectory = "c:\"
  End With
End Sub

and call that from wherever you need:
SaveFileDialog_XMLProperty(SaveFileDialog1)
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 1000 total points
ID: 17801182
Here is a more generic approach using a HashTable to store key/value pairs and the CallByName() function:

(Reflection could also be used here...)

    Private presets As New Hashtable

    Private Sub TestForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        presets.Add("Filter", "XML Files (*.xml)|*.xml")
        presets.Add("DefaultExt", "XML")
        presets.Add("InitialDirectory", "c:\")
    End Sub

    Public Sub SetProperties(ByVal obj As Object, ByVal values As Hashtable)
        For Each de As DictionaryEntry In values
            Try
                CallByName(obj, de.Key, CallType.Let, de.Value)
            Catch ex As Exception
            End Try
        Next
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim SaveFileDialog1 As New SaveFileDialog
        Dim OpenFileDialog1 As New OpenFileDialog
        SetProperties(SaveFileDialog1, presets)
        SetProperties(OpenFileDialog1, presets)
    End Sub
0
 
LVL 13

Author Comment

by:Corey Scheich
ID: 17816180
Idle_Mind do you have any examples for using reflection in this manner?  The examples here are likely simpler but I would like to know incase it fits this or another situation better.  Thanks.
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 1000 total points
ID: 17824601
Sure...

    Public Sub SetProperties(ByVal obj As Object, ByVal values As Hashtable)
        Dim T As Type = obj.GetType
        Dim P As System.Reflection.PropertyInfo
        For Each de As DictionaryEntry In values
            Try
                P = T.GetProperty(de.Key)
                P.SetValue(obj, de.Value, Nothing)
            Catch ex As Exception
            End Try
        Next
    End Sub
0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question