Results 1 to 4 of 4

Thread: variant type used improperly?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Posts
    195

    variant type used improperly?

    Hello everybody. I often come across pieces of code wherein the variant type is used seemingly without reason. I'll show you one example from Microsoft (so it's not supposed to be trash code):http://support.microsoft.com/kb/137038/en-us

    You'll find that the Ret variable (which is declared as variant) is used to store the return value from the GetPrivateProfileString API call. However, that call no question will return a long. So why did they use the variant type? And this is just one example. Like I said, there's plenty of cases where variants are gonna receive well-defined values. Anybody knows why this happens?

    Thank you.

  2. #2
    Frenzied Member
    Join Date
    Mar 2009
    Posts
    1,182

    Re: variant type used improperly?

    Laziness...
    Option Explicit should not be an Option!

  3. #3
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: variant type used improperly?

    It is just a code snippet to explain things to you. In your real code you would declare your variables according to your needs and judgements. It is very much possible that the snippet they have put there was a part of a larger program and they have just copied the relevent parts as it is without any changes.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: variant type used improperly?

    it would appear that microsoft themselves tend to support the use of variant variables, prior to .net, that is why many of there scripting and VBA functions return variants, or variant arrays
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Click Here to Expand Forum to Full Width