|
-
Jan 25th, 2011, 07:47 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Object Problem - Simple to fix, am I missing something?
Hi guys , I have this code where an object is made and it needs to be filled with other objects depending on a string value.
The code is:
Code:
Dim o As Object = Activator.CreateInstance(LoadedType)
Dim oo As Object() = New Object() {}
For pI As Integer = 0 To strParamTypes.Split(",").Length - 1
Dim strParam As String = strParamTypes.Split(",")(pI)
Select Case strParam.ToLower
Case "s"
oo(pI) = Convert.ToString(strParams.Split(",")(pI))
Case "i"
oo(pI) = Convert.ToInt32(strParams.Split(",")(pI))
Case "f"
oo(pI) = Convert.ToDecimal(strParams.Split(",")(pI))
Case "b"
oo(pI) = Convert.ToBoolean(strParams.Split(",")(pI))
End Select
Next
retStr = Convert.ToString(LoadedMethod.Invoke(o, oo))
An example of strParamTypes: "I,I" (To tell the program that both need to be converted to integers for the oo object.)
An example of strParams: "1,5"
The type 'LoadedType' and the method 'LoadedMethod' are loaded perfectly with no problems whatsoever.
The idea of the above code is that it invokes the fucntion in another DLL giving the required parameters as specified in the strings, seperated with commas and it should then return the value given back by the function.
For some reason I either get an error saying "Object reference not set to and isntance of an object" or one saying the "Index is out of bounds".
I've narrowed the object error down to the oo object not being filled properly and I presume the "index out of bounds" error is something to do wwith the string splitting, but I can't work out what the solution to any of the problems is.
I've been working at this for a good few hours now and it's probably something simple I keep missing but if any of you can help then I'd appreciate it 
Thanks in advance, knxrb.
Last edited by knxrb; Jan 25th, 2011 at 08:10 PM.
Did I help you with your problem? If I did rate me by clicking here: Rate knxrb

-
Jan 25th, 2011, 08:13 PM
#2
Re: Object Problem - Simple to fix, am I missing something?
Not sure as there is not enough information at least for me but can see you need to set Option Strict = On since when I looked at your code with Option Strict On there were issues i.e.
This
Code:
Dim strParam As String = strParamTypes.Split(",")(pI)
Should be
Code:
Dim strParam As String = strParamTypes.Split(","c)(pI)
Perhaps turning Option Strict On will reveal more issues that can assist in determining what is going on.
-
Jan 25th, 2011, 08:17 PM
#3
Thread Starter
Hyperactive Member
-
Jan 25th, 2011, 08:42 PM
#4
Re: Object Problem - Simple to fix, am I missing something?
 Originally Posted by knxrb
Thanks kevin, after doing that I changed them and added the 'c' characters like you said and no other problems arose.
It does seem to have eliminated the 'Object reference not set...' error and I am now getting the index one: "Index was outside the bounds of the array."
Any ideas, I can't work it out at all :S?
This is where stepping thru the code would be helpful.
This should be okay from simply scanning
Code:
For pI As Integer = 0 To strParamTypes.Split(",").Length - 1
My guess is this line is the problem
Code:
Dim strParam As String = strParamTypes.Split(",")(pI)
But of course can not run your code without mocking up missing objects.
-
Jan 25th, 2011, 08:44 PM
#5
Thread Starter
Hyperactive Member
-
Jan 25th, 2011, 09:05 PM
#6
Thread Starter
Hyperactive Member
Re: Object Problem - Simple to fix, am I missing something?
Ok, I've fixed the 'index out of bounds' error now, thanks 
Now for the 'object reference' error I've narrowed it down to this line causing the problem:
Code:
Dim oo As Object() = New Object() {}
Apparently their isn't enough space for each parameter so I tried this:
Code:
Dim oo As Object() = New Object(strParamTypes.Split(","c).Length - 1) {}
But that fails as well.
My only thought can be it's because of the {} brackets at the end with nothing in them.
The problem now is that I don't know how to fill the object() with the parameters based on the string value as something like the below would require hardcoding the values in.
Code:
Dim oo As Object() = New Object() { 2, 3.5, "A String" }
Did I help you with your problem? If I did rate me by clicking here: Rate knxrb

-
Jan 25th, 2011, 09:20 PM
#7
Re: Object Problem - Simple to fix, am I missing something?
 Originally Posted by knxrb
Ok, I've fixed the 'index out of bounds' error now, thanks
Now for the 'object reference' error I've narrowed it down to this line causing the problem:
Code:
Dim oo As Object() = New Object() {}
Apparently their isn't enough space for each parameter so I tried this:
Code:
Dim oo As Object() = New Object(strParamTypes.Split(","c).Length - 1) {}
But that fails as well.
My only thought can be it's because of the {} brackets at the end with nothing in them.
The problem now is that I don't know how to fill the object() with the parameters based on the string value as something like the below would require hardcoding the values in.
Code:
Dim oo As Object() = New Object() { 2, 3.5, "A String" }
How about
Code:
Dim oo As New ArrayList
oo.Add("Kevin")
oo.Add(100)
For Each item In oo
Console.WriteLine("{0} {1}", item, IsNumeric(item))
Next
-
Jan 26th, 2011, 06:34 AM
#8
Thread Starter
Hyperactive Member
-
Jan 26th, 2011, 09:25 AM
#9
Re: Object Problem - Simple to fix, am I missing something?
 Originally Posted by knxrb
Hi Kevin, the ArrayList seems to work fine for adding parameters but the "object reference" error is still being thrown.
I've attached a working copy of the source so you can see it and maybe work out what's going wrong :S?
It includes the code I'm trying to run and also the code for the 'BusinessLogic' DLL I'm using as a basic example to run a method from.
I had little time to look at this but here is the deal.
- Use what I have attached
- This version has Option Strict On
- Several slight changes in the form project
- Business logic dll loaded a specific way and if for some reason the DLL does not exists it will not be loaded
- Entire solution compiles clean, meaning no errors but needs work on the Invoke code as I did not have time to see what it needs to work
Lastly, there is no need to prefix variables with their type and also I highly suggest no one character variable names.
So in regards to prefix
Code:
Dim strParam As String = strParamTypes.Split(","c)(pI)
Might be
Code:
Dim InstanceParameters As String = ParamTypes.Split(","c)(pI)
This
Code:
Dim o As Object = Activator.CreateInstance(LoadedType)
to this
Code:
Dim Instance As Object = Activator.CreateInstance(LoadedType)
Last edited by kareninstructor; Jan 26th, 2011 at 11:53 AM.
Reason: Removal of attachment as per poster request.
-
Jan 26th, 2011, 11:42 AM
#10
Thread Starter
Hyperactive Member
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
|