Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a User's array`
C#
User[] users = new User[]
        {
             new User ("Petros", "Petrosyan", 20),
             new User ("Poghos", "Poghosyan", 22),
             new User ("Valod", "Hakobyan", 23),
             new User ("Vazgen", "Hovhannisyan", 19),
             new User ("Ruben", "Martirosyan", 25),
        };

and class Controller which has a Delete Function.Need me when i will call the delete function in main for example users.delete(2); it will delete second index's elements in my array, for example` new User ("Valod", "Hakobyan", 23),

What I have tried:

I tried like this`
C#
public void delete(int index)
        {
            int current_index = 0;
            User[] nor_zang = new User[users.Length - 1];
            for (int i = 0; i < users.Length; i++)
            {
                if (i != index)
                {
                    nor_zang[current_index] = users[i];
                    current_index++;
                }
            }
            users = nor_zang;
            
        }



Main

Controller c = new Controller();
c.delete(2);

but it's not working, which is my fault?
Posted
Updated 3-Feb-18 7:26am
v2

Probably, the problem is that the new controller doesn't have any items in it - but from those code fragments, we can't tell.
So it's going to be up to you. Start with the debugger, and look at exactly what is going on.
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
I used this code to find the problem (copy&paste into a C# Console App Project):

The OriginalGriff is right, if you expect someone to help you should give a clear statement of what it is that does not work and give enough evenidence to verify what you are looking at - I just had to comment out one line and slightly improved the readability of the delete function by passing in parameters and returning a result ...hope this helps ...

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

namespace ConsoleApp2
{
    public class User
    {
        public User(string name, string lastname, int age)
        {
            Name = name;
            LastName = lastname;
            Age = age;
        }

        public string Name { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }
    }

    class Program
    {
        public static User[] delete(int index, User[] users)
        {
            int current_index = 0;
            User[] nor_zang = new User[users.Length - 1];

            for (int i = 0; i < users.Length; i++)
            {
                if (i != index)
                {
                    nor_zang[current_index] = users[i];
                    current_index++;
                }
            }
////        users = nor_zang;

            return nor_zang;
        }

        static void Main(string[] args)
        {
            User[] users = new User[]
            {
                 new User ("Petros", "Petrosyan", 20),
                 new User ("Poghos", "Poghosyan", 22),
                 new User ("Valod", "Hakobyan", 23),
                 new User ("Vazgen", "Hovhannisyan", 19),
                 new User ("Ruben", "Martirosyan", 25),
            };

            var result = delete(2, users);
        }
    }
}
 
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