Share via


Is it possible to increment variable names in a loop?

Question

Friday, April 5, 2013 12:28 PM

I'm very new to C#, having learnt only some basic C++ during my studies.

My problem is this:
I have a class "BookDB" with quite a few objects inside, namely "Book1" to "Book9".

What I'm trying to do is implement a simple search function by accessing "Book1" all the way to "Book9" and comparing the content.

Is it possible to change the target in every iteration of the loop?
For example:

Iteration : Variable Name

1 : Book1

2 : Book2

3 : Book3 ?

What I want is to work with Book[X] in each loop iteration.

If it's not possible, I guess I could manually program it, since there are only 9 items to work with, but I'd like to optimise the code length as much as I can so that it can accommodate more items.

As a final thought, should I instead create an array to hold all the items and work with that?

*edit*

From what I've seen in the forums so far, it might not be possible at all to do this kind of thing with C#. Are arrays really the only way to go?

All replies (9)

Friday, April 5, 2013 1:06 PM ✅Answered | 1 vote

E.g.

class BookDB
{
  private List<Book> books = new List<Book>();

  public Books()
  {
    this.books.Add(
        new Book() 
        { 
          Title = 'Book1', 
          Author = 'AuthorA', 
          Shelf = 'ShelfA'
        }
      );
    // repeat for other books.
  }

  public List<Book> Books
  {
    get { return this.books; }
  }
}

Friday, April 5, 2013 3:23 PM ✅Answered | 1 vote

Just add the following method to BookDB:

public IEnumerable<Book> ListBooks()
{
    yield return this.Book1;
    yield return this.Book2;
    // and so on ...
    yield return this.Book9;
}

For more information, see yield (C# Reference)

Marcus Björklund


Friday, April 5, 2013 12:35 PM | 2 votes

This really depends on your code and how the BookDB class looks like.

Basically your BookDB - I think of it as a database access layer - should have a method ListBooks (RetrieveBooks  or named similar) and return a IEnumerabe<Book>. Then you can simple use it as

foreach (Book book in this.BookDB.ListBooks()
{
  // Work with your book instance.
}

Friday, April 5, 2013 12:43 PM

each item in the "BookDB" class that holds them all looks like this...

public class BookDB
{
    public static Book Book1 = new Book
    {
        Title = "Book 1",
        Author = "Author A",
        Shelf = "Shelf A",
    };

What should I do to implement the enumerable? I think I understand the basic concept, but I'm really just learning as I go by referring to the MSDN reference materials.

As far as I understand, it should look something like this:

public class BookDB
{
    ListBooks()
    {       //insert implementation here
    }
}

Friday, April 5, 2013 12:48 PM | 1 vote

It's almost impossible. If you wanna do a list of things of the same type, plz use List<string> or Dictionary<Key,Value> instead as a collection to store values.

If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats


Friday, April 5, 2013 3:56 PM | 2 votes

From what I've seen in the forums so far, it might not be possible at all to do this kind of thing with C#. Are arrays really the only way to go?

Yes, as reported by others the point is that in such a case you don't want your language to be able to "create dynamically variable names". You want your language to be able to handle all those objects using a *single* variable which will be a list of objects. It doesn't have to be an array. It could be any data structure that allows to handle a list.

Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".


Friday, April 5, 2013 5:38 PM

On an interesting note...I don't see the "Mark As Answer" function below all your posts.

All I can do is Vote as Helpful, Reply, Quote, and Report As Abuse.

I'm using Chrome as my Browser on Windows 8 Pro. Nothing seems out of place or wrong, so I'm not really sure why the button isn't there.


Friday, April 5, 2013 6:20 PM

The OP has posted as a discussion, not a question.


Saturday, April 6, 2013 12:57 PM

Thanks for the help, everyone.

I'll test out the code today and see how it works out.