Results 1 to 10 of 10

Thread: [RESOLVED] Object Problem - Simple to fix, am I missing something?

  1. #1

    Thread Starter
    Hyperactive Member knxrb's Avatar
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    321

    Resolved [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

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    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.

  3. #3

    Thread Starter
    Hyperactive Member knxrb's Avatar
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    321

    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?
    Did I help you with your problem? If I did rate me by clicking here: Rate knxrb

  4. #4
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Object Problem - Simple to fix, am I missing something?

    Quote Originally Posted by knxrb View Post
    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.

  5. #5

    Thread Starter
    Hyperactive Member knxrb's Avatar
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    321

    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.
    Did I help you with your problem? If I did rate me by clicking here: Rate knxrb

  6. #6

    Thread Starter
    Hyperactive Member knxrb's Avatar
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    321

    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

  7. #7
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Object Problem - Simple to fix, am I missing something?

    Quote Originally Posted by knxrb View Post
    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

  8. #8

    Thread Starter
    Hyperactive Member knxrb's Avatar
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    321

    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.
    Last edited by knxrb; Jan 26th, 2011 at 11:44 AM.
    Did I help you with your problem? If I did rate me by clicking here: Rate knxrb

  9. #9
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Object Problem - Simple to fix, am I missing something?

    Quote Originally Posted by knxrb View Post
    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.
    1. Use what I have attached
    2. This version has Option Strict On
    3. Several slight changes in the form project
    4. Business logic dll loaded a specific way and if for some reason the DLL does not exists it will not be loaded
    5. 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.

  10. #10

    Thread Starter
    Hyperactive Member knxrb's Avatar
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    321

    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
    Did I help you with your problem? If I did rate me by clicking here: Rate knxrb

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width