Solved

Loading a class from a string with a class name.

Posted on 2006-10-19
20
236 Views
Last Modified: 2012-08-13
I don't no if this is easy to do?

I would like to make a function like

FillCombo(ddControl, CollectionClass, ValueField, TextField, NeedsNullValueRow)

this is the code that works:

            Dim BuyerCol As New Csb.EabOrder.EabOrderBuyerCollection
            BuyerCol.LoadAll()
            Dim buyer As Csb.EabOrder.EabOrderBuyer
           
            dropdown.Items.Clear()
            Dim FirstlistItem As New ListItem("", "")
            dropdown.Items.Add(FirstlistItem)
           
            For Each Buyer In BuyerCol
                Dim listItem As New ListItem(Buyer.BuyerName, Buyer.BuyerId)
                dropdown.Items.Add(listItem)
            Next

Thanks.
0
Comment
Question by:keezebees
  • 11
  • 9
20 Comments
 
LVL 7

Expert Comment

by:Mohamed Zedan
ID: 17767647
I don't get what you want ?
0
 

Author Comment

by:keezebees
ID: 17771764
Sorry, should be al little more specific.

What i want is an function that automates the listed code
To do that I would have to be able to make an instance of the some classes just by there class name (string)
And thats what my problem is.

How do ik load the class 'Csb.EabOrder.EabOrderBuyer' as an instance based on only a class name??

public Function FillCombo(ddControl as dropdownlist, CollectionClassName as string, ValueField as string, TextField as string, NeedsNullValueRow as boolean) as boolean

Hope this is more clear.
0
 
LVL 7

Accepted Solution

by:
Mohamed Zedan earned 500 total points
ID: 17773757
ok to create an instance just by classname is like this

        Dim asm As Reflection.Assembly = Reflection.Assembly.GetExecutingAssembly
        Dim instance As Object = asm.CreateInstance(CollectionClassName)

if the collection class implements Ienumerable you should be able to enumerate through its items

just like
              for each buyer in buyercol

will be replaced by

             dim Enumer as iEnumrator = ctype(instance,IEnumerable).getEnumerator
             while enumer.movenext
                 dim buyer as object = enumer.current
                 'Work with the buyer object
             end while

If you need more info please ask :) ...
0
 

Author Comment

by:keezebees
ID: 17775721
Thanks mohzedan.

Any Idea why i get an 'object reference not set to an instance of an object' error??

NullReference exception was unhandled by user code


Private Sub test()
            Dim asm As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly
            Dim instance As Object = asm.CreateInstance("csb.EabOrder.EabOrderVerkoopOrderViewCollection")

'object reference error is here!!!!!            
Dim Enumer As IEnumerator = CType(instance, IEnumerable).GetEnumerator
            While Enumer.movenext
                Dim buyer As Object = Enumer.Current
                'Work with the buyer object
            End While

        End Sub
0
 
LVL 7

Expert Comment

by:Mohamed Zedan
ID: 17775770
try using eaborderverkooporderviewCollection
only in the createinstance...
and could you run this code
after dim asm .....

Dim types As Type() = asm.GetTypes
for i as integer = 0 to asm.length-1
  Console.writeline(asm(i).Name)
next

and the names of the types that are printed are the names you could load using this way.
i.e. asm.createinstance("name of type from the list printed")
0
 
LVL 7

Expert Comment

by:Mohamed Zedan
ID: 17775780
the exception is because the type name wasn't found in that assembly ... if it is contained in  a dll then that dll should be loaded ... if that type is in your namespace just check it's type name correctly ...
0
 

Author Comment

by:keezebees
ID: 17775940
ehh...

asm.length doesnt excists....

Must be getting to late for me:)
0
 
LVL 7

Expert Comment

by:Mohamed Zedan
ID: 17775970
sorry i meant types.length must be too late for me too . :)
0
 

Author Comment

by:keezebees
ID: 17776049
Like this right?

Dim asm As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly
            Dim instance As Object = asm.CreateInstance("Csb.EabOrder.EabOrderVerkoopOrder")

            Dim types As Type() = asm.GetTypes
            For i As Integer = 0 To types.Length - 1
                MsgBox(types(i).Name)
            Next
0
 
LVL 7

Expert Comment

by:Mohamed Zedan
ID: 17776096
yes but instead of the msgbox use console.writeline(types(i).name)

that way you'll have them written in the output window
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:keezebees
ID: 17776194
Here is the output:

MyComputer
MyProject
ThreadSafeObjectProvider`1
desktopmodules_csb_eaborder_editcsb_eaborder_ascx
EditCsb_EabOrder
desktopmodules_csb_eaborder_viewcsb_eaborder_ascx
ViewCsb_EabOrder
desktopmodules_csb_eaborder_settings_ascx
Settings
FastObjectFactory_app_web_yu75jgso
MyComputer
MyProject
ThreadSafeObjectProvider`1
desktopmodules_csb_eaborder_editcsb_eaborder_ascx
EditCsb_EabOrder
desktopmodules_csb_eaborder_viewcsb_eaborder_ascx
ViewCsb_EabOrder
desktopmodules_csb_eaborder_settings_ascx
Settings
FastObjectFactory_app_web_yu75jgso
0
 

Author Comment

by:keezebees
ID: 17776256
Sorry  mohzedan,

Have to go now.
Will be back for another go tomorrow.

Thanks.
0
 
LVL 7

Expert Comment

by:Mohamed Zedan
ID: 17777921
see you tomorrow :)
0
 

Author Comment

by:keezebees
ID: 17844795
A bit late, but here are your points.
Just got it to work

Thanks.
0
 
LVL 7

Expert Comment

by:Mohamed Zedan
ID: 17844926
Sorry for being too late :)
0
 
LVL 7

Expert Comment

by:Mohamed Zedan
ID: 17844933
If you need any more info post here ok ... :)
0
 

Author Comment

by:keezebees
ID: 17844980
well,

what i did was just write my code the way i would normaly do.
Do the testing making sure that I dont need to come back no more.
And then i replaced my object declaration with:

Dim instance As Object = asm.CreateInstance(CollectionClassName)

intellisence obviusly wont work, but hey, who needs intellisence.........well actuali I do but dont tell anyone Ok?

can you tell me a bit more about  supporting Ienumerable in a class so i could do the enumerating?
0
 
LVL 7

Expert Comment

by:Mohamed Zedan
ID: 17845020
What is your collection class like ?
0
 

Author Comment

by:keezebees
ID: 17845373
I am using EntitySpaces for my DAL

You  know of that??
0
 
LVL 7

Expert Comment

by:Mohamed Zedan
ID: 17845483
I just read about it .... your collections should already implement Ienumerable
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now