Error using object type array.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • William Apple

    Error using object type array.

    Despite the fact this deals with webservices I believe it is a VB question.

    I am working on a test application that passes data to a webservice.
    The webservices takes a variable type that is defined below:

    Public Class Variable
    Inherits MarshalByRefObj ect

    '<remarks/>
    Public strVariableName As String

    '<remarks/>
    Public objVariableValu e() As Object
    End Class

    My code is as follows
    Dim varList(2) as Variable

    varList(0) = New Variable
    varList(0).strV ariableName = "Name"
    varList(0).objV ariableValue = New Object <- Error Occurs Here
    varList(0).objV ariableValue(0) = name

    If I use the above code I get a "Specified Cast is not Valid" error.

    If I code it this way...

    Dim varList(2) as Variable

    varList(0) = New Variable
    varList(0).strV ariableName = "Name"
    varList(0).objV ariableValue(0) = name <-- Error Occurs here

    I get "Object reference not set to an instance of an object." as my
    error. What am I missing?


  • Chris Dunaway

    #2
    Re: Error using object type array.

    On Tue, 28 Sep 2004 09:14:33 -0400, William Apple wrote:
    [color=blue]
    > Public Class Variable
    > Inherits MarshalByRefObj ect
    >
    > '<remarks/>
    > Public strVariableName As String
    >
    > '<remarks/>
    > Public objVariableValu e() As Object
    > End Class
    >
    > My code is as follows
    > Dim varList(2) as Variable
    >
    > varList(0) = New Variable
    > varList(0).strV ariableName = "Name"
    > varList(0).objV ariableValue = New Object <- Error Occurs Here
    > varList(0).objV ariableValue(0) = name[/color]

    You've declared objVariableValu e as an array of objects, so you need to
    initialize it as an array of objects and specify a size for the array:

    Public objVariableValu e(size) As Object

    Or if you didn't mean for objVariableValu e to be an array of objects,
    remove the parentheses in the declaration:

    '<remarks/>
    Public objVariableValu e As Object

    Hope this helps

    --
    Chris

    dunawayc[AT]sbcglobal_lunch meat_[DOT]net

    To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
    replace certain words in my E-Mail address.

    Comment

    • Imran Koradia

      #3
      Re: Error using object type array.

      Chris has answered the first part of your problem. For the second part:
      [color=blue]
      > If I code it this way...
      >
      > Dim varList(2) as Variable
      >
      > varList(0) = New Variable
      > varList(0).strV ariableName = "Name"
      > varList(0).objV ariableValue(0) = name <-- Error Occurs here[/color]

      What variable is 'name'? I don't see it being declared anywhere in the piece
      of code you've posted. Is this declared somewhere else? Or did you mean
      something like:
      varList(0).objV ariableValue(0) = "name"
      If so, then you do need to create an instance of the object before assigning
      it a value:
      varList(0).objV ariableValue(0) = New Object
      varList(0).objV ariableValue(0) = "name"


      hope that helps..
      Imran.


      Comment

      • William Apple

        #4
        Re: Error using object type array.

        I didn't write the webservice, so I have no control over the declaration of
        objVariableValu e. Is it possible to get this to work or is it a bug on their
        part?

        "Chris Dunaway" <"dunawayc[[at]_lunchmeat_sbcg lobal[dot]]net"> wrote in
        message news:ylp1xrrxz4 nh$.tycvapsoapx u$.dlg@40tude.n et...[color=blue]
        > On Tue, 28 Sep 2004 09:14:33 -0400, William Apple wrote:
        >[color=green]
        > > Public Class Variable
        > > Inherits MarshalByRefObj ect
        > >
        > > '<remarks/>
        > > Public strVariableName As String
        > >
        > > '<remarks/>
        > > Public objVariableValu e() As Object
        > > End Class
        > >
        > > My code is as follows
        > > Dim varList(2) as Variable
        > >
        > > varList(0) = New Variable
        > > varList(0).strV ariableName = "Name"
        > > varList(0).objV ariableValue = New Object <- Error Occurs Here
        > > varList(0).objV ariableValue(0) = name[/color]
        >
        > You've declared objVariableValu e as an array of objects, so you need to
        > initialize it as an array of objects and specify a size for the array:
        >
        > Public objVariableValu e(size) As Object
        >
        > Or if you didn't mean for objVariableValu e to be an array of objects,
        > remove the parentheses in the declaration:
        >
        > '<remarks/>
        > Public objVariableValu e As Object
        >
        > Hope this helps
        >
        > --
        > Chris
        >
        > dunawayc[AT]sbcglobal_lunch meat_[DOT]net
        >
        > To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
        > replace certain words in my E-Mail address.[/color]


        Comment

        • William Apple

          #5
          Re: Error using object type array.

          My code should have read.....the variable name was declared. How I wish it
          was simple stupidity on my part.

          Dim varList(2) as Variable
          Dim name As Object

          name = "xyz"

          varList(0) = New Variable
          varList(0).strV ariableName = "Name"
          varList(0).objV ariableValue = New Object <- Error Occurs Here
          varList(0).objV ariableValue(0) = name

          If I use the above code I get a "Specified Cast is not Valid" error.

          If I code it this way...

          Dim varList(2) as Variable

          varList(0) = New Variable
          varList(0).strV ariableName = "Name"
          varList(0).objV ariableValue(0) = name <-- Error Occurs here
          "Imran Koradia" <nospam@microso ft.com> wrote in message
          news:uaGiS$VpEH A.132@TK2MSFTNG P14.phx.gbl...[color=blue]
          > Chris has answered the first part of your problem. For the second part:
          >[color=green]
          > > If I code it this way...
          > >
          > > Dim varList(2) as Variable
          > >
          > > varList(0) = New Variable
          > > varList(0).strV ariableName = "Name"
          > > varList(0).objV ariableValue(0) = name <-- Error Occurs here[/color]
          >
          > What variable is 'name'? I don't see it being declared anywhere in the[/color]
          piece[color=blue]
          > of code you've posted. Is this declared somewhere else? Or did you mean
          > something like:
          > varList(0).objV ariableValue(0) = "name"
          > If so, then you do need to create an instance of the object before[/color]
          assigning[color=blue]
          > it a value:
          > varList(0).objV ariableValue(0) = New Object
          > varList(0).objV ariableValue(0) = "name"
          >
          >
          > hope that helps..
          > Imran.
          >
          >[/color]


          Comment

          • Imran Koradia

            #6
            Re: Error using object type array.

            The problem is that you've not defined the length of the objVariableValu e
            array. So, you should do something like this:

            Dim varList(2) as Variable
            Dim name As Object

            name = "xyz"

            ' this will make your object array of length 3
            ReDim varList(0).objV ariableValue(2)

            varList(0) = New Variable
            varList(0).strV ariableName = "Name"
            varList(0).objV ariableValue(0) = New Object
            varList(0).objV ariableValue(0) = name


            hope that helps..
            Imran.

            "William Apple" <wapple@nospa m-cisys.com> wrote in message
            news:uYqgIEWpEH A.3868@TK2MSFTN GP15.phx.gbl...[color=blue]
            > My code should have read.....the variable name was declared. How I wish it
            > was simple stupidity on my part.
            >
            > Dim varList(2) as Variable
            > Dim name As Object
            >
            > name = "xyz"
            >
            > varList(0) = New Variable
            > varList(0).strV ariableName = "Name"
            > varList(0).objV ariableValue = New Object <- Error Occurs Here
            > varList(0).objV ariableValue(0) = name
            >
            > If I use the above code I get a "Specified Cast is not Valid" error.
            >
            > If I code it this way...
            >
            > Dim varList(2) as Variable
            >
            > varList(0) = New Variable
            > varList(0).strV ariableName = "Name"
            > varList(0).objV ariableValue(0) = name <-- Error Occurs here
            > "Imran Koradia" <nospam@microso ft.com> wrote in message
            > news:uaGiS$VpEH A.132@TK2MSFTNG P14.phx.gbl...[color=green]
            > > Chris has answered the first part of your problem. For the second part:
            > >[color=darkred]
            > > > If I code it this way...
            > > >
            > > > Dim varList(2) as Variable
            > > >
            > > > varList(0) = New Variable
            > > > varList(0).strV ariableName = "Name"
            > > > varList(0).objV ariableValue(0) = name <-- Error Occurs here[/color]
            > >
            > > What variable is 'name'? I don't see it being declared anywhere in the[/color]
            > piece[color=green]
            > > of code you've posted. Is this declared somewhere else? Or did you mean
            > > something like:
            > > varList(0).objV ariableValue(0) = "name"
            > > If so, then you do need to create an instance of the object before[/color]
            > assigning[color=green]
            > > it a value:
            > > varList(0).objV ariableValue(0) = New Object
            > > varList(0).objV ariableValue(0) = "name"
            > >
            > >
            > > hope that helps..
            > > Imran.
            > >
            > >[/color]
            >
            >[/color]


            Comment

            • William Apple

              #7
              Re: Error using object type array.

              Thanks, that works. I also found this to work also

              varlist(0).objV ariableValue = New Object(2) {}

              "Imran Koradia" <nospam@microso ft.com> wrote in message
              news:%23z3yINWp EHA.2948@TK2MSF TNGP11.phx.gbl. ..[color=blue]
              > The problem is that you've not defined the length of the objVariableValu e
              > array. So, you should do something like this:
              >
              > Dim varList(2) as Variable
              > Dim name As Object
              >
              > name = "xyz"
              >
              > ' this will make your object array of length 3
              > ReDim varList(0).objV ariableValue(2)
              >
              > varList(0) = New Variable
              > varList(0).strV ariableName = "Name"
              > varList(0).objV ariableValue(0) = New Object
              > varList(0).objV ariableValue(0) = name
              >
              >
              > hope that helps..
              > Imran.
              >
              > "William Apple" <wapple@nospa m-cisys.com> wrote in message
              > news:uYqgIEWpEH A.3868@TK2MSFTN GP15.phx.gbl...[color=green]
              > > My code should have read.....the variable name was declared. How I wish[/color][/color]
              it[color=blue][color=green]
              > > was simple stupidity on my part.
              > >
              > > Dim varList(2) as Variable
              > > Dim name As Object
              > >
              > > name = "xyz"
              > >
              > > varList(0) = New Variable
              > > varList(0).strV ariableName = "Name"
              > > varList(0).objV ariableValue = New Object <- Error Occurs Here
              > > varList(0).objV ariableValue(0) = name
              > >
              > > If I use the above code I get a "Specified Cast is not Valid" error.
              > >
              > > If I code it this way...
              > >
              > > Dim varList(2) as Variable
              > >
              > > varList(0) = New Variable
              > > varList(0).strV ariableName = "Name"
              > > varList(0).objV ariableValue(0) = name <-- Error Occurs here
              > > "Imran Koradia" <nospam@microso ft.com> wrote in message
              > > news:uaGiS$VpEH A.132@TK2MSFTNG P14.phx.gbl...[color=darkred]
              > > > Chris has answered the first part of your problem. For the second[/color][/color][/color]
              part:[color=blue][color=green][color=darkred]
              > > >
              > > > > If I code it this way...
              > > > >
              > > > > Dim varList(2) as Variable
              > > > >
              > > > > varList(0) = New Variable
              > > > > varList(0).strV ariableName = "Name"
              > > > > varList(0).objV ariableValue(0) = name <-- Error Occurs here
              > > >
              > > > What variable is 'name'? I don't see it being declared anywhere in the[/color]
              > > piece[color=darkred]
              > > > of code you've posted. Is this declared somewhere else? Or did you[/color][/color][/color]
              mean[color=blue][color=green][color=darkred]
              > > > something like:
              > > > varList(0).objV ariableValue(0) = "name"
              > > > If so, then you do need to create an instance of the object before[/color]
              > > assigning[color=darkred]
              > > > it a value:
              > > > varList(0).objV ariableValue(0) = New Object
              > > > varList(0).objV ariableValue(0) = "name"
              > > >
              > > >
              > > > hope that helps..
              > > > Imran.
              > > >
              > > >[/color]
              > >
              > >[/color]
              >
              >[/color]


              Comment

              • Imran Koradia

                #8
                Re: Error using object type array.

                Yup - thats fine too. However, if you just want to assign the variable
                'name' to the object, you don't necessarily have to create a new instance
                (which is what's happening with New Object(2) {}) since the instance already
                exists (the 'name' instance) and you directly assign the reference to it.
                Just a thought...

                Imran.

                "William Apple" <wapple@nospa m-cisys.com> wrote in message
                news:uTKjyQWpEH A.2636@TK2MSFTN GP09.phx.gbl...[color=blue]
                > Thanks, that works. I also found this to work also
                >
                > varlist(0).objV ariableValue = New Object(2) {}
                >
                > "Imran Koradia" <nospam@microso ft.com> wrote in message
                > news:%23z3yINWp EHA.2948@TK2MSF TNGP11.phx.gbl. ..[color=green]
                > > The problem is that you've not defined the length of the[/color][/color]
                objVariableValu e[color=blue][color=green]
                > > array. So, you should do something like this:
                > >
                > > Dim varList(2) as Variable
                > > Dim name As Object
                > >
                > > name = "xyz"
                > >
                > > ' this will make your object array of length 3
                > > ReDim varList(0).objV ariableValue(2)
                > >
                > > varList(0) = New Variable
                > > varList(0).strV ariableName = "Name"
                > > varList(0).objV ariableValue(0) = New Object
                > > varList(0).objV ariableValue(0) = name
                > >
                > >
                > > hope that helps..
                > > Imran.
                > >
                > > "William Apple" <wapple@nospa m-cisys.com> wrote in message
                > > news:uYqgIEWpEH A.3868@TK2MSFTN GP15.phx.gbl...[color=darkred]
                > > > My code should have read.....the variable name was declared. How I[/color][/color][/color]
                wish[color=blue]
                > it[color=green][color=darkred]
                > > > was simple stupidity on my part.
                > > >
                > > > Dim varList(2) as Variable
                > > > Dim name As Object
                > > >
                > > > name = "xyz"
                > > >
                > > > varList(0) = New Variable
                > > > varList(0).strV ariableName = "Name"
                > > > varList(0).objV ariableValue = New Object <- Error Occurs Here
                > > > varList(0).objV ariableValue(0) = name
                > > >
                > > > If I use the above code I get a "Specified Cast is not Valid" error.
                > > >
                > > > If I code it this way...
                > > >
                > > > Dim varList(2) as Variable
                > > >
                > > > varList(0) = New Variable
                > > > varList(0).strV ariableName = "Name"
                > > > varList(0).objV ariableValue(0) = name <-- Error Occurs here
                > > > "Imran Koradia" <nospam@microso ft.com> wrote in message
                > > > news:uaGiS$VpEH A.132@TK2MSFTNG P14.phx.gbl...
                > > > > Chris has answered the first part of your problem. For the second[/color][/color]
                > part:[color=green][color=darkred]
                > > > >
                > > > > > If I code it this way...
                > > > > >
                > > > > > Dim varList(2) as Variable
                > > > > >
                > > > > > varList(0) = New Variable
                > > > > > varList(0).strV ariableName = "Name"
                > > > > > varList(0).objV ariableValue(0) = name <-- Error Occurs here
                > > > >
                > > > > What variable is 'name'? I don't see it being declared anywhere in[/color][/color][/color]
                the[color=blue][color=green][color=darkred]
                > > > piece
                > > > > of code you've posted. Is this declared somewhere else? Or did you[/color][/color]
                > mean[color=green][color=darkred]
                > > > > something like:
                > > > > varList(0).objV ariableValue(0) = "name"
                > > > > If so, then you do need to create an instance of the object before
                > > > assigning
                > > > > it a value:
                > > > > varList(0).objV ariableValue(0) = New Object
                > > > > varList(0).objV ariableValue(0) = "name"
                > > > >
                > > > >
                > > > > hope that helps..
                > > > > Imran.
                > > > >
                > > > >
                > > >
                > > >[/color]
                > >
                > >[/color]
                >
                >[/color]


                Comment

                Working...