Resolved Help: string.contains prob , & Console.WriteLine() not working ?

vmars316

Active member
Joined
Aug 24, 2020
Messages
39
Programming Experience
Beginner
Hello & Thanks ;
win 10 .vb
Help: string.contains prob , & Console.WriteLine() not working ?
Pls , why is "Console.WriteLine() not working" ?

Also , How could I make test with 'case insensitive' ?
Thanks for your Help...

VB.NET:
    Private Sub TestThis_Click(sender As Object, e As EventArgs) Handles TestThis.Click
        Test("Dot Net Perls")
        Test("dot net perls")
    End Sub

    Sub Test(ByVal input As String)
        ' Write the formal parameter.
        Console.Write("--- ")
        Console.Write(input)
        Console.WriteLine(" ---")

        ' See if it contains this string.
        Dim net As Boolean = input.Contains("Net")
        WriteLine("Contains 'Net': ")
        Console.WriteLine(net)
        Console.Read()
        '        MsgBox("Contains 'Net': ")

        ' See if it contains this string.
        If input.Contains("perls") Then
            Console.WriteLine("Contains 'perls'")
            Console.Read()
            '            MsgBox("Contains 'perls'")
        End If

        ' See if it does not contain this string.
        If Not input.Contains("Dot") Then
            Console.WriteLine("Doesn't Contain 'Dot'")
            Console.Read()
            '            MsgBox("Doesn't Contain 'Dot'")
        End If
    End Sub
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,346
Location
Sydney, Australia
Programming Experience
10+
Firstly, Console.WriteLine is working. It is doing what it does. If what it does is not what you expect then you have to actually provide an explanation of the problem. That means providing a detailed description of EXACTLY what you expect to happen, where you expect it to happen and what is happening instead.
 
Last edited:

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,346
Location
Sydney, Australia
Programming Experience
10+
With regards to String.Contains, here's an extension method that provides a case-sensitive version that works the same way internally:
VB.NET:
Imports System.Runtime.CompilerServices

Public Module StringExtensions

    <Extension>
    Public Function Contains(source As String, value As String, comparisonType As StringComparison) As Boolean
        Return source.IndexOf(value, comparisonType) >= 0
    End Function

End Module
Add that module to your project and then you can do something like this:
VB.NET:
Dim net As Boolean = input.Contains("Net", StringComparison.CurrentCultureIgnoreCase)
which is the same way various other String methods work.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,346
Location
Sydney, Australia
Programming Experience
10+
For future reference, please keep each thread to a single topic and each topic to a single thread. If you have multiple questions to ask, even about the same code, please create multiple threads and post ONLY the relevant information about each issue in each thread. When you ask multiple questions in the same thread, you can end up with multiple conversations crossing each other and you may not be able to mark the thread Resolved even if one of the questions has been fully answered.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,346
Location
Sydney, Australia
Programming Experience
10+
For the record, here's a more complete module containing extension methods that provide the full range of options for Contains as exist for IndexOf:
VB.NET:
Imports System.Runtime.CompilerServices

Public Module StringExtensions

    <Extension>
    Public Function Contains(source As String, value As Char) As Boolean
        Return source.IndexOf(value) >= 0
    End Function

    <Extension>
    Public Function Contains(source As String, value As Char, startIndex As Integer) As Boolean
        Return source.IndexOf(value, startIndex) >= 0
    End Function

    <Extension>
    Public Function Contains(source As String, value As Char, startIndex As Integer, count As Integer) As Boolean
        Return source.IndexOf(value, startIndex, count) >= 0
    End Function

    <Extension>
    Public Function Contains(source As String, value As String, comparisonType As StringComparison) As Boolean
        Return source.IndexOf(value, comparisonType) >= 0
    End Function

    <Extension>
    Public Function Contains(source As String, value As String, startIndex As Integer) As Boolean
        Return source.IndexOf(value, startIndex) >= 0
    End Function

    <Extension>
    Public Function Contains(source As String, value As String, startIndex As Integer, count As Integer) As Boolean
        Return source.IndexOf(value, startIndex, count) >= 0
    End Function

    <Extension>
    Public Function Contains(source As String, value As String, startIndex As Integer, comparisonType As StringComparison) As Boolean
        Return source.IndexOf(value, startIndex, comparisonType) >= 0
    End Function

    <Extension>
    Public Function Contains(source As String, value As String, startIndex As Integer, count As Integer, comparisonType As StringComparison) As Boolean
        Return source.IndexOf(value, startIndex, count, comparisonType) >= 0
    End Function

End Module
 

vmars316

Active member
Joined
Aug 24, 2020
Messages
39
Programming Experience
Beginner
Firstly, Console.WriteLine is working. It is doing what it does. If what it does is not what you expect then you have to actually provide an explanation of the problem. That means providing a detailed description of EXACTLY what you expect to happen, where you expect it to happen and what is happening instead.
I expect to see the 'black console screen to show' .
But it doesnt show .
Pls , why ?
Thanks
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,346
Location
Sydney, Australia
Programming Experience
10+
I expect to see the 'black console screen to show' .
But it doesnt show .
Of course it doesn't. Why would it in a WinForms app?
Pls , why ?
Because yours is not a Console app. Either you have a Console app or a WinForms app, not both. If you want a Console app then create a Console Application project in the first place. If you have created a Windows Forms Application because that's what you want then use it. You interact with the user through controls in that case, which might mean displaying output in a Label and gathering input through a TextBox. You can also use a MessageBox to notify users or a StatusStrip to be less intrusive. While debugging, the output from the Console class will be displayed in the Output window by default. Outside the debugger, that output goes nowhere by default. You can configure one or more listeners to receive that output but that would normally be something like a file to log it.

Basically, as I suggested, Console.WriteLine IS workiing because it's doing exactly what it's supposed to. It's your expectations that are broken, so spend a bit of time thinking about what type of application you actually want and then create and build a project appropriate for that.
 

vmars316

Active member
Joined
Aug 24, 2020
Messages
39
Programming Experience
Beginner
Thanks,
If I started a new console process
could I pass it some commands ?
Thanks
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,346
Location
Sydney, Australia
Programming Experience
10+
could I pass it some commands ?
That's a very vague question. What have you tried and where are you stuck? If you haven't tried anything, including any reading on the subject, that would be step 1.
 

vmars316

Active member
Joined
Aug 24, 2020
Messages
39
Programming Experience
Beginner
Ok ,
I think I'll drop this branch for now .
Thanks
 
Top Bottom