|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
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" "Herman Jones" <HermanJo***@discussions.microsoft.com> wrote in message They are used indicate the item in the brackets is not a keyword. For news:6F3326FF-B08F-4729-A6AB-80AD27F9EA27@microsoft.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" 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 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? Show quoteHide quote "Michael C" wrote: > "Herman Jones" <HermanJo***@discussions.microsoft.com> wrote in message > news:6F3326FF-B08F-4729-A6AB-80AD27F9EA27@microsoft.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 > > > "Herman Jones" <HermanJo***@discussions.microsoft.com> wrote in message The dim was just 1 example, it can be used in many places. If a program had news:2E9EE6D2-FEDB-4521-967F-12BB3C2C3203@microsoft.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? a datatype called "As" then you'd need to do Dim x as [As] Michael 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: Show quoteHide quote > 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? 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: Show quoteHide quote > 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? >I found this statement in some sample code. It seems to be syntactically That the programmer of this program is probably an idiot.> correct. What do the brackets around "String" mean? > > Dim sWork As [String] = "Some number of characters" You replace with this the general meaning of the keyword. Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) 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 "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message Does that actually work in vb.net?news:%237JZpfOHHHA.4056@TK2MSFTNGP03.phx.gbl... > Public Sub New(ByVal b As Integer) > b = b > End Sub > Public b As Integer Michael It runs and gives very strange results.
Cor Show quoteHide quote "Michael C" <nospam@nospam.com> schreef in bericht news:OFxm6kOHHHA.3952@TK2MSFTNGP02.phx.gbl... > "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message > news:%237JZpfOHHHA.4056@TK2MSFTNGP03.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 > "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message Would it just leave this.b = 0 or uninitialised?news:u%2390dtOHHHA.1248@TK2MSFTNGP03.phx.gbl... > It runs and gives very strange results. Michael Try it and find out!
Show quoteHide quote "Michael C" <nospam@nospam.com> wrote in message news:%23mUCp4OHHHA.2456@TK2MSFTNGP06.phx.gbl... > "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message > news:u%2390dtOHHHA.1248@TK2MSFTNGP03.phx.gbl... >> It runs and gives very strange results. > > Would it just leave this.b = 0 or uninitialised? > > Michael > zero
Show quoteHide quote "Michael C" <nospam@nospam.com> schreef in bericht news:%23mUCp4OHHHA.2456@TK2MSFTNGP06.phx.gbl... > "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message > news:u%2390dtOHHHA.1248@TK2MSFTNGP03.phx.gbl... >> It runs and gives very strange results. > > Would it just leave this.b = 0 or uninitialised? > > Michael > I'm still a little confused, but it's getting clearer.
I've attached the entire code sample below. It's from http://msdn2.microsoft.com/en-us/library/9yyz8a6c.aspx As you can see, in this sample, there's no Class definition for [String]. It also does not use the "New" keyword as your example did. I'm gussing that in this case VB is using the standard System.String even though "String" is enclosed in brackets. Is this correct? Option Explicit On Option Strict On Imports System Imports System.IO Public Class CharsFromStr Public Shared Sub Main() ' Create a string to read characters from. Dim str As [String] = "Some number of characters" ' Size the array to hold all the characters of the string ' so that they are all accessible. Dim b(24) As Char ' Create an instance of StringReader and attach it to the string. Dim sr As New StringReader(str) ' Read 13 characters from the array that holds the string, starting ' from the first array member. sr.Read(b, 0, 13) ' Display the output. Console.WriteLine(b) ' Close the StringReader. sr.Close() End Sub End Class Show quoteHide quote "Cor Ligthert [MVP]" wrote: > >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(ByVal sender As System.Object, _ > ByVal e As System.EventArgs) 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 > > > "Herman Jones" <HermanJo***@discussions.microsoft.com> wrote in I don't see the point of using the brackets in this context. I checkedmessage news:6F3326FF-B08F-4729-A6AB-80AD27F9EA27@microsoft.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" 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 CSharpEmailMessageType .From = "M*@MyDomain.com" .To = "Y**@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 CSharpEmailMessageType .From = "M*@MyDomain.com" .[To] = "Y**@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.VisualBasic Imports System Public Module [module] Public Sub Main Dim c As New [class] Console.WriteLine(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. _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 Show quoteHide quote "_AnonCoward" <abc***@uvwxyz.com> schreef in bericht news:yhqfh.5261$dD2.673@tornado.southeast.rr.com... > > "Herman Jones" <HermanJo***@discussions.microsoft.com> wrote in > message news:6F3326FF-B08F-4729-A6AB-80AD27F9EA27@microsoft.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 CSharpEmailMessageType > .From = "M*@MyDomain.com" > .To = "Y**@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 CSharpEmailMessageType > .From = "M*@MyDomain.com" > .[To] = "Y**@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.VisualBasic > Imports System > > Public Module [module] > Public Sub Main > Dim c As New [class] > Console.WriteLine(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. > >
Computer Name - Best way to obtain this VB.Net
How to Add *AND* Process Checkbox Column in a DataGridView? Settings in VB.NET/WinForms WebBrowser BeforeNavigate2 - What is pDisp? How to create an IDE? A Simple SQL Result displayed into a TextBox Keeping Code Snippets? how can I tell if it is a folder or a non-folder file Exit Button Placeholder Control |
|||||||||||||||||||||||