dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: UDT Type array question?

  1. #1
    Shannon Guest

    UDT Type array question?


    Trying to find a way to numerically call a random element from a type array.
    Example:

    Type Colors
    Blue as string
    Red as string
    Green as string
    Yellow as string
    End type

    Is there a numerical way to call a random color from this list (some type
    of list index)? For example the second element in the color list, regardless
    of that element's actual name?

    (I realize this is not a proper way to use the TYPE array, just chose colors
    to make the question more clear.

    Any help appreciated (as usual .


  2. #2
    Bernie Guest

    Re: UDT Type array question?


    Shannon,

    I don't know if this will solve your problem, but it is an approach. redefine
    the type to contain an array as shown below.

    Type Colors
    Name As String
    Color(3) As String
    End Type

    Then Color has an index and can be used to randomly select an element.

    Private Sub cmdtest_Click()
    Dim udtColor As Colors
    Dim i As Integer
    With udtColor
    .Name = "My Colored Object "
    .Color(0) = "Blue as in Moon"
    .Color(1) = "Green as in Cheese"
    .Color(2) = "Red as in Rock Eater"
    .Color(3) = "Yellow as in Dog"
    Debug.Print .Name & .Color(Int(4 * Rnd))
    End With

    End Sub

    Best of Luck, Bernie

    "Shannon" <se_eckert@hotmail.com> wrote:
    >
    >Trying to find a way to numerically call a random element from a type array.
    > Example:
    >
    >Type Colors
    > Blue as string
    > Red as string
    > Green as string
    > Yellow as string
    >End type
    >
    >Is there a numerical way to call a random color from this list (some type
    >of list index)? For example the second element in the color list, regardless
    >of that element's actual name?
    >
    >(I realize this is not a proper way to use the TYPE array, just chose colors
    >to make the question more clear.
    >
    >Any help appreciated (as usual .
    >



  3. #3
    Shannon Guest

    Re: UDT Type array question?


    Thanks, Bernie for your reply. No, it doesn't help me (but it was a cute
    example). I figured my color example my get a response like this [bad example
    ]. In your example, I'm trying to figure how to randomly access either
    "name" or "color." (either of the actual master elements of the array).

    Thanks anyway for your time.


    "Bernie" <bernie_thompson@msn.com> wrote:
    >
    >Shannon,
    >
    >I don't know if this will solve your problem, but it is an approach. redefine
    >the type to contain an array as shown below.
    >
    >Type Colors
    > Name As String
    > Color(3) As String
    >End Type
    >
    >Then Color has an index and can be used to randomly select an element.
    >
    >Private Sub cmdtest_Click()
    > Dim udtColor As Colors
    > Dim i As Integer
    > With udtColor
    > .Name = "My Colored Object "
    > .Color(0) = "Blue as in Moon"
    > .Color(1) = "Green as in Cheese"
    > .Color(2) = "Red as in Rock Eater"
    > .Color(3) = "Yellow as in Dog"
    > Debug.Print .Name & .Color(Int(4 * Rnd))
    > End With
    >
    >End Sub
    >
    >Best of Luck, Bernie
    >



  4. #4
    Dean Earley Guest

    Re: UDT Type array question?

    "Shannon" <se_eckert@hotmail.com> wrote in message news:3c83d812$1@10.1.10.29...
    >
    > Thanks, Bernie for your reply. No, it doesn't help me (but it was a cute
    > example). I figured my color example my get a response like this [bad example
    > ]. In your example, I'm trying to figure how to randomly access either
    > "name" or "color." (either of the actual master elements of the array).


    NameOrColour = IIf(Rnd() < 0.5, MyType.Name, MyType.Colour)

    OR

    If Rnd() < 0.5 Then
    NameOrColour = MyType.Name
    Else
    NameOrColour = MyType.Colour
    End if

    Is this more use?
    --
    Dean Earley (dean.earley@icode.co.uk)
    Assistant Developer

    iCode Systems



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center