Resolved string comparison causes error msg

cfrank2000

Well-known member
Joined
Mar 6, 2021
Messages
71
Programming Experience
Beginner
I have a string and I would like to compare its content with a string from a record list and I get the following error:
Severity Code Description Project File Line Suppression State
Error CS0019 Operator '==' cannot be applied to operands of type 'ArrayList' and 'string' toto C:\Users\Dell\source\repos\toto\toto\Program.cs 130 Active

string comparison against record list string:
using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;

namespace toto
{
    class Rekord
    {
        public string ev { get; set; }
        public string het { get; set; }
        public string fordulo { get; set; }
        public string Ndarab13p1talalat { get; set; }
        public string egydbtelitalalat { get; set; }
        public string eredmenyek { get; set; }
        public long ossznyeremeny { get; set; }
    }
   
    class beolvasas
    {
        static int db = 0;
        static int telitalalatok = 0;
        static int fordulo = 0;
        static int egyfrd = 0;

        public List<Rekord> rlista = new List<Rekord>();
        public ArrayList minmax = new ArrayList();
        public void beolvas()
        {
            string olvas = @"c:\Users\Public\textfiles\toto.txt";
            using (StreamReader sr = new StreamReader(olvas, Encoding.Default))
            {
                string ev;
                string het;
                string egydbtelitalalat;
                sr.ReadLine();
                while (!sr.EndOfStream)
                {
                    string[] sor = sr.ReadLine().Split(';');
                   
                    Rekord adat = new Rekord();
                   
                    adat.ev = sor[0];
                    adat.het = sor[1];
                    adat.fordulo = sor[2];
                    adat.Ndarab13p1talalat = sor[3];
                    adat.egydbtelitalalat = sor[4];
                    adat.eredmenyek = sor[5];
                    adat.ossznyeremeny = Int32.Parse(adat.Ndarab13p1talalat)*Int32.Parse(adat.egydbtelitalalat);
                    if (Int32.Parse(adat.fordulo) == 1)
                    {
                        egyfrd = egyfrd + 1;
                        // Console.WriteLine(" 1 Fordulo " + adat.fordulo);
                        minmax.Add(Int32.Parse(adat.egydbtelitalalat));
                    }
                    rlista.Add(adat);
                    db++;

                }
                Console.WriteLine(" 1 Fordulo " + egyfrd);
                Console.WriteLine(db);
               
                //Random rd = new Random();
                //int rand_num = rd.Next(1, db);
            }
        }


        public void kiir()
        {
            //int csere = 0;
            long osszeg = 0;
            double atlag = 0;
            for (int i= 0; i<db;i++)
            {
                Console.WriteLine(" ");
                // rekordlista adat = new rekordlista();
               
               Console.WriteLine(
                    " Ev " + rlista[i].ev +
                    " Het " + rlista[i].het +
                    " Fordulo " + rlista[i].fordulo +
                    " N darab 13 + 1 " + rlista[i].Ndarab13p1talalat +
                    " Egy db telitalalat/osszeg  " + rlista[i].egydbtelitalalat +
                    " Eredmeny " + rlista[i].eredmenyek);
               
                    fordulo = fordulo + Int32.Parse(rlista[i].fordulo);
                    telitalalatok = telitalalatok + Int32.Parse(rlista[i].Ndarab13p1talalat);
                    osszeg = osszeg + rlista[i].ossznyeremeny;

            }
            atlag = osszeg / db;
            Console.WriteLine();
           Console.WriteLine(
                   " 3 Feledat: Fordulo szama    " + fordulo);
            Console.WriteLine(
                    " 4 Feledat: 13 + 1-es \n   talalatok szama           " + telitalalatok);
            Console.WriteLine(
                   " 5 Feledat: Atlag nyeremeny  " + atlag);
            int index = 0;
            minmax.Sort();
            int utolso = 0;
            int indexmin = 0;
            int a = 0;
            foreach (int n in minmax)
              {
                index++;
               
                  //Console.WriteLine(" minmax " + n+" indexmin "+ indexmin + " index " + index);
                if (n > 0)
                {
                    a++;
                    if (a < 2)
                    {
                        indexmin = index;
                    }
                }
                  utolso = n;
              }
            Console.WriteLine(" 6 Feledat: max. nyeremeny   " + utolso + "  index " + index);
            Console.WriteLine(" 7 Feledat: min. nyeremeny   " + minmax[indexmin-1] + "      indexmin " + indexmin);
            Console.WriteLine(" minmaxst " + minmax);
            string minmaxst = " ";
            minmaxst = Convert.ToString(minmax);
            for (int i = 0; i < db; i++)
            {
                //ev = rlista[i].ev;
                minmaxst = rlista[i].egydbtelitalalat;
                if (minmax == minmaxst)
                {
                    Console.WriteLine(" minmaxst " + minmaxst);
                 

                }
                Console.WriteLine(" osszeg " + rlista[i].egydbtelitalalat);
               
            }
            /*
            for (int i = 0;i < minmax.Count; i++)
            {
                for (int j = 1; j < minmax.Count; j++)
                {
                   if(minmax[i] < minmax[j - 1])
                    {
                        csere = minmax[i];
                    }
                }
                Console.WriteLine(
                  " minmax " + minmax[i]);
            }*/
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            beolvasas kiir1 = new beolvasas();
            kiir1.beolvas();
            kiir1.kiir();
            Console.WriteLine("Hello World!");
        }
    }
}
 
The error is correct. You can't compare an ArrayList against a single string. It's like trying to compare an brick wall to a single brick.

As an aside, I think this is the second time I've seen you code that uses an ArrayList. ArrayLists have fallen out of active use in C# since about .NET Framework 2.0. If your book or tutorial is teaching you this, it is either severely out of date, or it is a book or tutorial that has been transliterated from Java to C# and does not take into account changes in the way the language is used.
 
Like an list/collection, an ArrayList is a list of items. It doesn't make sense to compare a string to that list. What makes sense is to compare it to an item in that list, or perhaps each item in the list in turn, one at a time. As always when it comes to programming, think about how you do the same thing manually. If you had a list of names and you wanted to see whether a specific name was on that list, wouldn't you compare that specific name to each name on the list one by one? Of course you would, so why would it be different in code?

As suggested, you should not be using an ArrayList for anything these days. Generics were introduced in .NET in version 2.0 and, since then, you should use a List<T>. If you want to store string objects, a List<string> is specifically what you want.
 
Like an list/collection, an ArrayList is a list of items. It doesn't make sense to compare a string to that list. What makes sense is to compare it to an item in that list, or perhaps each item in the list in turn, one at a time. As always when it comes to programming, think about how you do the same thing manually. If you had a list of names and you wanted to see whether a specific name was on that list, wouldn't you compare that specific name to each name on the list one by one? Of course you would, so why would it be different in code?

As suggested, you should not be using an ArrayList for anything these days. Generics were introduced in .NET in version 2.0 and, since then, you should use a List<T>. If you want to store string objects, a List<string> is specifically what you want.
thank you for help. I replace the arraylist but as I was adviced but I get error:
Severity Code Description Project File Line Suppression State
Error CS0246 The type or namespace name 'nyeremeny2' could not be found (are you missing a using directive or an assembly reference?) toto C:\Users\Dell\source\repos\toto\toto\Program.cs 29 Active
can I get help?thank you.
 
There is simply no way that the code you posted generated that error message, because that name is nowhere in that code, so you're expecting us to help you fix code we've never seen. You need to post the relevant code and we shouldn't have to ask for that.
 
There is simply no way that the code you posted generated that error message, because that name is nowhere in that code, so you're expecting us to help you fix code we've never seen. You need to post the relevant code and we shouldn't have to ask for that.
thank you for help. that part managed to fix, I've got help from school,
List has to be string type not to use a name of a declared string. but I have another one I'll post it,
calling a class in main program I get error, I don't understand, please help me, thank you.
toto:
using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;

namespace toto
{
    class Rekord
    {
        public string ev { get; set; }
        public string het { get; set; }
        public string fordulo { get; set; }
        public string Ndarab13p1talalat { get; set; }
        public string egydbtelitalalat { get; set; }
        public string eredmenyek { get; set; }
        public long ossznyeremeny { get; set; }
    }
    
    class beolvasas
    {
        static int db = 0;
        static int telitalalatok = 0;
        static int fordulo = 0;
        static int egyfrd = 0;
        
        public List<Rekord> rlista = new List<Rekord>();
        public List<int> minmax = new List<int>();
        public void beolvas()
        {
            string olvas = @"c:\Users\Public\textfiles\toto.txt";
            using (StreamReader sr = new StreamReader(olvas, Encoding.Default))
            {
                sr.ReadLine();
                while (!sr.EndOfStream)
                {
                    string[] sor = sr.ReadLine().Split(';');
                    
                    Rekord adat = new Rekord();
                    
                    adat.ev = sor[0];
                    adat.het = sor[1];
                    adat.fordulo = sor[2];
                    adat.Ndarab13p1talalat = sor[3];
                    adat.egydbtelitalalat = sor[4];
                    adat.eredmenyek = sor[5];
                    adat.ossznyeremeny = Int32.Parse(adat.Ndarab13p1talalat)*Int32.Parse(adat.egydbtelitalalat);
                    if (Int32.Parse(adat.fordulo) == 1)
                    {
                        egyfrd = egyfrd + 1;
                        // Console.WriteLine(" 1 Fordulo " + adat.fordulo);
                        minmax.Add(Int32.Parse(adat.egydbtelitalalat));
                    }
                    rlista.Add(adat);
                    db++;

                }
                Console.WriteLine(" 1 Fordulo " + egyfrd);
                Console.WriteLine(db);
                
                //Random rd = new Random();
                //int rand_num = rd.Next(1, db);
            }
        }


        public void kiir()
        {
            //int csere = 0;
            long osszeg = 0;
            double atlag = 0;
            int minnyeremeny = 0;
            string minmaxst = " ";
            for (int i= 0; i<db;i++)
            {
                Console.WriteLine(" ");
                // rekordlista adat = new rekordlista();
                
               Console.WriteLine(
                    " Ev " + rlista[i].ev +
                    " Het " + rlista[i].het +
                    " Fordulo " + rlista[i].fordulo +
                    " N darab 13 + 1 " + rlista[i].Ndarab13p1talalat +
                    " Egy db telitalalat/osszeg  " + rlista[i].egydbtelitalalat +
                    " Eredmeny " + rlista[i].eredmenyek);
                
                    fordulo = fordulo + Int32.Parse(rlista[i].fordulo);
                    telitalalatok = telitalalatok + Int32.Parse(rlista[i].Ndarab13p1talalat);
                    osszeg = osszeg + rlista[i].ossznyeremeny;

            }
            atlag = osszeg / db;
            Console.WriteLine();
           Console.WriteLine(
                   " 3 Feledat: Fordulo szama    " + fordulo);
            Console.WriteLine(
                    " 4 Feledat: 13 + 1-es \n   talalatok szama           " + telitalalatok);
            Console.WriteLine(
                   " 5 Feledat: Atlag nyeremeny  " + atlag);
            int index = 0;
            minmax.Sort();
            int utolso = 0;
            int indexmin = 0;
            int a = 0;
            foreach (int n in minmax)
              {
                index++;
                
                 // Console.WriteLine(" minmax " + n+" indexmin "+ indexmin + " index " + index);
                if (n > 0)
                {
                    a++;
                    if (a < 2)
                    {
                        indexmin = index;
                    }
                }
                  utolso = n;
              }
            Console.WriteLine(" 6 Feledat: Legnagyobb nyeremeny   ");
            for (int i = 0; i < db; i++)
            {
                //ev = rlista[i].ev;
                minmaxst = rlista[i].egydbtelitalalat;
                if (utolso == Int32.Parse(minmaxst))
                {
                    Console.WriteLine(" Ev         " + rlista[i].ev);
                    Console.WriteLine(" Het        " + rlista[i].het);
                    Console.WriteLine(" Fordulo    " + rlista[i].fordulo);
                    Console.WriteLine(" Nyeremeny  " + rlista[i].egydbtelitalalat);
                    Console.WriteLine(" Eredmeny   " + rlista[i].eredmenyek);
                }
                //Console.WriteLine(" osszeg " + rlista[i].egydbtelitalalat);

            }
            //Console.WriteLine(" 7 Feledat: min. nyeremeny   " + minmax[indexmin-1] + "      indexmin " + indexmin);
            Console.WriteLine(" 7 Feledat: Legkissebb nyeremeny   ");
            Console.WriteLine();

            //minmaxst = Convert.ToString(minmax);
            minnyeremeny = minmax[indexmin - 1];
            for (int i = 0; i < db; i++)
            {
                //ev = rlista[i].ev;
                minmaxst = rlista[i].egydbtelitalalat;
                if (minnyeremeny == Int32.Parse(minmaxst))
                {
                    Console.WriteLine(" Ev         " + rlista[i].ev);
                    Console.WriteLine(" Het        " + rlista[i].het);
                    Console.WriteLine(" Fordulo    " + rlista[i].fordulo);
                    Console.WriteLine(" Nyeremeny  " + rlista[i].egydbtelitalalat);
                    Console.WriteLine(" Eredmeny   " + rlista[i].eredmenyek);
                }
                //Console.WriteLine(" osszeg " + rlista[i].egydbtelitalalat);
                
            }
            /*
            for (int i = 0;i < minmax.Count; i++)
            {
                for (int j = 1; j < minmax.Count; j++)
                {
                   if(minmax[i] < minmax[j - 1])
                    {
                        csere = minmax[i];
                    }
                }
                Console.WriteLine(
                  " minmax " + minmax[i]);
            }*/
        }
    }

    class EredmenyElemzo
    {
        private string Eredmenyek;

        private int DontetlenekSzama
        {
            get
            {
                return Megszamol('X');
            }
        }

        private int Megszamol(char kimenet)
        {
            int darab = 0;
            foreach (var i in Eredmenyek)
            {
                if (i == kimenet) darab++;
            }
            return darab;
        }

        public bool NemvoltDontetlenMerkozes
        {
            get
            {
                return DontetlenekSzama == 0;
            }
        }

        public EredmenyElemzo(string eredmenyek) // konstruktor
        {
            Eredmenyek = eredmenyek;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            beolvasas kiir1 = new beolvasas();
            kiir1.beolvas();
            kiir1.kiir();
            EredmenyElemzo eredmeny1 = new EredmenyElemzo();
            //Console.WriteLine("Hello World!");
        }
    }
}
 
What error are you getting?
 
What error are you getting?
thank you for help. the error is after new at EredmenyElemzo()
Severity Code Description Project File Line Suppression State
Error CS7036 There is no argument given that corresponds to the required formal parameter 'eredmenyek' of 'EredmenyElemzo.EredmenyElemzo(string)' toto C:\Users\Dell\source\repos\toto\toto\Program.cs 215 Active
 
Look at your constructor on line 203. It requires that you pass in a string. Look at your line 215 where you are calling the constructor. Why are you not passing in a string?

What I just wrote in the paragraph above is just rephrasing the error message the compiler gave you. Part of learning a programming language is also learning how to understand the error messages it gives you. Take time to analyze what it says instead of just throwing up your hands and asking others for help automatically. (Yes, I know -- some governments and institutions do not encourage this kind of thinking for yourself, but in the case of software development, thinking for yourself is highly encouraged.)
 
Look at your constructor on line 203. It requires that you pass in a string. Look at your line 215 where you are calling the constructor. Why are you not passing in a string?

What I just wrote in the paragraph above is just rephrasing the error message the compiler gave you. Part of learning a programming language is also learning how to understand the error messages it gives you. Take time to analyze what it says instead of just throwing up your hands and asking others for help automatically. (Yes, I know -- some governments and institutions do not encourage this kind of thinking for yourself, but in the case of software development, thinking for yourself is highly encouraged.)
thank you. I managed to sort it out. the last problem is that I can not insert form for a graphic task, developer tool doesn't allow me. I have visual studio 2019. can you help me please? thank you.
 
Last edited:
Open a new thread with your question about your new problem. If you are using WinForms, open the question under the WinForms subforum. If you are using WPF, open the question under the WPF subforum. If you are using ASP.NET MVC, open the question under the MVC subforum. You get the picture, I hope. (pun intended)
 
Open a new thread with your question about your new problem. If you are using WinForms, open the question under the WinForms subforum. If you are using WPF, open the question under the WPF subforum. If you are using ASP.NET MVC, open the question under the MVC subforum. You get the picture, I hope. (pun intended)
thank you for help. as I understood I should start as windows form.
 
Back
Top Bottom