+ Reply to Thread
Results 1 to 28 of 28

Beginner to User Defined Data Types (Custom Type, UDTs)

Hybrid View

  1. #1
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: Beginner to User Defined Data Types (Custom Type, UDTs)

    Disadvantage 1. UDT can be public just like any other variable. You just need to scope them in the correct place.

    Public Type Car
        Make    As String
        Model   As String
        Price   As Currency
        Km      As Long
    End Type
    
    Public g_udtMyCar As Car
    
    Sub Example3()
        'use UDTs (defined at top of module)
        'e.g.
        g_udtMyCar.Make = "Unknown"
    
    ' do something
    End Sub
    Disadvantage 2. Again depends on your scope definition

    Disadvantage 3. Incorrect, you can redim and redim preserve
    Option Explicit
    
    Public Type Car
        Make    As String
        Model   As String
        Price   As Currency
        Km      As Long
    End Type
    
    Public g_udtMyCars() As Car
    Sub Example4()
        
        ReDim g_udtMyCars(1 To 3)
        
        g_udtMyCars(1).Make = "Ford"
        g_udtMyCars(2).Make = "Audi"
        g_udtMyCars(3).Make = "BMW"
        
    End Sub
    Sub Example5()
        
        ReDim Preserve g_udtMyCars(1 To 5)
        
        g_udtMyCars(4).Make = "Mercedes"
        g_udtMyCars(5).Make = "VW"
        
    End Sub
    Sub Test()
        
        Dim lngIndex As Long
        
        Example4
        Example5
        
        For lngIndex = LBound(g_udtMyCars) To UBound(g_udtMyCars)
            Debug.Print lngIndex, g_udtMyCars(lngIndex).Make
        Next
        
    End Sub
    Disadvantage 4. I don't see the use of udt against normal variables having a performance hit. Do you have any evidence of this?

    Disadvantage 5. Don't see why, you need to clarify your statement about why they are hard to use in addin.

    Disadvantage 6. No they are not.

    extends previous code example.
    Sub Test()
        
        Dim lngIndex As Long
        
        Example4
        Example5
        
        For lngIndex = LBound(g_udtMyCars) To UBound(g_udtMyCars)
            Debug.Print lngIndex, g_udtMyCars(lngIndex).Make
        Next
        
        Example6 g_udtMyCars(2)
        Example7 g_udtMyCars
        
        Debug.Print GetCar(g_udtMyCars, "Ford").Price
        
    End Sub
    Sub Example6(MyCar As Car)
        Debug.Print MyCar.Make
    End Sub
    
    
    Sub Example7(Cars() As Car)
    
        Dim lngIndex As Long
        
        For lngIndex = LBound(Cars) To UBound(Cars)
            Cars(lngIndex).Price = Int((50000 - 15000 + 1) * Rnd + 15000)
    Debug.Print Cars(lngIndex).Make, Cars(lngIndex).Price
        Next
    
    End Sub
    Function GetCar(Cars() As Car, Name As String) As Car
        Dim lngIndex As Long
        
        For lngIndex = LBound(Cars) To UBound(Cars)
            If StrComp(Cars(lngIndex).Make, Name, vbTextCompare) = 0 Then
                GetCar = Cars(lngIndex)
                Exit Function
            End If
        Next
    End Function
    Disadvantage 7. no more than stanadard variables
    Cheers
    Andy
    www.andypope.info

  2. #2
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Beginner to User Defined Data Types (Custom Type, UDTs)

    Quote Originally Posted by Andy Pope View Post
    ...
    Appreciate your detailed myth busting of UDTs disadvantages. +1
    At this rate, I may end up using them after all!

    1. (Tests code provided). :eep: Whoa. It's now working! Now why didn't it work in my current project? Will go back, test and report back.
    2. As above
    3. (dazed) That's incredible. Then what did Chip Pearson mean? See: http://www.cpearson.com/excel/classes.aspx (1/3 down the page - where he describes 3 disadvantages to Type)
    4. I was reading about UDTs having a slight impact on code performance. It involved a test that used LenB in Immediate window to measure the size of variables. But I can't remember where I read it and a quick google can't find it again
    5. This? http://stackoverflow.com/questions/1...-from-an-addin
    6. I have come across a few web pages re UDTs and arrays. Here's one: http://www.vbaexpress.com/forum/show...r-Defined-Type
    7. I concur.

    UPDATE: 1 & 2 tested in current project. Working fine now. (No idea why it played up previously)
    Last edited by mc84excel; 11-19-2013 at 10:40 PM.
    *******************************************************

    HELP WANTED! (Links to Forum threads)
    Trying to create reusable code for Custom Events at Workbook (not Application) level

    *******************************************************

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Is it possible to combine 2 User Defined Types?
    By tom.hogan in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-11-2012, 09:08 AM
  2. User-Defined Data Types
    By CrazyFileMaker in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-14-2009, 05:23 AM
  3. Editing User Defined Lists - Beginner
    By Julesy9 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-31-2006, 08:23 AM
  4. [SOLVED] Setting up a Custom type, User-defined, Default Chart
    By Kurt in forum Excel Charting & Pivots
    Replies: 0
    Last Post: 12-22-2005, 03:45 PM
  5. How do I share User-Defined Custom Chart Types in Pivot Charts?
    By jantz_question in forum Excel Charting & Pivots
    Replies: 0
    Last Post: 09-20-2005, 09:05 AM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1