Re: question(s) about declaring variables



> 1st. Which is better for a variable that is going to be used for text. Letting a variable be a
> variant or declaring it as a string. According to MSDN, variants are more "versatile" ( if I'm
> reading it right. )

Variants are more versatile in that they can contain many different variable types, however this comes with a huge performance
penalty so unless you have specific reason to create a variable without an explicit type then use String.
The majority of the MSDN examples are written very poorly and often use Variants when explicit variable types would be more suited,
this does not mean you should follow suit!

> 2nd. Why do all code examples use "As String to" declare each variable like this:
> Dim var1 As String
> Public var2 As String.
>
> instead of using the $ type-declaration character like this:
>
> Dim var1$
> Public var2 $

The type declaration characters are only included only for backward compatibility with older versions of BASIC, declaring a variable
"As String" in this case is more self documenting and the 'preferred' method of type declaration (although some still choose to use
the old type declaration characters.) IMO this just makes messy code, but they are identical when compiled.

> 3rd. Why a separate line for each variable
>
> I realize that declaring them like this:
> Dim var1, var2, var3 As String
>
> makes var1 and var2 variants and only var3 is declared as a string.
>
> when done this way however:
>
> Dim var1$, var2$, var3$
>
> They are all recognized as String variables.

This would also declare three string variables in a single line:

'***
Dim var1 As String, var2 As String, var3 As String
'***

There is no rule for how many variables to declare in a single declaration line, this is entirely personal preference. Again
however the more variables and different types you declare in a single line, the more difficult to read the code becomes.
Hope this helps,

Mike


- Microsoft Visual Basic MVP -
E-Mail: EDais@xxxxxxxx
WWW: Http://EDais.mvps.org/


.



Relevant Pages

  • Re: Is C99 the final C? (some suggestions)
    ... >> defined a function that returns an image string for a value of your ... >> need to decide how and whether the user can define precedence for the ... > of declaration. ... "parenthesize, parenthesize, parenthesize". ...
    (comp.lang.c)
  • Re: curious about array initialization.
    ... understanding that its through the variable declaration that we ... By "inline string", I assume you mean a string literal. ... more than that you have implicit storage. ... Its a concept from computer programming. ...
    (comp.lang.c)
  • Re: DhcpRequestParams from dhcpcsvc.dll
    ... it is because I do think that my own vb declaration is wrong, ... string to Unicode with StrConv and pass that as a string. ... Dim sendParams As DHCPAPI_PARAMS ...
    (microsoft.public.vb.winapi)
  • Re: curious about array initialization.
    ... understanding that its through the variable declaration that we ... By "inline string", I assume you mean a string literal. ... more than that you have implicit storage. ...
    (comp.lang.c)
  • Re: curious about array initialization.
    ... understanding that its through the variable declaration that we ... By "inline string", I assume you mean a string literal. ... more than that you have implicit storage. ...
    (comp.lang.c)