| Visual Basic (Declaration) | |
|---|---|
<DefaultValueAttribute()> <DescriptionAttribute("Gets or Sets an object reference that can be used to associate this instance with any other.")> <BrowsableAttribute(False)> Public Property Tag As Object | |
| Visual Basic (Usage) | Copy Code |
|---|---|
Dim instance As Tcp Dim value As Object instance.Tag = value value = instance.Tag | |
| C# | |
|---|---|
[DefaultValueAttribute()] [DescriptionAttribute("Gets or Sets an object reference that can be used to associate this instance with any other.")] [BrowsableAttribute(false)] public object Tag {get; set;} | |
| Managed Extensions for C++ | |
|---|---|
[DefaultValueAttribute()] [DescriptionAttribute("Gets or Sets an object reference that can be used to associate this instance with any other.")] [BrowsableAttribute(false)] public: __property Object* get_Tag(); public: __property void set_Tag( Object* value ); | |
| C++/CLI | |
|---|---|
[DefaultValueAttribute()] [DescriptionAttribute("Gets or Sets an object reference that can be used to associate this instance with any other.")] [BrowsableAttribute(false)] public: property Object^ Tag { Object^ get(); void set ( Object^ value); } | |
Property Value
Any object that is associated with the component; null if no objects are associated with the component. Any object that is associated with the object, null if no objects are associated with the objectThe following example demonstrates a simple multi-client chat server.
| Visual Basic | Copy Code |
|---|---|
Private Sub StartServer() ' Begin listening for connections on port 8888. Server1.Listen(8888) End Sub Private Sub Server1_Connection(ByVal sender As Object, ByVal e As ConnectionEventArgs) Handles Server1.Connection ' This event is raised on a new thread when a connection is received. Try ' Send username command. e.Tcp.Stream.Write("Please enter your username: ") ' Read until a CRLF is reached Dim found As Boolean = False Dim s As String = e.Tcp.Stream.Read(vbCrLf, 1024, found) If (found) Then ' Trim(whitespace) s = s.Trim() ' Associate username with TCP instance e.Tcp.Tag = s e.Tcp.Stream.Write("Go ahead and chat" + vbCrLf) Else ' Disconnect e.Tcp.Stream.Write("Bad Input" + vbCrLf) e.Tcp.Stream.Close() End If Do While (e.Tcp.Connected) ' Receive data. found = False Dim text As String = e.Tcp.Stream.Read(vbCrLf, 1024, found) ' Echo data back to all clients Dim tcp As Tcp For Each tcp In Server1.Connections ' Preface text with user name and send tcp.Stream.Write(e.Tcp.Tag.ToString() + ": " + text + vbCrLf) Next Loop Catch ex As Exception ' eat exception End Try End Sub | |
| C# | Copy Code |
|---|---|
private void StartServer() { // Begin listening for connections on port 8888. server1.Listen(8888); } private void server1_Connection(object sender, ConnectionEventArgs e) { // This event is raised on a new thread when a connection is received. try { // Send username command. e.Tcp.Stream.Write("Please enter your username: "); // Read until a CRLF is reached bool found = false; string s = e.Tcp.Stream.Read("\r\n", 1024, ref found); if(found) { // Trim whitespace s = s.Trim(); // Associate username with TCP instance e.Tcp.Tag = s; e.Tcp.Stream.Write("Go ahead and chat\r\n"); } else { // Disconnect e.Tcp.Stream.Write("Bad Input\r\n"); e.Tcp.Stream.Close(); } while(e.Tcp.Connected) { // Receive data. found = false; string text = e.Tcp.Stream.Read("\r\n", 1024, ref found); // Echo data back to all clients foreach(Tcp tcp in server1.Connections) { // Preface text with user name (from Tcp.Tag) and send tcp.Stream.Write(e.Tcp.Tag.ToString() + ": " + text + "\r\n"); } } } catch(Exception ex) { // eat exception } } | |
Use this property to associate any type derived from Object class with the component. A common use for the Tag property is to store data that is closely associated with the component.
Use this property to associate any object with the object.
Target Platforms: Microsoft .NET Framework 2.0
Copy Code