Results 1 to 8 of 8

Thread: [RESOLVED] [2005] What does [Byte] mean?

  1. #1

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Resolved [RESOLVED] [2005] What does [Byte] mean?

    Ive read up and it seems they all use Dim ToSend as [Byte](). This is strange to me as it would appear Dim ToSend() as Byte would be the appropriate way to do it. Can anyone explain what this is doing?

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,470

    Re: [2005] What does [Byte] mean?

    The location of the parentheses doesn't matter. You can place them after the identifier or after the type and the meaning is the same. If you want to specify a upper bound then you must place them after the identifier though.

    The square brackets allow you to use a VB.NET keyword without the compiler interpreting it as a keyword. If you do this:
    VB Code:
    1. Dim ToSend() as Byte
    then the word "Byte" will turn blue because it is being interpreted as the VB.NET keyword Byte, which is an in-built data type. If you do this:
    VB Code:
    1. Dim ToSend() as [Byte]
    then "Byte" is not interpreted as the keyword and the compiler looks for a type with that name, so it is actually interpreted as the System.Byte structure. Given that the Byte data type is implemented using the System.Byte structure, the net effect of this is zero, as the IL will be exactly the same. It's just that some people like to use all .NET types and avoid VB types altogether. A more useful way to use the square brackets is for identifiers. There may be times when you would like to name a variable "String". Normally you wouldn't be able to because the compiler would interpret it as a keyword and flag it as a syntax error. You can do this though:
    VB Code:
    1. Dim [String] As String
    Note that the brackets are NOT part of the identifier. They merely denote a keyword being used in a different capacity. Normally you should avoid using keywords as identifiers but there are times when it is useful.

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,651

    Re: [2005] What does [Byte] mean?

    Not part of the identifier as such but you would still need to use them when referencing the variable for the same reasons

    VB Code:
    1. [String] = "Hello"

  4. #4

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: [2005] What does [Byte] mean?

    The thing about the variables with the brackets is pretty stupid, there should never be a reason to name a variable String..

    So the [] around byte in this case are pointless? If I had my own structure named byte (God knows why I couldnt change it?) and i put the [] around byte it would goto my byte structure instead? Sorry if im not following

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,470

    Re: [2005] What does [Byte] mean?

    The use of "String" was just an example. You're right about not using "String" as an identifier. Something like "Date" is a more likely candidate. There are many other keywords too, not just the in-built types. There may be legitimate reasons to use some of those as identifiers. If you had your own type named Byte and you used [Byte] then you'd get a compiler error telling you that it was ambiguous, given that there was no reason to choose your Byte over System.Byte. A better example might be Date or Integer, as the equivalent .NET types have different names. If you were to define your own structure named Date then using [Date] would use your type instead of the VB.NET date type. Mind you, you'd have to use the brackets when you declared the type itself anyway:
    VB Code:
    1. Public Structure [Date]
    An even better example would be if you were to create a Date structure in C# code. There is no corresponding keyword in C# so you can use Date with impunity:
    Code:
    public struct Date {}
    There is no need to use square brackets so it is obvious that they are not part of the identifier. if you then want to create an instance of that type in your VB code you would have to use square brackets:
    VB Code:
    1. Dim dt1 As Date 'Uses in-built VB data type, which corresponds to a DateTime object.
    2. Dim dt2 As [Date] 'Uses your Date type defined in C#.

  6. #6
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,713

    Re: [2005] What does [Byte] mean?

    Its similar to using a Reserved word in an Access Field name and getting around it by wrapping it with brackets.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: [2005] What does [Byte] mean?

    Okay I understand it now, thankyou fellas

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,470

    Re: [RESOLVED] [2005] What does [Byte] mean?

    I just thought I'd add that I recalled a legitimate use of square brackets in my own code. I've posted a Stopwatch class in the CodeBank and I declared a Stop method. Stop is a VB.NET reserved word so in the declaration I had to enclose it in square brackets. Wherever it is used the brackets are not required because it will be qualified by an instance so there is no ambiguity.

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