From: Tony Johansson on
Hi!

As you can see I have defined namespace ConnectionStringDemo in the console
app. From Main I call method GetSection that returns type
ConnectionStringDemo.ValuesHandler.

I just wonder why is it not possible to skip the namespace becuse I have
already declared it and just write
ValuesHandler val =
(ValuesHandler)ConfigurationManager.GetSection("MyFirstSectionGroup/MyFirstSection");
When I do so I get the following exception.
An unhandled exception of type 'System.InvalidCastException' occurred in
ConsoleApplication7.exe

My question is just why ?
I mean as long as I have declared the namespace in my case
ConnectionStringDemo I have automatically access to it.

using ConnectionStringDemo;

class Program
{
static void Main(string[] args)
{
ConnectionStringDemo.ValuesHandler val =
(ConnectionStringDemo.ValuesHandler)
ConfigurationManager.GetSection("MyFirstSectionGroup/MyFirstSection");
}
}

//Tony


From: Adam Clauss on
Tony Johansson wrote:
> Hi!
>
> As you can see I have defined namespace ConnectionStringDemo in the console
> app. From Main I call method GetSection that returns type
> ConnectionStringDemo.ValuesHandler.
>
> I just wonder why is it not possible to skip the namespace becuse I have
> already declared it and just write
> ValuesHandler val =
> (ValuesHandler)ConfigurationManager.GetSection("MyFirstSectionGroup/MyFirstSection");
> When I do so I get the following exception.
> An unhandled exception of type 'System.InvalidCastException' occurred in
> ConsoleApplication7.exe
>
> My question is just why ?
> I mean as long as I have declared the namespace in my case
> ConnectionStringDemo I have automatically access to it.
>
> using ConnectionStringDemo;
>
> class Program
> {
> static void Main(string[] args)
> {
> ConnectionStringDemo.ValuesHandler val =
> (ConnectionStringDemo.ValuesHandler)
> ConfigurationManager.GetSection("MyFirstSectionGroup/MyFirstSection");
> }
> }
>
> //Tony
>
>
You should be able to do that, something else is going on that you have
not showed us. Catch the exception, what does the exception actually say?

-Adam
From: Tony Johansson on
"Adam Clauss" <cabadam.do@.not.gmail.spam.com> skrev i meddelandet
news:uMPHOKYsKHA.6140(a)TK2MSFTNGP05.phx.gbl...
> Tony Johansson wrote:
>> Hi!
>>
>> As you can see I have defined namespace ConnectionStringDemo in the
>> console app. From Main I call method GetSection that returns type
>> ConnectionStringDemo.ValuesHandler.
>>
>> I just wonder why is it not possible to skip the namespace becuse I have
>> already declared it and just write
>> ValuesHandler val =
>> (ValuesHandler)ConfigurationManager.GetSection("MyFirstSectionGroup/MyFirstSection");
>> When I do so I get the following exception.
>> An unhandled exception of type 'System.InvalidCastException' occurred in
>> ConsoleApplication7.exe
>>
>> My question is just why ?
>> I mean as long as I have declared the namespace in my case
>> ConnectionStringDemo I have automatically access to it.
>>
>> using ConnectionStringDemo;
>>
>> class Program
>> {
>> static void Main(string[] args)
>> {
>> ConnectionStringDemo.ValuesHandler val =
>> (ConnectionStringDemo.ValuesHandler)
>>
>> ConfigurationManager.GetSection("MyFirstSectionGroup/MyFirstSection");
>> }
>> }
>>
>> //Tony
>>
> You should be able to do that, something else is going on that you have
> not showed us. Catch the exception, what does the exception actually say?
>
> -Adam

Transated from my native language to english it's something like.
It didn't work to convert an object of type
ConnectionStringDemo.ValuesHandle to type
ConfigTest.ValuesHandler.

//Tony.


From: Adam Clauss on
Tony Johansson wrote:
> Transated from my native language to english it's something like.
> It didn't work to convert an object of type
> ConnectionStringDemo.ValuesHandle to type
> ConfigTest.ValuesHandler.
>
> //Tony.
>
>
>
Ah, that's the missing piece. You actually have two different types
defined called ValuesHandler: ConnectionStringDemo.ValuesHandler and
ConfigTest.ValuesHandler and they are getting mixed up.

If you enter the code the way you wanted to do:

ValuesHandler val = (ValuesHandler)ConfigurationManager.GetSection("MyFirstSectionGroup/MyFirstSection");


You are getting back an object of type
ConnectionStringDemo.ValuesHandler, and then trying to cast it to type
ConfigTest.ValuesHandler. They are two separate types (sharing the same
name does not make them the same!).

I cannot tell you which of those types you need to get, but you need to
figure out what that call is SUPPOSED to return and sync up both sides
to refer to the same type. In general, I would avoid defining two types
with the same name in different namespaces, it will lead to confusion
like this (not to say it should NEVER be done, but you should have a
good reason for doing so).

-Adam
From: Peter Duniho on
Tony Johansson wrote:
> [...]
>> You should be able to do that, something else is going on that you have
>> not showed us. Catch the exception, what does the exception actually say?
>>
>> -Adam
>
> Transated from my native language to english it's something like.
> It didn't work to convert an object of type
> ConnectionStringDemo.ValuesHandle to type
> ConfigTest.ValuesHandler.

That message is telling you that you're trying to convert an object of
one type to a completely different type. Just because the name of the
type is "ValuesHandler", that doesn't mean it's the _same_
"ValuesHandler". And in this case, it's not. One "ValuesHandler" is
from the "ConnectionStringDemo" namespace while the other is from
"ConfigTest" (which could be a namespace or some other type�not possible
to know from your code example).

You either need to change your code so that GetSection() returns a
"ConnectionStringDemo.ValuesHandler" or your local variable so that it's
a "ConfigTest.ValuesHandler".

Pete