StringToObject

  • Thread starter Thread starter Albert
  • Start date Start date
A

Albert

Hi guys,

hope someone can help me out here.

Got two dll's

dll1 and dll2

this one works

dim myObj as dll1
dim myObj2 as dll2
dim iInt as integer

iInt = dll1.somefunction

Ok now what I want

dim sString as string="dll1"

dim myObj as sString <------ No good

How do I get this to work?
 
Hi Albert
Sure this can't be done
You are using the Dim key word and the As keyword , the complier expects
to find a type after the word as . what you are presenting is a variable .
of course , no compiler in the world would take this variable , figure its
value and then try to see if it match a type that we have ???? imagine you
are saying
dim K as String="interger"
Dim myint as K ' of course the complier will never understand that you want
to declare in it .
Whoever, if your logic require declaring a variable of a type that is not
defined until run time , I am sure there are many ways to achieve that , a
simple solution for example declare as many variables of all the possible
types and then use that one that you will want.
Hope that was clear

Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC
 
Mohamed,

I know this can't be done this way, that's why I posted my question ;-)
You tell me declaring a variable of a type that is not defined until runtime
can be done in many ways. Well I can't seem to find any.
Declaring all the variables that may or may not be used, is not an option.

Can someone else maybe shed some light on this issue?

Thanks,

Albert
 
Albert,

After reading this 10 times I did not understand what you want and could
only give an answer as Mohamoss did.

dim sString as string="dll1"
dim myObj as sString <------ No good

You told how it works and this does not work, that is true, as Mohamoss
said.

And?

Cor
 
Cor,

ok might be a bad example.
Imagine a project
I've got two dll's referenced, dll1 and dll2

within some function of my project i want to use a function out of dll1/dll2
Normally you then would put in the function:

Function fncSomeProjFunc as int

dim myobj as dll1 (Make the functions of dll1 available inside this
function)
dim myobj2 as dll2

dim sResult as string
dim sResult2 as string

sResult = myobj.SomeFunctionwithsResultAsReturnValue
sResult2 = myobj2.SomeFunctionwithsResultAsReturnValue

return 1
end function

this is what Mohammed suggested, agree?

Now what i want is this

Function fncSomeProjFunc(byval sDllToCall as String) as int
..
...
....
.....

end function

sDllToCall is either "dll1" or "dll2" being the NAME of the object that I
have to declare.
How can I use sDllToCall to declare an object?

If you for example look at it the other way around,

dim myobj as dll1

msgbox (myobj.toString) would give "dll1"
so maybe there is some function like myobj=sdlltocall.StringToObject or
something like this?

I don't know how to explain it more clearly.
 
* "Albert said:

Please avoid multiposts.

\\\
Private Function CreateClassByName( _
ByVal PartialAssemblyName As String, _
ByVal QualifiedClassName As String _
) As Object
Return _
Activator.CreateInstance( _
[Assembly].LoadWithPartialName( _
PartialAssemblyName _
).GetType(QualifiedClassName) _
)
End Function
///

Usage:

\\\
Dim c As Control = _
DirectCast( _
CreateClassByName( _
"System.Windows.Forms", _
"System.Windows.Forms.Button" _
), _
Control _
)
With c
.Location = New Point(10, 10)
.Size = New Size(80, 26)
.Text = "Hello World"
End With
Me.Controls.Add(c)
///
 
Hi Albert,

I was busy with it however I see you got an answer from Herfried,

Cor
 
Albert said:
Mohamed,

I know this can't be done this way, that's why I posted my question ;-)
You tell me declaring a variable of a type that is not defined until runtime
can be done in many ways. Well I can't seem to find any.
Declaring all the variables that may or may not be used, is not an option.

Can someone else maybe shed some light on this issue?

Thanks,

Albert


I have a vague feeling that your answer will involve delegates, even
if they don't solve your problem as stated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top