What Does This Mean?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Herman Jones

    #1

    What Does This Mean?

    I found this statement in some sample code. It seems to be syntactically
    correct. What do the brackets around "String" mean?

    Dim sWork As [String] = "Some number of characters"

  • Michael C

    #2
    Re: What Does This Mean?

    "Herman Jones" <HermanJones@di scussions.micro soft.comwrote in message
    news:6F3326FF-B08F-4729-A6AB-80AD27F9EA27@mi crosoft.com...
    >I found this statement in some sample code. It seems to be syntactically
    correct. What do the brackets around "String" mean?
    >
    Dim sWork As [String] = "Some number of characters"
    They are used indicate the item in the brackets is not a keyword. For
    example if you wanted to name a variable Dim you might be able to do this:

    Dim [Dim] as Int32
    [Dim] = 5

    I'm not sure if that works but that the sort of thing it's used for. It
    could be used for function names, class names etc. In the case of String
    above it does nothing but doesn't cause any problems either. You can remove
    them.

    Michael


    Comment

    • Herman Jones

      #3
      Re: What Does This Mean?

      This example seems to be a little different. In your example you're using
      [Dim] as a variable name. It comes before the "As" keyword.

      The sample code I found has [String] after the "As" keyword. I don't thing
      that "String" is being used as the variable name because the program later
      refers to the "sWork" variable.

      Is it possible that the brackets have some other purpose when they bracket a
      datatype?



      "Michael C" wrote:
      "Herman Jones" <HermanJones@di scussions.micro soft.comwrote in message
      news:6F3326FF-B08F-4729-A6AB-80AD27F9EA27@mi crosoft.com...
      I found this statement in some sample code. It seems to be syntactically
      correct. What do the brackets around "String" mean?

      Dim sWork As [String] = "Some number of characters"
      >
      They are used indicate the item in the brackets is not a keyword. For
      example if you wanted to name a variable Dim you might be able to do this:
      >
      Dim [Dim] as Int32
      [Dim] = 5
      >
      I'm not sure if that works but that the sort of thing it's used for. It
      could be used for function names, class names etc. In the case of String
      above it does nothing but doesn't cause any problems either. You can remove
      them.
      >
      Michael
      >
      >
      >

      Comment

      • Michael C

        #4
        Re: What Does This Mean?

        "Herman Jones" <HermanJones@di scussions.micro soft.comwrote in message
        news:2E9EE6D2-FEDB-4521-967F-12BB3C2C3203@mi crosoft.com...
        This example seems to be a little different. In your example you're using
        [Dim] as a variable name. It comes before the "As" keyword.
        >
        The sample code I found has [String] after the "As" keyword. I don't
        thing
        that "String" is being used as the variable name because the program later
        refers to the "sWork" variable.
        >
        Is it possible that the brackets have some other purpose when they bracket
        a
        datatype?
        The dim was just 1 example, it can be used in many places. If a program had
        a datatype called "As" then you'd need to do Dim x as [As]

        Michael


        Comment

        • Cor Ligthert [MVP]

          #5
          Re: What Does This Mean?

          >I found this statement in some sample code. It seems to be syntactically
          correct. What do the brackets around "String" mean?
          >
          Dim sWork As [String] = "Some number of characters"
          That the programmer of this program is probably an idiot.
          You replace with this the general meaning of the keyword.

          Public Class Form1
          Private Sub Form1_Load(ByVa l sender As System.Object, _
          ByVal e As System.EventArg s) Handles MyBase.Load
          Dim a As New [String](1)
          Dim b As String = "What the hell is this"
          End Sub
          End Class
          Public Class [String]
          Public Sub New(ByVal b As Integer)
          b = b
          End Sub
          Public b As Integer
          End Class

          Cor


          Comment

          • Michael C

            #6
            Re: What Does This Mean?

            "Cor Ligthert [MVP]" <notmyfirstname @planet.nlwrote in message
            news:%237JZpfOH HHA.4056@TK2MSF TNGP03.phx.gbl. ..
            Public Sub New(ByVal b As Integer)
            b = b
            End Sub
            Public b As Integer
            Does that actually work in vb.net?

            Michael


            Comment

            • Cor Ligthert [MVP]

              #7
              Re: What Does This Mean?

              It runs and gives very strange results.

              Cor

              "Michael C" <nospam@nospam. comschreef in bericht
              news:OFxm6kOHHH A.3952@TK2MSFTN GP02.phx.gbl...
              "Cor Ligthert [MVP]" <notmyfirstname @planet.nlwrote in message
              news:%237JZpfOH HHA.4056@TK2MSF TNGP03.phx.gbl. ..
              > Public Sub New(ByVal b As Integer)
              > b = b
              > End Sub
              > Public b As Integer
              >
              Does that actually work in vb.net?
              >
              Michael
              >

              Comment

              • Michael C

                #8
                Re: What Does This Mean?

                "Cor Ligthert [MVP]" <notmyfirstname @planet.nlwrote in message
                news:u%2390dtOH HHA.1248@TK2MSF TNGP03.phx.gbl. ..
                It runs and gives very strange results.
                Would it just leave this.b = 0 or uninitialised?

                Michael


                Comment

                • Stephany Young

                  #9
                  Re: What Does This Mean?

                  Try it and find out!


                  "Michael C" <nospam@nospam. comwrote in message
                  news:%23mUCp4OH HHA.2456@TK2MSF TNGP06.phx.gbl. ..
                  "Cor Ligthert [MVP]" <notmyfirstname @planet.nlwrote in message
                  news:u%2390dtOH HHA.1248@TK2MSF TNGP03.phx.gbl. ..
                  >It runs and gives very strange results.
                  >
                  Would it just leave this.b = 0 or uninitialised?
                  >
                  Michael
                  >

                  Comment

                  • Cor Ligthert [MVP]

                    #10
                    Re: What Does This Mean?

                    zero

                    "Michael C" <nospam@nospam. comschreef in bericht
                    news:%23mUCp4OH HHA.2456@TK2MSF TNGP06.phx.gbl. ..
                    "Cor Ligthert [MVP]" <notmyfirstname @planet.nlwrote in message
                    news:u%2390dtOH HHA.1248@TK2MSF TNGP03.phx.gbl. ..
                    >It runs and gives very strange results.
                    >
                    Would it just leave this.b = 0 or uninitialised?
                    >
                    Michael
                    >

                    Comment

                    • Brian Gideon

                      #11
                      Re: What Does This Mean?

                      Herman,

                      No, the bracket does not have alternate meaning when used with data
                      types. Brackets allow you to create escaped or verbatim identifiers.
                      This is useful when you need to refer to an identifier that is also a
                      keyword. The most common scenario is the interaction between different
                      programming languages. For example, a C# programmer may think Alias
                      would be a good choice for a property name not knowing that it is a
                      keyword in VB. A VB programmer would need to use brackets when
                      referring to that property.

                      Brian

                      Herman Jones wrote:
                      This example seems to be a little different. In your example you're using
                      [Dim] as a variable name. It comes before the "As" keyword.
                      >
                      The sample code I found has [String] after the "As" keyword. I don't thing
                      that "String" is being used as the variable name because the program later
                      refers to the "sWork" variable.
                      >
                      Is it possible that the brackets have some other purpose when they bracket a
                      datatype?

                      Comment

                      • Brian Gideon

                        #12
                        Re: What Does This Mean?

                        Herman,

                        No, the bracket does not have alternate meaning when used with data
                        types. Brackets allow you to create escaped or verbatim identifiers.
                        This is useful when you need to refer to an identifier that is also a
                        keyword. The most common scenario is the interaction between different
                        programming languages. For example, a C# programmer may think Alias
                        would be a good choice for a property name not knowing that it is a
                        keyword in VB. A VB programmer would need to use brackets when
                        referring to that property.

                        Brian

                        Herman Jones wrote:
                        This example seems to be a little different. In your example you're using
                        [Dim] as a variable name. It comes before the "As" keyword.
                        >
                        The sample code I found has [String] after the "As" keyword. I don't thing
                        that "String" is being used as the variable name because the program later
                        refers to the "sWork" variable.
                        >
                        Is it possible that the brackets have some other purpose when they bracket a
                        datatype?

                        Comment

                        • _AnonCoward

                          #13
                          Re: What Does This Mean?


                          "Herman Jones" <HermanJones@di scussions.micro soft.comwrote in
                          message news:6F3326FF-B08F-4729-A6AB-80AD27F9EA27@mi crosoft.com...
                          :
                          : I found this statement in some sample code. It seems to be
                          : syntactically correct. What do the brackets around "String"
                          : mean?
                          :
                          : Dim sWork As [String] = "Some number of characters"


                          I don't see the point of using the brackets in this context. I checked
                          out the complete code you included in the other post and it compiles
                          just fine with or without the brackets. Just out of curiousity, I
                          compared both versions of the code in the ildasm utility and the vb
                          compiler emitted the exact same code. In this instance, the brackets
                          are meaningless. That said, brackets are quite useful. Indeed, they
                          are often necessary.

                          Let's say you have access to an email message class written in C#
                          which exposes the following Properties:

                          From
                          To
                          Subject
                          Body


                          If you were to declare an instance of this class in vb, you'd have a
                          problem because "To" is a vb keyword. The following would fail:

                          With New CSharpEmailMess ageType
                          .From = "Me@MyDomain.co m"
                          .To = "You@YourDomain .com"
                          .Subject = "Hello"
                          .Body = "World"
                          End With

                          This would not compile. To get around this, you would need to do the
                          following:

                          With New CSharpEmailMess ageType
                          .From = "Me@MyDomain.co m"
                          .[To] = "You@YourDomain .com"
                          .Subject = "Hello"
                          .Body = "World"
                          End With

                          Now the code would compile just fine. Brackets tell the compiler to
                          treat what would otherwise be a reserved keyword as any other code
                          element.

                          Another use I've personally found for the brackets is when I'm
                          generating throw away code. I don't post to this forum often, but
                          occasionally I will offer a code example to a question. In those
                          cases, I may post a complete example that will compile and run. For
                          example, I might do something like this:

                          =============== =============== =========
                          Option Strict

                          Imports Microsoft.Visua lBasic
                          Imports System

                          Public Module [module]
                          Public Sub Main
                          Dim c As New [class]
                          Console.WriteLi ne(c.GetMessage )
                          End Sub
                          End Module

                          Public Class [class]
                          Public Function GetMessage() As String
                          Return "Hello World" & vbCrLf
                          End Function
                          End Class
                          =============== =============== =========


                          I defined a module and a class but really didn't want to waste time
                          trying to figure out a clever or relevant name for them, so I just
                          named them '[module]' and '[class]' and moved on from there.


                          Ralf
                          --
                          --
                          ----------------------------------------------------------
                          * ^~^ ^~^ *
                          * _ {~ ~} {~ ~} _ *
                          * /_``>*< >*<''_\ *
                          * (\--_)++) (++(_--/) *
                          ----------------------------------------------------------
                          There are no advanced students in Aikido - there are only
                          competent beginners. There are no advanced techniques -
                          only the correct application of basic principles.


                          Comment

                          • Cor Ligthert [MVP]

                            #14
                            Re: What Does This Mean?

                            _Anon,

                            Nobody discuss that they can be handy. I do not believe that there is a
                            serious person who would give String another name even not somebody from who
                            is selling underwear.

                            Cor

                            "_AnonCowar d" <abcdef@uvwxyz. comschreef in bericht
                            news:yhqfh.5261 $dD2.673@tornad o.southeast.rr. com...
                            >
                            "Herman Jones" <HermanJones@di scussions.micro soft.comwrote in
                            message news:6F3326FF-B08F-4729-A6AB-80AD27F9EA27@mi crosoft.com...
                            :
                            : I found this statement in some sample code. It seems to be
                            : syntactically correct. What do the brackets around "String"
                            : mean?
                            :
                            : Dim sWork As [String] = "Some number of characters"
                            >
                            >
                            I don't see the point of using the brackets in this context. I checked
                            out the complete code you included in the other post and it compiles
                            just fine with or without the brackets. Just out of curiousity, I
                            compared both versions of the code in the ildasm utility and the vb
                            compiler emitted the exact same code. In this instance, the brackets
                            are meaningless. That said, brackets are quite useful. Indeed, they
                            are often necessary.
                            >
                            Let's say you have access to an email message class written in C#
                            which exposes the following Properties:
                            >
                            From
                            To
                            Subject
                            Body
                            >
                            >
                            If you were to declare an instance of this class in vb, you'd have a
                            problem because "To" is a vb keyword. The following would fail:
                            >
                            With New CSharpEmailMess ageType
                            .From = "Me@MyDomain.co m"
                            .To = "You@YourDomain .com"
                            .Subject = "Hello"
                            .Body = "World"
                            End With
                            >
                            This would not compile. To get around this, you would need to do the
                            following:
                            >
                            With New CSharpEmailMess ageType
                            .From = "Me@MyDomain.co m"
                            .[To] = "You@YourDomain .com"
                            .Subject = "Hello"
                            .Body = "World"
                            End With
                            >
                            Now the code would compile just fine. Brackets tell the compiler to
                            treat what would otherwise be a reserved keyword as any other code
                            element.
                            >
                            Another use I've personally found for the brackets is when I'm
                            generating throw away code. I don't post to this forum often, but
                            occasionally I will offer a code example to a question. In those
                            cases, I may post a complete example that will compile and run. For
                            example, I might do something like this:
                            >
                            =============== =============== =========
                            Option Strict
                            >
                            Imports Microsoft.Visua lBasic
                            Imports System
                            >
                            Public Module [module]
                            Public Sub Main
                            Dim c As New [class]
                            Console.WriteLi ne(c.GetMessage )
                            End Sub
                            End Module
                            >
                            Public Class [class]
                            Public Function GetMessage() As String
                            Return "Hello World" & vbCrLf
                            End Function
                            End Class
                            =============== =============== =========
                            >
                            >
                            I defined a module and a class but really didn't want to waste time
                            trying to figure out a clever or relevant name for them, so I just
                            named them '[module]' and '[class]' and moved on from there.
                            >
                            >
                            Ralf
                            --
                            --
                            ----------------------------------------------------------
                            * ^~^ ^~^ *
                            * _ {~ ~} {~ ~} _ *
                            * /_``>*< >*<''_\ *
                            * (\--_)++) (++(_--/) *
                            ----------------------------------------------------------
                            There are no advanced students in Aikido - there are only
                            competent beginners. There are no advanced techniques -
                            only the correct application of basic principles.
                            >
                            >

                            Comment

                            Working...