explicit cast

T

Tony Johansson

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
 
A

Adam Clauss

Tony said:
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
 
T

Tony Johansson

Adam Clauss said:
Tony said:
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.
 
A

Adam Clauss

Tony said:
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
 
P

Peter Duniho

Tony said:
[...]
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top