Showing posts with label collection. Show all posts
Showing posts with label collection. Show all posts

Saturday, March 31, 2012

System.InvalidCastException

I have UI Page.aspx which has this class:
PAGE.ASPX
private class Blah
public bVar as boolean
end class
On page load this class is added to the collection ColBlah and sent to the
function in business layer which has identical class defined.
BusLayer.vb
private class Blah
bVar as boolean
end class
class BusLayer
Public function Process(oCol as collection)
dim oObject, oBlah as Blah
for each oObject in oCol
oBlah=CType(oObject,Blah)
next oCol
end class
the line oBlah=CType(oObject,Blah) generates the following exception
System.InvalidCastException
Why? Anybody?A class can't exist in two places at the same time. Just because they have
the same name and share all the same members, doesn't make them the same.
As far as .NET is concerned, you might as well be trying to cast a textbox
to a database connection.
This class should be defined in your business layer ONLY...and your aspx
page should create an instance of THAT class.
Karl
http://www.openmymind.net/
http://www.codebetter.com/
"Mike Kansky" <MikeKansky@.discussions.microsoft.com> wrote in message
news:8DD38BB8-E1A6-4C92-8629-918A5F3FAC83@.microsoft.com...
>I have UI Page.aspx which has this class:
> PAGE.ASPX
> private class Blah
> public bVar as boolean
> end class
>
> On page load this class is added to the collection ColBlah and sent to the
> function in business layer which has identical class defined.
> BusLayer.vb
> private class Blah
> bVar as boolean
> end class
> class BusLayer
> Public function Process(oCol as collection)
> dim oObject, oBlah as Blah
> for each oObject in oCol
> oBlah=CType(oObject,Blah)
> next oCol
> end class
> the line oBlah=CType(oObject,Blah) generates the following exception
> System.InvalidCastException
> Why? Anybody?
>

Wednesday, March 28, 2012

System.InvalidOperationException: Collection was modified; enumeration operation may not e

hi all,

i am getting the following error when i am trying to move the ltems form listbox1 to listbox2

"System.InvalidOperationException: Collection was modified; enumeration operation may not execute"

ForEach itemAs ListItemIn Listbox1.Items

If item.SelectedAndNot Listbox2.Items.Contains(item)Then

Dim newItemAsNew ListItem(item.Text, item.Value)

Listbox2.Items.Add(newItem)

listbox1.Remove(newitem)

EndIf

Next

thanks.

You can't modify the listbox1 item collection while you are iterating over it using "for each". Wait until you add everything to listbox2, and then call Clear() on listbox1.

Thursday, March 22, 2012

system.object to namevalue collection

I'm reading some stuff in a custom section of the
web.config file into a name value collection. What do I
need to do to make this work:

Dim nvcConfig As NameValueCollection = _
ConfigurationSettings.GetConfig
("myApp/mySection ")

With option strict on?See:

http://msdn.microsoft.com/library/d...ionsections.asp

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
The more I learn, the less I know.

"Charles" <a@.b.c> wrote in message
news:0b3c01c36e2f$fca64820$a401280a@.phx.gbl...
> I'm reading some stuff in a custom section of the
> web.config file into a name value collection. What do I
> need to do to make this work:
> Dim nvcConfig As NameValueCollection = _
> ConfigurationSettings.GetConfig
> ("myApp/mySection ")
> With option strict on?