Shared Sub Connect(server As [String], message As [String]) ?

  • Thread starter Thread starter Emilio
  • Start date Start date
E

Emilio

Question about

Shared Sub Connect(server As [String], message As [String])

Why is [String] in square brackets?

Is it like

Shared Sub Connect(server() As String, message() As String)

If so, is there any difference at all?
 
Again, the square brackets are not needed unless you are trying to avoid a
conflict of types names

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
 
Emilio

Here is an explanation about brackets as used in VBNet made by Jay B. Harlow
for the newsgroup languages.vb some days ago. Beneath that is a sample from
me not for using but to show it completly and to show how weird that can be.
In your sample it is useless to use brackets, except when String is a
special class.

Start of Jay B's text
-----
The brackets allow you to use a Keyword as an identifier. The first is using
String as a keyword, the second is using String specifically as an
identifier.

When brackets are used for a string than the String keyword in VB.NET is
simply an alias for System.String, and System is normally always imported,
you are correct they are semantically identical.

The only time that they would not be semantically identical is if you
defined a String type in your project.
\\\
Public Class [String]
End Class
///

In this case the brackets are required as I am using String as an
identifier. The String keyword would continue to be a keyword, an alias to
System.String, while [String] would be an identifier to my String class.
\\\
Dim a As [String] ' defines a MyProject.String variable
Dim b As String ' defines a System.String variable

If a = b Then ' fails as MyProject.String cannot be compared to
System.String
End If

---Start of my sample
To show how weird this can be when used
\\\
Public Class Weird
Public Shared Sub Main()
Dim [String] As New [String]
MessageBox.Show([String](0))
End Sub
End Class
Friend Class [String]
Private [String]() As String = _
{"Jay", "Cor", "Terry"}
Default Public Overloads ReadOnly _
Property Item(ByVal index As Integer) As String
Get
Return [String](index)
End Get
End Property
End Class
///
I hope this helps a little bit?

Cor
 
Can you explain why they use this syntax in the TcpClient overview
in the help pages? i.e.

Shared Sub Connect(server As [String], message As [String])
Try
' Create a TcpClient.
' Note, for this client to work you need to have a TcpServer
' connected to the same address as specified by the server, port
' combination.
Dim port As Int32 = 13000
Dim client As New TcpClient(server, port)

' Translate the passed message into ASCII and store it as a Byte
array.
Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(message)

' Get a client stream for reading and writing.
' Stream stream = client.GetStream();
Dim stream As NetworkStream = client.GetStream()

' Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length)

Console.WriteLine("Sent: {0}", message)

' Receive the TcpServer.response.
' Buffer to store the response bytes.
data = New [Byte](256) {}

' String to store the response ASCII representation.
Dim responseData As [String] = [String].Empty

' Read the first batch of the TcpServer response bytes.
Dim bytes As Int32 = stream.Read(data, 0, data.Length)
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
Console.WriteLine("Received: {0}", responseData)

' Close everything.
client.Close()
Catch e As ArgumentNullException
Console.WriteLine("ArgumentNullException: {0}", e)
Catch e As SocketException
Console.WriteLine("SocketException: {0}", e)
End Try

Console.WriteLine(ControlChars.Cr + " Press Enter to continue...")
Console.Read()
End Sub 'Connect

One Handed Man ( OHM - Terry Burns ) said:
Again, the square brackets are not needed unless you are trying to avoid a
conflict of types names

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--


Emilio said:
Question about

Shared Sub Connect(server As [String], message As [String])

Why is [String] in square brackets?

Is it like

Shared Sub Connect(server() As String, message() As String)

If so, is there any difference at all?
 
The probability is that this code was ported from C++ or something or there
are other parts of the code which are not show but would have caused a
conflict.

Have you tried the example without the brackets ?


--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--


Emilio said:
Can you explain why they use this syntax in the TcpClient overview
in the help pages? i.e.

Shared Sub Connect(server As [String], message As [String])
Try
' Create a TcpClient.
' Note, for this client to work you need to have a TcpServer
' connected to the same address as specified by the server, port
' combination.
Dim port As Int32 = 13000
Dim client As New TcpClient(server, port)

' Translate the passed message into ASCII and store it as a Byte
array.
Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(message)

' Get a client stream for reading and writing.
' Stream stream = client.GetStream();
Dim stream As NetworkStream = client.GetStream()

' Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length)

Console.WriteLine("Sent: {0}", message)

' Receive the TcpServer.response.
' Buffer to store the response bytes.
data = New [Byte](256) {}

' String to store the response ASCII representation.
Dim responseData As [String] = [String].Empty

' Read the first batch of the TcpServer response bytes.
Dim bytes As Int32 = stream.Read(data, 0, data.Length)
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
Console.WriteLine("Received: {0}", responseData)

' Close everything.
client.Close()
Catch e As ArgumentNullException
Console.WriteLine("ArgumentNullException: {0}", e)
Catch e As SocketException
Console.WriteLine("SocketException: {0}", e)
End Try

Console.WriteLine(ControlChars.Cr + " Press Enter to continue...")
Console.Read()
End Sub 'Connect

"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message
Again, the square brackets are not needed unless you are trying to avoid a
conflict of types names

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--


Emilio said:
Question about

Shared Sub Connect(server As [String], message As [String])

Why is [String] in square brackets?

Is it like

Shared Sub Connect(server() As String, message() As String)

If so, is there any difference at all?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top