Subtitute for Select/Case on a UDT
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: Subtitute for Select/Case on a UDT

  1. #1
    Join Date
    Apr 2003
    Posts
    322

    Post Subtitute for Select/Case on a UDT

    I've defined several UDTs, but need a way to detect the data type, when an instance is passed to a lower level function.
    I could just add a string type field to each UDT, but I was hoping for a less kludgy way to do this in VB.

    fso declaration and file opening/writing/closing code omitted for brevity.

    The reason I need to detect each UDT type separately, is because when writing these vars to a file, they require some special formatting, for syntax reasons in the target application.


    Private Type dblParam
    Val As Double
    name As String
    desc As String
    End Type

    sub myFunc(fso as object)

    Dim udtDouble as dblParam

    udtDouble.val = 1.1
    udtDouble.name="Frequency"
    udtDouble.desc="clock frequency"

    WriteParams(fso, udtDouble)

    end sub



    sub WriteParams(fso as object, Instance as variant)

    Select Case (VarType(Instance))

    Case dblParam <-- THIS DOESN'T COMPILE :-)
    Call WriteDouble(fso, Instance)

    'Case lngParam
    Call WriteLong(fso, Instance)

    'Case strParam
    Call WriteString(fso, Instance)

    'Case lstParam
    Call WriteList(fso, Instance)

    End Select

    End sub

  2. #2
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,982

    Re: Subtitute for Select/Case on a UDT

    Append the variable to a file for VarType(Instance) to see what the type is, or else, just put it in a CASE ELSE after all the other choices.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Apr 2003
    Posts
    322

    Re: Subtitute for Select/Case on a UDT

    Quote Originally Posted by dglienna
    Append the variable to a file for VarType(Instance) to see what the type is, or else, just put it in a CASE ELSE after all the other choices.
    I don't understand. I'm having a compile problem
    WOuld you show an example?
    thanks

  4. #4
    Join Date
    Dec 2001
    Posts
    6,332

    Re: Subtitute for Select/Case on a UDT

    It's not clear to me what you want exactly, since a UDT can have different data types as members. A UDT can be written to a file in one shot, which is obviously preferable if you can use the resulting format. However, a specific format would require dealing with each member of the UDT seporately. Is that what you are trying to achive?
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  5. #5
    Join Date
    Apr 2003
    Posts
    322

    Re: Subtitute for Select/Case on a UDT

    Quote Originally Posted by WizBang
    However, a specific format would require dealing with each member of the UDT seporately. Is that what you are trying to achive?
    Yes, this is what I'm after. I also want to use the same function for writing to a file, so once I can detect the dat type, I will add the special formatting for each dat type as a string, then pass the string to a function for writing to the file.

  6. #6
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Subtitute for Select/Case on a UDT

    I hope this is close on what your looking for...

    I'm assuming that you have files in certain formats (Lets say like Tracker formats.. Mod, S3M, FAR, 669)..

    You first need to detect the file type ( Extention or signatures...) then you can build a UDT around the format..
    Code:
     Public Type MOD_Format
        Name as string * 20
        Sample1_Name as string *16
        S1_ Start as Long
        S1_length as Long
        S1_Volume as Byte
        Other as Other_UDT
    '.... Etc
    End Type
    when ot come to writing the file (or reading for that matter) you can write to the file using the UDT directly..
    Code:
    Dim UDT_var as Mod_Format
    Open [Filename] for Random as [Filenum] Len = Len(UDT_var)
    Put [Filenumber] , [Record_No] , UDT_var
    This obviously will only work if the Different files of the same type always have the same format...

    Files of different types but with the same or simuler info can get seperate UDT's built around there uniquness, but using the same names within the UDT...

    ie
    Code:
     Public Type S3M_Format
        Filler as string * 10
        Name as string * 30
        No_samples as byte
        Sample1_Name as string *20
        S1_Volume as Byte
        S1_ Start as Long
        S1_length as Long
        Other as Other_UDT
    '.... Etc
    End Type
    and this way if your converting files from one format to another you can use.. UDT1.Name = UDT2.Name...

    If this is helpfull and you need more, let me know....

    Gremmy...
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  7. #7
    Join Date
    Apr 2003
    Posts
    322

    Re: Subtitute for Select/Case on a UDT

    Quote Originally Posted by GremlinSA
    I hope this is close on what your looking for...

    I'm assuming that you have files in certain formats (Lets say like Tracker

    You first need to detect the file type ( Extention or signatures...)

    If this is helpfull and you need more, let me know....

    Gremmy...
    >>I'm assuming that you have files in certain formats (Lets say like Tracker
    It's not the files that are in certain formats, but the variables written to a plain text file, needs to be of a certain syntax, so it can be parsed by the target application. This is why I need to format doubles, differently from longs, strings, etc.

    >>You first need to detect the file type ( Extention or signatures...)
    I can't do this-0 as posted in the original email.

    Select/case doesn't work on UDT's, and I don't know how else to detect an instance of a UDT.

    This is the fundamental problem I cannot figure out.

  8. #8
    Join Date
    Dec 2001
    Posts
    6,332

    Re: Subtitute for Select/Case on a UDT

    As far as I'm aware, there's no way to achive exactly what you want. You will have to write a function to specifically work with the UDT members one at a time. Select Case could be used to determine the type of UDT, then the data would be assembled into the desired format. The same file access code could then write the data.

    Alternatively, use the same UDT type for all the different kinds of data you have. Make all the members strings if needed, or just have the different variable types defined, though not always used. Then if you need to format each type differently, use one member to determine the formatting, so that the Select Case function can work accordingly. Since the member names and types will already be known, this could cut down on coding depending on the data you are dealing with.

    One way which might sorta work (though quite a bit of work and code) would be to go through the pointers. I think it may be possible, but really it's more work than it's worth.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  9. #9
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Subtitute for Select/Case on a UDT

    Okay... Lets try another track.... Enum...

    Code:
    Public Enum Var_type_Enum
        TDouble
        TLong
        TInteger
       etc...
    end enum 
    
    ----- in your sub
        WriteParams(fso, udtDouble, TDouble)
    
    -- Or --
    
        WriteParams(fso, udtLong, TLong)
    
    end sub
    
    sub WriteParams(fso as object, Instance as variant, varType as Var_type_Enum)
    
    Select Case Var_type_Enum 
    
    Case TDouble 
    Call WriteDouble(fso, Instance)
    
    'Case TLong
    This i think is a little less Kludgy than adding a string type to the UDT's....

    Basicaly if your passing a UDT over you already know which one it is, so you can pass the type as a Enum...

    I hope this is better...
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  10. #10
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    516

    Re: Subtitute for Select/Case on a UDT

    I'm assuming that the reason you need to find out what UDT you were passed is because they are different sizes. Can you build your select structure around the length of the passed item in bytes? IE:
    Code:
    Sub WriteParams(fso As Object, Instance As Variant)
    
        Select Case LenB(Instance)
            Case 16 'dblParam
                Call WriteDouble(fso, Instance)
            Case 18 'lngParam
                Call WriteLong(fso, Instance)
            Case 20 'strParam
                Call WriteString(fso, Instance)
            Case 24 'lstParam
                Call WriteList(fso, Instance)
        End Select
    
    End Sub
    Obviously, you'd have to change the values in the example ('cept the first one), I just stuck them in to illustrate.

  11. #11
    Join Date
    Apr 2003
    Posts
    322

    Re: Subtitute for Select/Case on a UDT

    Quote Originally Posted by GremlinSA
    Okay... Lets try another track.... Enum...

    [code]Public Enum Var_type_Enum
    TDouble
    TLong
    TInteger
    etc...
    end enum

    I hope this is better...
    Thanks- but, I need string fields in the UDT, because the special formatting for each data type will be generated as a string, then passed to one function (which is common to all data types) to write the formatted string to a file.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center