Jump to content
Xtreme .Net Talk

rbulph

Avatar/Signature
  • Posts

    398
  • Joined

  • Last visited

Everything posted by rbulph

  1. This seems to work well: Friend Function CrossCast(Of T As New)(ByVal source As Object) As T 'Loop to find the first shared parent type of T and source. Dim sharedparenttype As Type = GetType(T) Do Until sharedparenttype.BaseType Is Nothing Or source.GetType.IsSubclassOf(sharedparenttype) sharedparenttype = sharedparenttype.BaseType Loop 'This is the resulting object, which is an instance of the base class. Dim Result As New T 'Now we will loop through each field in the BASE CLASS and copy the value from 'the source object to the resulting object. '(We need full trust because we must copy private members, which can't be accessed in partial trust contexts.) 'If sharedparenttype is Object there will be no fields to loop through. For Each field As FieldInfo In sharedparenttype.GetFields( _ BindingFlags.Public Or BindingFlags.NonPublic Or BindingFlags.FlattenHierarchy Or BindingFlags.Instance) field.SetValue(Result, field.GetValue(source)) Next Return Result End Function
  2. If you believe OO "doesn't go" here, try the following. Add a menustrip to a form. Add a menu item. Through the Properties Window, set its margin on the left to 25 pixels (say). Right click on the menustrip to get the context menu. Select Convert To/TextBox. The ToolStripMenuItem object has now converted to a ToolStripTextBox and you will see that it has kept its left margin. Now examine these controls in the Object Browser. The Margins property is a property of the Component class, from which both of these controls inherit, indirectly. They don't hold references to a Component object, they inherit from it. This sort of conversion is exactly what I want to do. I wonder how Microsoft goes about it?
  3. No, I really don't want that sort of structure. For one thing, I'm displaying the objects in a propertygrid. If AClass and BClass hold references to an instance of baseclass instead of inheriting from it then I have to design a class inheriting from ExpandableObjectConverter to deal with the baseclass in the propertygrid. I neither want the hassle of doing that, nor the different appearance that it will give in the propertygrid.
  4. OK, here's a related but different situation. Supposing your object model has a Meal baseclass with a MeatType property. It's inherited by two subclasses, Rice and Noodles. You've got an order for Rice with Pork. Then the customer changes his mind and says he wants noodles instead of rice. You want to change the order object from a Rice one to a Noodles one without losing the Pork property. That seems a realistic possibility to me. So here's an adaptation of marble_eater's code to do that. I tried, but failed, to find a test I could carry out that would save me from having to catch errors for each property that didn't match. If you have any ideas how to do that then I'd be glad to hear them. Imports System.Reflection Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim j As New AClass j.BaseProperty = 12 j.AProp = "ioh" j.DuplicatedProp = 123 Dim u As BClass u = CrossCast(Of BClass)(j) Text = u.BaseProperty 'text shows 12 MsgBox(u.DuplicatedProp) 'shows 0, as I would want. End Sub Friend Function CrossCast(Of T As New)(ByVal source As Object) As T 'A System.Type that represents the base type we are converting to 'which we can use for reflection. Dim targetType As Type = GetType(T) 'Don't think there's any need to throw errors if they don't inherit from a common base. They will inherit from object in any case. 'This is the resulting object, which is an instance of the base class. Dim Result As New T 'Now we will loop through each field in the BASE CLASS and copy the value from 'the source object to the resulting object. '(We need full trust because we must copy private members, which can't be accessed in partial trust contexts.) For Each field As FieldInfo In targetType.GetFields( _ BindingFlags.Public Or BindingFlags.NonPublic Or BindingFlags.FlattenHierarchy Or BindingFlags.Instance) Try field.SetValue(Result, field.GetValue(source)) Catch End Try Next Return Result End Function End Class <Serializable()> Friend Class baseclass Friend BaseProperty As Long End Class <Serializable()> Friend Class AClass Inherits baseclass Friend AProp As String Friend DuplicatedProp As Long End Class <Serializable()> Friend Class BClass Inherits baseclass Friend BProp As Date Friend DuplicatedProp As Long End Class
  5. Thanks. The following shows how to get the info I need in VB: Dim sName As String = My.Computer.FileSystem.GetName(strDLLs(0)) Dim sCompanyName As String Dim aca() As System.Reflection.AssemblyCompanyAttribute = objDLL.GetCustomAttributes(GetType(AssemblyCompanyAttribute), True) If Not aca Is Nothing Then sCompanyName = aca(0).Company Dim sDate As String = FileSystem.FileDateTime(strDLLs(0)).Date
  6. How do I get hold of the properties of an dll? I want to know its name, the date it was created, and the Company information, all as shown in the Properties dialogue which you get by right-clicking on it in Explorer. It's a plug-in, so I'm finding it with code like below, which means that my starting point is an Assembly object. <vb> Dim strDLLs() As String, intIndex As Integer Dim objDLL As Assembly 'Go through all DLLs in the application's directory. strDLLs = Directory.GetFileSystemEntries(Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath), "*.dll") For intIndex = 0 To strDLLs.Length - 1 Try objDLL = Assembly.LoadFrom(strDLLs(intIndex))...</vb>
  7. Thanks, yes, that does it. The bin folder for the dll does not contain debug and release folders - the dll is directly within the bin folder. So I must need that dll for both debugging and the release.
  8. rbulph

    2D Arrays

    Thanks, that's useful. I wasn't aware of the possibility of doing this. Ideally I'd like to have the facility to rearrange both rows and columns, but I don't think there's a structure that will allow me to do this. Perhaps I will just have to reposition items one by one on one axis.
  9. rbulph

    2D Arrays

    Is there a simple way to re-order the data in a 2D array by rows? e.g. in an array declared as "s(3,3) as string", move all the items with the first index as 0 to the end, and shunt the other items up on level as regards the first index.
  10. It is harder if you don't even know what a batch file is! Would you be really kind and give me an example of the sort of code I should be putting in the post build event command line?
  11. No, using the Professional Edition. Thanks for the suggestions re Build Events. I'll look into this if I have time, but I can't help thinking it would be easier for me just to copy the dll over manually each time I change it...
  12. I don't have a Configuration combobox (see attached screenshot). But anyway, when I set the build output path on the Compile tab it only sets the path for the release build, not the debug build. There's no comparable textbox on the Debug tab. Also, as I mentioned, setting the path on the Compile tab sometimes works, and sometimes is completely ignored. I can't figure out why.
  13. I'm using Visual Basic in Visual Studio 2005. This has a Compile tab under the Project Properties with a "Build Output Path" entry. Sounds like this is the thing you're talking about. I have set this to be the same for both the exe and the dll, and it's completely ignored - nothing is created in the directory I specify and the projects just build to the bin directories in the same directory as they themselves are in, as before. I don't see anywhere to set release and debug build output paths separately.
  14. Could someone please clarify to me how the build output paths in http://www.xtremedotnettalk.com/showthread.php?t=49597&highlight=build+output+path work? I would like to have the exe and dlls in the same folder on the user's computer, so how do I go about mirroring this as I develop the application and plug-ins? Do I need a folder especially for building both the host exe and plug-in dll? How does this tie in with the need to have both a debug and a release version of whatever I build?
  15. I certainly think that consistency across classes is the best thing. I may well stop using the Collection class since it's a pain to have to keep thinking "Now is this a zero based or 1 based index?". If it's always zero then that is a lot easier for me.
  16. Is it true to say that every type of grouping class (collection, array, hashtable etc.) is zero based in VB.net, except for the collection class which is 1 based? I note in particular that the IList contained in the List property of CollectionBase is 0 based. I find this a bit annoying. If I have a collection and then decide I need to make it strongly typed by having a class inheriting from CollectionBase I either have to change all item references in my code or have the following rather awkward code in the new class: Public Function item(ByVal i As Integer) As String Return MyBase.List.Item(i + 1) End Function Any thoughts?
  17. Yes, I do want this to happen on the textbox mousedown event. And yes, the tooltip is stealing/preventing it from firing. And the tooltip has no mousedown event of its own. So I'm still looking at capturing the mouse to force the textbox's mousedown event to occur.
  18. Of what? It would be a bit excessive to have to handle the mousedown event of every control in my application.
  19. OK, I didn't realise that ClientRectangle existed for controls. Shame it just gives the control's Bounds for combo boxes though.
  20. I'd like to have an autosize textbox and combobox. While these controls do have a (hidden) AutoSize property, setting this property doesn't seem to have any impact. So I have come up with the following code, which you will appreciate is not perfect. Any suggestions for improvements, especially removal of the figure 6? Public Class Form1 Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, ComboBox1.TextChanged With CType(sender, Control) Dim lbl As New Label Me.Controls.Add(lbl) lbl.Font = .Font lbl.AutoSize = True lbl.Text = .Text Dim W As Integer = lbl.Width If TypeOf sender Is ComboBox Then W = W + SystemInformation.VerticalScrollBarWidth + 6 .Width = W lbl.Dispose() End With End Sub End Class
  21. I have three alternative bits of code that I can use to refresh the property grid when a property changes other than through the user changing it through the propertygrid. These are shown below: Public Class Form1 Dim WithEvents h As New ABC Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Me.PropertyGrid1.SelectedObject = h End Sub Private Sub h_paintgrid() Handles h.paintgrid Me.PropertyGrid1.Refresh() End Sub End Class Public Class ABC Event paintgrid() Private def As Long <System.ComponentModel.RefreshProperties(System.ComponentModel.RefreshProperties.Repaint)> Public Property Def1() As Long 'Option 1 = RefreshProperties.Repaint, Option 2 = RefreshProperties.All Get Return def End Get Set(ByVal value As Long) def = value Me.Ghi1 = value & " *!$@" ' RaiseEvent paintgrid() 'Option 3 End Set End Property Private ghi As String Public Property Ghi1() As String Get MsgBox("Property Queried") Return ghi End Get Friend Set(ByVal value As String) ghi = value End Set End Property Private jkl As Boolean Public Property Jkl1() As Boolean Get Return jkl End Get Set(ByVal value As Boolean) jkl = value End Set End Property End Class Each of them works OK to refresh the value shown for Ghi1 when Def1 is changed in the propertygrid and the focus is then moved away from Def1 (to Jkl1 for instance). But I have three questions: 1. Is there really any difference between Options 1 and 2? The help files say that RefreshProperties.All means that the properties will be requeried. But I get my message box even if RefreshProperties.Repaint is set and cannot see any difference between the two options. 2. Is there any argument against just using my event based Option 3 approach? I find this the simplest; and 3. Is there any way of just having the Ghi1 item refreshed in the propertygrid rather than the whole grid being refreshed each time? That would seem more efficient, if it can be done, especially in a larger grid. EDIT: Option 3 seems to be better in fact because RefreshProperties only operates if the change is made by the user through the propertygrid, not if I make a change in code.
  22. Thanks. In fact, just setting the items in the load event of the form/control where I use JCombo seems to be the simplest way.
  23. I have a class which inherits from the combobox ("JCombo"). Its items are an ComboBox.ObjectCollection of a type of object ("Class1") which I define in my project. The items are set in the New event of JCombo. An instance of JCombo appears in a form and a user control in my application, having been added at design time. If I make any changes in Class1 then the project won't compile - I get the error "Invalid Resx file" and a reference to some code in the resx file for the form and user control which contain the JCombo. The code which is referenced looks no different to a random series of letters and numbers to me, and clearly I can't correct this. So what's the best solution to this? Do I need to delete the resx file each time I make a change to Class1? Should I set the items elsewhere?
×
×
  • Create New...