Click here to Skip to main content
15,885,998 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
I am relatively new to C# so I am kind of stuck on some code but I feel the fix is kind of simple. If anyone could help fix the code that would help me a lot.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication13
{
    class Program
    {
        static void Main(string[] args)
        {
            arrayList mylist = new arrayList(4);
            mylist.addFirst('a');
            mylist.addFirst('b');
            mylist.addFirst('c');
            mylist.addFirst('d');
            try
            {
                mylist.addFirst('e');
            }
            catch
            {
            }
        }
    }
}


C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace ConsoleApplication13
{
        public arrayList(int v)
        {
            mylist = new char(v);
            size = 0;
        }

        public bool isEmpty()
        {
            bool rvalue = false;
            if (size == 0)
            (
                rvalue = true;
            )

            return rvalue;
        }

        public void addFirst(char c)
        {
            if (size == mylist.Length)
                throw new Exception
        }

    
}
Posted
Updated 8-Dec-13 12:53pm
v2
Comments
So what is the problem here?
Sergey Alexandrovich Kryukov 8-Dec-13 19:27pm    
Don't use ArrayList, it's obsolete, superseded by System.Collections.Generic.List<>
—SA
gggustafson 8-Dec-13 21:14pm    
I don't think ArrayList has been supersceded at all. It's just another tool in the C# programmer's toolbox.
Sergey Alexandrovich Kryukov 8-Dec-13 21:52pm    
What you think is not true, not at all. There is nothing "another" in it. It wasn't formally marked obsolete only because there is nothing wrong with keeping it in legacy code thus keeping compatibility. There absolutely no sense in using this type in development. Doing it is less safe compared to equivalent System.Collections.Generic.List<> which does not require potentially dangerous typecast.
—SA

Well, you could make the code simpler:
// required
using System;
using System.Collections;

// somewhere in a method ...

ArrayList aList = new ArrayList();
aList.AddRange(new string[] {"a","b","c","d","e"});
aList.Reverse();
But, is that really what you are after here ?
 
Share this answer
 
Um.
You can't do that: the second code sample has no class declaration, so what you are trying to do is declare the constructor and two methods as global methods - which C# does not have (if a "global constructor" made any sense anyway, which it doesn't).

Try adding the class declarations round it:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace ConsoleApplication13
{
    public class arrayList
    {
        public arrayList(int v)
        {
            mylist = new char(v);
            size = 0;
        }

        public bool isEmpty()
        {
            bool rvalue = false;
            if (size == 0)
            (
                rvalue = true;
            )

            return rvalue;
        }

        public void addFirst(char c)
        {
            if (size == mylist.Length)
                throw new Exception
        }
    }
}
That should fix it.
 
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