Valhalla Legends Archive

Programming => General Programming => .NET Platform => Topic started by: Joe[x86] on June 26, 2006, 02:19 PM

Title: VB8 - Weird problem using constant color value?
Post by: Joe[x86] on June 26, 2006, 02:19 PM
I've started writing a bot in VB8, so naturally, I had to write an addChat-type function.

Module modUserInterface

    Public Sub addChat(ByVal Color As System.Drawing.Color, ByVal Text As String)
        With frmMain.rtbOutput
            .SelectionStart = Len(.Text)
            .SelectionColor = Color.White
            .SelectedText = "[" & Microsoft.VisualBasic.TimeString & "] "
            .SelectionColor = Color
            .SelectedText = Text & Microsoft.VisualBasic.Chr(13)
            .SelectionStart = Len(.Text)
        End With
    End Sub

End Module


On the line, .SelectionColor = Color.White, I get the warning Access of shared member, constant member, enum member or nested type through an instance; qualifying expression will not be evaluated.

Obviously it's not a fatal error or anything, but I'm trying to follow good coding style and proper language usage all throughout this bot. What's the proper way of doing what I'm attempting?






After applying K's suggestions, here's the following code, example usage, and output.

Code:
Module modUserInterface

    Public Sub addChat(ByVal myColor As System.Drawing.Color, ByVal myText As String)
        With frmMain.rtbOutput
            .SelectionStart = Len(.Text)
            .SelectionColor = Color.White
            .SelectedText = "[" & DateTime.Now.ToLongTimeString() & "] "
            .SelectionColor = myColor
            .SelectedText = myText & Environment.NewLine
            .SelectionStart = Len(.Text)
        End With
    End Sub

End Module


Usage:
Public Class frmMain

    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        modUserInterface.addChat(Color.GreenYellow, "Welcome to .NET Bot by Joe[e2]!")
    End Sub

[...]

End Class


Output:
Quote[1:27:03 PM] Welcome to .NET Bot by Joe[e2]!
Title: Re: VB8 - Weird problem using constant color value?
Post by: K on June 26, 2006, 04:09 PM
Tips:

Don't use anything from the Microsoft.VisualBasic namespace.  Those are there for conversion of legacy code.

Microsoft.VisualBasic.TimeString => DateTime.Now.ToString(optional format string)
Microsoft.VisualBasic.Chr(13) => Environment.NewLine

The warning you are getting is because you have named your variable "Color," which conflicts with the name of the type (if you have imported the System.Drawing namespace.  If you haven't imported that namespace, VB.NET must be super lax about things).

Either rename your variable, or change the Color.White to System.Drawing.Color.White.
Title: Re: VB8 - Weird problem using constant color value?
Post by: Joe[x86] on June 26, 2006, 05:24 PM
VB.NET is super lax about stuff.

That idea of conflicting names came to mind for a second, but I guess I never tested it.

Thanks!
Title: Re: VB8 - Weird problem using constant color value?
Post by: l)ragon on July 01, 2006, 04:04 PM
Quote from: J on June 26, 2006, 02:19 PM
I've started writing a bot in VB8, so naturally, I had to write an addChat-type function.

Module modUserInterface

    Public Sub addChat(ByVal Color As System.Drawing.Color, ByVal Text As String)
        With frmMain.rtbOutput
            .SelectionStart = Len(.Text)
            .SelectionColor = Color.White
            .SelectedText = "[" & Microsoft.VisualBasic.TimeString & "] "
            .SelectionColor = Color
            .SelectedText = Text & Microsoft.VisualBasic.Chr(13)
            .SelectionStart = Len(.Text)
        End With
    End Sub

End Module
There's no need to use Len() on strings anymore lol do this.
            .SelectionStart = .Text.Length
Title: Re: VB8 - Weird problem using constant color value?
Post by: Joe[x86] on July 01, 2006, 04:17 PM
Advantages?
Title: Re: VB8 - Weird problem using constant color value?
Post by: l)ragon on July 01, 2006, 04:27 PM
Quote from: J on July 01, 2006, 04:17 PM
Advantages?
length is allready created in string your recounting a second time.
Title: Re: VB8 - Weird problem using constant color value?
Post by: K on July 01, 2006, 06:01 PM
Quote from: l)ragon on July 01, 2006, 04:27 PM
Quote from: J on July 01, 2006, 04:17 PM
Advantages?
length is allready created in string your recounting a second time.


That and it makes more sense from an OO point of view.  "Length" is something that a string has, not an operation you apply to a string.
Title: Re: VB8 - Weird problem using constant color value?
Post by: Warrior on July 02, 2006, 01:22 PM
Plus it looks totally cool.