[RESOLVED] Object Problem - Simple to fix, am I missing something?
Hi guys :wave:, 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 :thumb: :)
Thanks in advance, knxrb.
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.
Re: Object Problem - Simple to fix, am I missing something?
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?
Re: Object Problem - Simple to fix, am I missing something?
Quote:
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.
Re: Object Problem - Simple to fix, am I missing something?
Ok, thanks Kevin.
I'll run through and check the line you've pointed out and see if it's where the problem is.
If it doesn't fix it then I'll make a working copy and upload it.
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" }
Re: Object Problem - Simple to fix, am I missing something?
Quote:
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
Re: Object Problem - Simple to fix, am I missing something?
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.
[EDIT] Removed source attachment deliberately.
Re: Object Problem - Simple to fix, am I missing something?
Quote:
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)
Re: Object Problem - Simple to fix, am I missing something?
Thanks Kevin, after looking through the code fix you sent I've fixed all errors and it works exactly as it's supposed to.
Thanks for your time and effort in helping me, I realy appreciate it ;)
Robert