VB.NET - How to assign an object to an object instance (better description inside)

I have an instance of an object that I access ME when it accesses an instance of an object. I have a method that gets a collection of these objects and I want to assign the first instance of the object.

This is a piece of code

Dim Books As New BookCollection(True)
Books.ListByThemeFeatured(ThemeID, 1) ' Fills the collection

If Books.Count > 0 Then
   Me = Books(0) ' Should set the first item to the current object
End If

      

Is it possible?

EDIT: Me belongs to the class that was created. In this case, it is the BookEntity class. This method could be called with the following code

 Dim Book As New BookEntity
 Book.FeaturedBook() ' Should fill the book entity with a featured book

      

0


source to share


4 answers


You can not. What you're looking for is a static (shared in VB) way:



Dim Book as BookEntity
Book = BookEntity.FeaturedBook()

      

0


source


This seems to be impossible as you are trying to replace an instance of an object where you do not have a handle. One option could replace all members of your current workbook instance with workbook values ​​(0), for example:

Me.ConstructMeFrom(Books(0))

      

Where ConstructMeFrom replaces all fields of the book:

Sub ConstructMeFrom(Book newBook)
    Me.Title = newBook.Title
    Me.PageCount = newBook.PageCount
    'etc
End Sub

      

Or you can create some method to change the value of the handle that your instance contains. Ex. you can use a delegate that replaces the instance itself with another instance. I'm not familiar with vb syntax, but here's how it's done in C #:

public class Book
{
    public delegate void Replacer(Book book);

    public void ReplaceMe(Replacer replacer, Book newBook)
    {
        replacer(newBook);
    }

    public int PageCount;
}

static void Main(string[] args)
{
    Book b1 = new Book() { PageCount = 3 };
    Book b2 = new Book() { PageCount = 5 };
    b1.ReplaceMe(book => b1 = book, b2);
    Console.WriteLine(b1.PageCount); // Output is 5.
}

      



Update:

C # example with updated example:

FeaturedBook(Replacer replacer)
{
    Book featuredBook = FindTheFeaturedBookFromSomeList();
    replacer(featuredBook);
}

      

Now do the following:

BookEntity Book;
Book = BookEntity.FeaturedBook(delegate(BookEntity b) { Book = b; });

      

0


source


Or use sub

public sub FeaturedBooks() 
    dim FB as Book = GetFeaturedBook() 
    CopyToMe(FB) 
end sub

private shared sub CopyToMe(Abook as book)
    Me.Bookname = aBook.BookName
     etc
end sub

      

or have a generic function such that you write:

Dim Book as BookEntity = BookEntity.FeaturedBook()


Class BookEntity

Public shared function FeaturedBook() as BookEntity
    Dim NewBook as BookEntity
    Get the featured book into here...

    return NewBook
end function

end class

      

0


source


Sorry, but something "smells bad" in this code ... why would you need to replace the current object BookEntity

? Your code seems to be doing some things that should be out of scope BookEntity

.

I think instead you should have a class like BookDisplayer

that has a property CurrentBook

that handles the logic needed to display the new book. Assuming this is what you are trying to accomplish, of course.

EDIT

It looks like the following example code:

Book.FeaturedBook() ' Should fill the book entity with a featured book

      

It should look like this, the logic FeaturedBook

living in the class BookCollection

is instead:

Book = Books.GetFeaturedBook()
' NOW perform display logic

      

0


source







All Articles