Click here to Skip to main content
15,886,019 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, working on a project where I'm making a "blog" that puts the blog posts in to a string array that then gets transferred to a list array. This is my current code and I'd appreciate any help. I'm still new to c# and programming as a whole so unsure how to fix it.

The current problem is that in case 3 I get the error message : Operator '==' cannot be applied to operands of type 'string' and 'string[]'

        bool search = false;
        bool minBool = true;
        List<string[]> blogPost = new List<string[]> { };
        string[] post = new string[2];

        while (minBool)
        {
            Console.WriteLine("\n\n\tWelcome to the blog!");
            Console.WriteLine("\n\t[1] - Write a blogpost");
            Console.WriteLine("\t[3] - Search for a blogpost");
            Console.Write("\n\tChoice:");
            Int32.TryParse(Console.ReadLine(), out int input);

            switch (input)
            {
                case 1:
                    Console.Write("\tName your post: ");
                    post = new string[2];
                    post[0] = Console.ReadLine();
                    Console.Write("\tWrite your post: ");
                    post[1] = Console.ReadLine();
                    blogPost.Add(post);

                    break;

                    case 3:

                    Console.Write("\tSearch for a blogpost:");
                    string searchTerm = Console.ReadLine();
                    for (int i = 0; i < blogPost.Count; i++)

                        if (searchTerm == blogPost)
                        {
                            search = true;
                            Console.Write("\tThe post is in the blog: " + post[i]);
                        }
                    if (search == false)
                    {
                        Console.WriteLine("The blogpost doesn't exist");
                    }
                    break;


What I have tried:

Well, I've tried doing the same thing but replacing the list name with the string array and it works just fine. But I need to be able to search through all of the blog posts and not just the most recent one so the string array doesn't cut it.
Posted
Updated 28-Oct-21 20:13pm
v2
Comments
BillWoodruff 28-Oct-21 20:26pm    
please tag your question to indicate if this is ASP, or WinForms, or whatever.

what would would the type of search queries you want to perform look like ? what syntax; what results expected ?

are you saving the blogs to a file, to a DB ?

my inability to read your language is at work here :)
Strandp 28-Oct-21 20:31pm    
I updated the post and translated all the text to english now. No idea what a DB is ?the blog posts get saved in case: 1

I just want the search to announce the name of the matching blogpost. I don't know if it's possible but I also want to have the searchterm be non-case sensitive with either .ToUpper() or .ToLower() if it works?

It's just a small program that otherwise also has the functions to post all blog posts and to remove the entries.
BillWoodruff 28-Oct-21 21:07pm    
great, please tag your question to indicate context. context is important to know since search facilities/methods vary.
[no name] 28-Oct-21 23:41pm    
You're "indexing" (through) blogPost (the list); the statement to reference the "item" in the list is:

if (searchTerm == blocPost[i])

or

if (blockPost[i].Contains(searchTerm))

or ... StartsWith(...)

and so on.

1 solution

The error is pretty self explanatory:
Operator '==' cannot be applied to operands of type 'string' and 'string[]'
It's tell you that this line:
C#
if (searchTerm == blogPost)
is trying to compare two very different types - in this case a string with an array of strings string[]
That's pretty understandable if you think about it: can you compare a coin with a stack of mixed coins in the real world?
No - you would compare your coin with each element of the stack one by one!

You need to do the same thing with your code, and as Gerry has said, that means indexing the array so you compare each string one by one.

There are other problems though: once found, you continue to search which is pointless as identical blogs will give you repeated results, or you will waste time processing the rest of the strings unnecessarily.

The way I'd do it is different:
C#
string searchTerm = Console.ReadLine();
string result = "The blogpost doesn't exist";
foreach (string blog in blogPost)
    {
    if (searchTerm == blog)
        {
            result = $"\tThe post is in the blog: {post[i]}";
            break;
        }
    }
Console.WriteLine(result);
break;
As foreach makes code more readable if you aren't interested in the index value.

Though in fact, I'd use simpler code and Linq methods:
C#
string result = blogPost.FirstOrDefault(b => b == searchTerm);
Console.WriteLine(result == null ? "The blogpost doesn't exist"
                                 : $"\tThe post is in the blog: {result}");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900