|
-
Sep 9th, 2008, 02:48 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] [2008] [.NET 2.0] Dynamically Create object of unknown type
Is it possible to create an object of an unknown type? I know that is an ambiguous questions, hopefully this will demonstrate what I am trying to do.
I have an if statement that says:
Code:
if recordItem is GetType(InventoryItem) then
Dim inventoryItem As InventoryItem = DirectCast(recordItem, InventoryItem)
Elseif recordItem is GetType(NonInventoryResaleItem) then
Dim inventoryItem As NonInventoryResaleItem = DirectCast(recordItem, NonInventoryResaleItem)
End If
Well, there are other types it could be, and rather than making an elaborate if statement I was wondering if I could do it dynamically.
I tried using the activator.createInstance method, but I couldn't get it to work the way it showed in MSDN. Is what I want to do possible, or am I thinking crazy?
Ben
Using Visual Basic 2005/2008
-
Sep 9th, 2008, 03:05 PM
#2
Re: [2008] [.NET 2.0] Dynamically Create object of unknown type
Heck, I'm not even sure it will work the way you have it in your example. You are attempting to declare a local variable, but have that variable be declared as different types depending on the argument? How would you expect the compiler to create any kind of type safe code off of that? It can't know what type it is being handed.
This might be a situation where an Interface is worthwhile, though you haven't posted quite enough to be sure. If all the different types that the thing could be all implement the same interface, then you can pass in an instance of the interface and use that regardless of what the underlying type is. This would mean that all the types would have the same set of functions, and if an object of Class A was passed in, then A.SomeFunction would be called, whereas if an object of Class B was passed in, then B.SomeFunction would be called.
The only other thing that I can think of at the moment that seems even vaguely appropriate might be generics, but that is less likely.
My usual boring signature: Nothing
 
-
Sep 9th, 2008, 03:19 PM
#3
Thread Starter
Hyperactive Member
Re: [2008] [.NET 2.0] Dynamically Create object of unknown type
Thank you for your reply.
The code I have up there does work. recordItem is a type of Record from a NetSuite webservice. I think Record is a base class that can become an InventoryItem or a NonInventoryItem, and now that they have updated the webservice it can become more things, so I have to change my code. My code works because variables created inside an If block are scoped just to that section of the if statement.
I am sure there has to be a better way, but I don't know what it is, and I can't change their objects so I have to figure out if I can do it in my own code. Does that help you have any other ideas?
Ben
Using Visual Basic 2005/2008
-
Sep 9th, 2008, 03:32 PM
#4
Re: [2008] [.NET 2.0] Dynamically Create object of unknown type
is the action that takes place on all these objects going to be the same? You might want to looking into making a function that uses Generics... it would look something like this:
Code:
Private Function doSomethingWith (Of T)(ByVal myVar As object) As Boolean 'or what ever you need it to return as
Dim myNewT As T = DirectCast(myVar, T)
.
.
.
'Do what you need with myNewT
'This replaces the stuff between your Ifs
Return True
End Function
Then you can call it like this:
Code:
if recordItem is GetType(InventoryItem) then
doSomething(Of InventoryItem)(recordItem)
Elseif recordItem is GetType(NonInventoryResaleItem) then
doSomething(Of NonInventoryResaleItem)(recordItem)
End If
-tg
-
Sep 9th, 2008, 03:42 PM
#5
Thread Starter
Hyperactive Member
Re: [2008] [.NET 2.0] Dynamically Create object of unknown type
Thank you, techgnome, for your reply.
I see what you are doing there, but I am not sure how that is much different that what I am doing. In the if statement you have, I still have to know ahead of time what possible options Record can be cast into. I want to avoid knowing that ahead of time.
Here is what I know about the Record object from NetSuite.
Record
The Record type is an abstract type used as the parameter for the add, addList, delete, deleteList, update and updateList operations. It is also returned in the get, getList, search, searchMore and searchNext operations. All business object types extend the Record type. The record type is defined in core.xsd.
If it would be helpful, here is the full code for this method.
Code:
Private Shared Function ExtractNetSuiteInformation(ByVal recordItem As Record) As PSNetSuiteItemType
Dim netSuiteInformation As PSNetSuiteItemType = Nothing
Try
If recordItem.GetType Is GetType(InventoryItem) Then
Dim inventoryItem As InventoryItem = DirectCast(recordItem, InventoryItem)
netSuiteInformation = New PSNetSuiteItemType
With netSuiteInformation
.ItemExists = True
.InternalID = inventoryItem.internalId
.QuantityAvailable = inventoryItem.quantityAvailable
.QuantityBackordered = inventoryItem.quantityBackOrdered
.QuantityCommited = inventoryItem.quantityCommitted
.QuantityOnHand = inventoryItem.quantityOnHand
.QuantityOnOrder = inventoryItem.quantityOnOrder
.NetSuiteCost = CDec(inventoryItem.cost)
.IsDropShipItem = inventoryItem.isDropShipItem
.IsSpecialOrderItem = inventoryItem.isSpecialOrderItem
.NetSuitePurchaseDescription = inventoryItem.purchaseDescription
.NetSuiteSalesDescription = inventoryItem.salesDescription
.NetSuiteStoreDescription = inventoryItem.storeDescription
End With
ElseIf recordItem.GetType Is GetType(NonInventoryResaleItem) Then
Dim inventoryItem As NonInventoryResaleItem = DirectCast(recordItem, NonInventoryResaleItem)
netSuiteInformation = New PSNetSuiteItemType
With netSuiteInformation
.ItemExists = True
.InternalID = inventoryItem.internalId
.NetSuiteCost = CDec(inventoryItem.cost)
.IsDropShipItem = inventoryItem.isDropShipItem
.IsSpecialOrderItem = inventoryItem.isSpecialOrderItem
.NetSuitePurchaseDescription = inventoryItem.purchaseDescription
.NetSuiteSalesDescription = inventoryItem.salesDescription
.NetSuiteStoreDescription = inventoryItem.storeDescription
End With
End If
Return netSuiteInformation
Catch ex As Exception
Throw
End Try
End Function
My ultimate goal for this method is to cast the Record object into the correct type without me hard coding that type, then with the properties that are common to all derived objects I assign them, but with properties that are unique to a particular object, I would check to see if that property exists and assign it only if it does. I am not 100% sure if it is possible, though.
Ben
Using Visual Basic 2005/2008
-
Sep 9th, 2008, 03:59 PM
#6
Re: [2008] [.NET 2.0] Dynamically Create object of unknown type
If it is, I would say it is through interfaces. I originally thought that your example was the whole code, so the variable you scoped within the If block would not be useful outside of it. Evidently, you have more stuff inside the if block.
Suppose you created an interface that implemented ALL the properties and methods common to all the functions. For those that didn't need property X, nothing would happen (it would be an empty function). Then you could pass the interface in rather than the InventoryItem and you could get rid of the If statement entirely because the interface would stand in for any type of class. Of course, this means that all varieties of InventoryItem would need to implement the interface, which could potentially be more work than the If statement. Still, I think you should be thinking more about interfaces as a solution.
My usual boring signature: Nothing
 
-
Sep 9th, 2008, 04:18 PM
#7
Thread Starter
Hyperactive Member
Re: [2008] [.NET 2.0] Dynamically Create object of unknown type
Yah, unfortunately InventoryItem is a part of the NetSuite web service API so I can't do anything with it. Maybe, this is just a pipe dream. I will keep looking, but I may just have to buckle down and add more elements to the if statement. Or, maybe someone else will have a better idea.
It would be really nice if I could do something like this.
Code:
Dim item as recordItem.GetType = DirectCast(recordItem, recordItem.GetType)
Ben
Using Visual Basic 2005/2008
-
Sep 9th, 2008, 04:42 PM
#8
Re: [2008] [.NET 2.0] Dynamically Create object of unknown type
How many If statements will you end up with? Will the number keep increasing?
I can think of no solution that is reasonably superior to the one you have. I can think (vaguely) of some ugly solutions. The real key question is: What will be the cost and frequency of just adding a further IF statement? If that will be so painful that you really need to split this particular functionality out into a separate module such that you can update just that module, then so be it. Otherwise, you may have the best solution you will find.
My usual boring signature: Nothing
 
-
Sep 9th, 2008, 05:03 PM
#9
Re: [2008] [.NET 2.0] Dynamically Create object of unknown type
 Originally Posted by Krenshau
The code I have up there does work.
I find it odd that you say the code works as expected...
Consider the following class:
Code:
Public Class InventoryItem
End Class
Now if I were to create an instance of that class (we will call the variable recordItem to comply with your example).
Code:
Dim recordItem As New InventoryItem
If recordItem Is GetType(InventoryItem) Then
MessageBox.Show("recordItem is type InventoryItem")
Else
MessageBox.Show("This doesn't work right")
End If
now according to you, I should see the message of "recordItem is type InventoryItem" when I run this code, however I don't. I get the "This doesn't work right" message.
The correct code to use is the TypeOf keyword, along with the Is keyword. If I change your code as follows, the results are as expected.
Code:
Dim recordItem As New InventoryItem
If TypeOf recordItem Is InventoryItem Then
MessageBox.Show("recordItem is type InventoryItem")
Else
MessageBox.Show("this doesnt work right")
End If
Not sure if I am answering your question here, but something looks fundamentally wrong with the original code you posted, unless I am missing something.
-
Sep 9th, 2008, 05:08 PM
#10
Thread Starter
Hyperactive Member
Re: [2008] [.NET 2.0] Dynamically Create object of unknown type
At this point, I only need to add two, I think. It is already in a dll by itself (well, with similar functionality). I was just trying to think ahead because anytime we make changes we charge the customer, so I was trying to avoid the customer getting annoyed every time NetSuite makes changes.
I have been doing a bit of searching, and I think you are right. If I was using .NET 3.5 I could have .NET infer the type which would save a half the code, but I would still need to know the type for the cast. That isn't an option, though.
Thank you for your help.
Ben
Using Visual Basic 2005/2008
-
Sep 9th, 2008, 05:11 PM
#11
Thread Starter
Hyperactive Member
Re: [2008] [.NET 2.0] Dynamically Create object of unknown type
Kleinma,
You are close with the if statement, but not quite right. I have
Code:
If recordItem.GetType Is GetType(InventoryItem) Then
That is why it works. You forgot the recordItem.GetType
I will meet you half way and say that my first post is the way you have it, which is my mistake for not retyping it correctly, but I did post the complete code later.
Ben
Using Visual Basic 2005/2008
-
Sep 9th, 2008, 06:14 PM
#12
Re: [2008] [.NET 2.0] Dynamically Create object of unknown type
 Originally Posted by Krenshau
I was just trying to think ahead because anytime we make changes we charge the customer, so I was trying to avoid the customer getting annoyed every time NetSuite makes changes.
Well, first off, in my opin, you shouldn't be charging for changes that YOU make because of someother vendor. That just seems wrong.
secondly, another thought, use an overloaded sub... then the correct version would get called.... if a new type shows, up add the new sub with that new type, and you're done. I don't see why that wouldn't work. If recordItem.GetType is returning its correct actual type (and not the interface Record type)....which it appears to be doing, since the IFs are working.... then it should be able to get the correct sub based on the parameter types....
Ahhh.... Okay.... I think I'm starting to get what you are doing there..... if you really want something flexible and virtually maintenance free.... you may want to look at using reflection. Using that, you can get to the properties and extract them... then you shouldn't care what the type is at all.
-tg
-
Sep 9th, 2008, 06:18 PM
#13
Re: [2008] [.NET 2.0] Dynamically Create object of unknown type
 Originally Posted by techgnome
Ahhh.... Okay.... I think I'm starting to get what you are doing there..... if you really want something flexible and virtually maintenance free.... you may want to look at using reflection. Using that, you can get to the properties and extract them... then you shouldn't care what the type is at all.
-tg
TG had to reflect on the problem a bit.
My usual boring signature: Nothing
 
-
Sep 9th, 2008, 06:26 PM
#14
Re: [2008] [.NET 2.0] Dynamically Create object of unknown type
It wasn't until I saw the full code that I could see what he's trying to do and why.... but upon further reflection I think that reflection might be ideal in this case.
-tg
-
Sep 9th, 2008, 06:51 PM
#15
Thread Starter
Hyperactive Member
Re: [2008] [.NET 2.0] Dynamically Create object of unknown type
Hey, thanks guys for your help. I will do some reading on reflection tomorrow. I have never used it before. Sorry, my original post wasn't very clear. Thanks for everything. I might be posting some questions about reflection tomorrow.
BTW, for clarification. We created this custom software for a business so they could pull sales that are generated on their website out of their database and import them into NetSuite using the NetSuite web service. It worked when NetSuites web service was version 2.5, and it doesn't make good business sense for use to maintain their custom software indefinitely for no fee. However, if we charged a subscription fee or if we were ISVs selling software in bulk then I would agree. I am not the boss, so I don't set the business model, though.
Ben
Using Visual Basic 2005/2008
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|