qualifying expression will not be evaluated

B

Ben

Hi,

i would like to have an explanation about this:
This works, but i get the message in code-behind:
access of shared member,... or nested type through an instance; qualifying
expression will not be evaluated.

Dim cl As System.Drawing.Color
Label1.ForeColor = cl.FromName(DropDownList1.SelectedValue)

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack=true>
<asp:ListItem Text="red" Value="red"></asp:ListItem>

By the way, i know that there is another solution:
System.Drawing.Color.Fromname(DropDownList1.SelectedValue).

Thanks
Ben
 
R

rowe_newsgroups

Hi,

i would like to have an explanation about this:
This works, but i get the message in code-behind:
access of shared member,... or nested type through an instance; qualifying
expression will not be evaluated.

Dim cl As System.Drawing.Color
Label1.ForeColor = cl.FromName(DropDownList1.SelectedValue)

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack=true>
<asp:ListItem Text="red" Value="red"></asp:ListItem>

By the way, i know that there is another solution:
System.Drawing.Color.Fromname(DropDownList1.SelectedValue).

Thanks
Ben

By the way, i know that there is another solution:
System.Drawing.Color.Fromname(DropDownList1.SelectedValue).

Won't using that take away the error?

Thanks,

Seth Rowe
 
B

Ben

I would like an explanation about the fact i get that error and nevertheless
it works. What does that error mean?
i mentioned that there is a solution without error in order to avoid that i
get that solution as answer on my post.
 
H

Herfried K. Wagner [MVP]

Ben said:
I would like an explanation about the fact i get that error and
nevertheless it works. What does that error mean?
i mentioned that there is a solution without error in order to avoid that
i get that solution as answer on my post.

'FromName' is a shared member of 'Color'. Thus it's not necessary to call
it on an instance. It can be called on the type directly ('... =
Color.FromName(...)'). The compile-time error does not make much sense in
your simple scenario, but it would if you replace the part on the left hand
side of the '.' by a function call, for example ('... =
GetColor().FromName(...)'). In this case, 'GetColor' won't get called
because the compiler will only emit a call to 'Color.FromName'.
 
R

rowe_newsgroups

I would like an explanation about the fact i get that error and nevertheless
it works. What does that error mean?
i mentioned that there is a solution without error in order to avoid that i
get that solution as answer on my post.

It's fairly simple - In your code cl is an instance of the color
class. FromName is a shared method of the Color class which means that
cl also has the method. Since shared methods can't be used with
instance the compiler will just ignore the instance (cl) and go
directly to the class (System.Drawing.Color). This means the "will not
be evaluated" error is talking about the instance and not the method -
that's why the expression still works. As for why the warning is even
generated I'm not sure - perhaps to prevent someone from making a
pointless variable when they should just call the method directly (you
know, like you did in your code :) )

Thanks,

Seth Rowe
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top