0
public class BaseDAL
{
    protected object GetOrder(int id)
    {
        using (var DB = AccessManager.db)
        {
            return DB.Videos.Where(x => x.ID == id).SingleOrDefault();
        }
    }

    public object GetByID(int id)
    {
        using (var DB = AccessManager.db)
        {
            return DB.Videos.Where(x => x.ID == id).SingleOrDefault();
        }
    }
}

Is there any option to add this param int id for any function in class without write it each function?

Somthing like:

public class BaseDAL : ADD SOME CODE THAT SHARE int id for all funcs
{
    protected object GetOrder()
    {
        using (var DB = AccessManager.db)
        {
            return DB.Videos.Where(x => x.ID == id).SingleOrDefault();
        }
    }

    public object GetByID()
    {
        using (var DB = AccessManager.db)
        {
            return DB.Videos.Where(x => x.ID == id).SingleOrDefault();
        }
    }
}

and GetOrder(12); will works. I want this done only to reduce the code.

1
  • 1
    You want a function that takes a parameter, but you don't want to include that parameter in the code? The compiler is at this time unable to read your mind. Commented Dec 28, 2014 at 19:06

2 Answers 2

1

You could set an ID parameter in the constructor, like so:

public class BaseDAL
{

    public BaseDAL(int ID)
    {
       this.id= ID;
    }

    int id;

    protected object GetOrder()
    {
        using (var DB = AccessManager.db)
        {
            return DB.Videos.Where(x => x.ID == id).SingleOrDefault();
        }
    }

    public object GetByID()
    {
        using (var DB = AccessManager.db)
        {
            return DB.Videos.Where(x => x.ID == id).SingleOrDefault();
        }
    }
}

That allows you to write code like this:

var result = new BaseDAL(5).GetOrder();

You could also combine this with the other answer, so that you have a property you can set and change. But I don't really think either is a good idea. I think what you already have is better. Also, I think you'd be better served to return Order and Video objects, rather than Objects.

Sign up to request clarification or add additional context in comments.

Comments

0

Add a CurrentIndex property to your class

public class BaseDAL
{
    public CurrentIndex { get; set; }

    public Order GetOrder()
    {
        using (var DB = AccessManager.db) {
            return DB.Videos.Where(x => x.ID == CurrentIndex).SingleOrDefault();
        }
    }

    ...
}

and use it like this

var dal = new BaseDAL();
dal.CurrentIndex = 5;
var result = dal.GetOrder();

2 Comments

Somthing like this but call the function like: var dal = new BaseDAL(); var result = dal.GetOrder(5);
No. If you want to call a method with parameters, you have to define them at the method declaration! What's wrong with declaring the parameter at each method declaration? int id has 6 characters, could it be that you are a bit lazy?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.