Hi everyone,
My client is requiring me to convert an existing web app from VB.Net to
C# to become more inline with their existing codebase.
I am running into a problem when converting a usercontrol, basically the
usercontrol has a property which is set by the hosting page, to indicate
if a user is logged in or not. This controls the navigation menu which
the user see's.
In the VB version its just a matter of:
Private _UserIsLoggedIn As Boolean
Public Property UserIsLoggedIn() As Boolean
Get
Return _UserIsLoggedIn
End Get
Set(ByVal value As Boolean)
_UserIsLoggedIn = value
End Set
End Property
So I convert it to C# and this gives me:
private bool _UserIsLoggedIn;
public bool UserIsLoggedIn
{
get { return _UserIsLoggedIn; }
set { _UserIsLoggedIn = value; }
}
However, I cannot access this property from the calling page.
Can anyone tell me where I am going wrong.
Regards
Hakan Fatih YILDIRIM - 24 Aug 2007 12:05 GMT
> Hi everyone,
>
[quoted text clipped - 33 lines]
>
> Regards
----------------------------------------------------------------------------------------------------------------------------------------------------------
if you want to access this property after creating the instance of the
class.there must be nothing wrong.But if you want to access it wlthout
creating instance ,you must use 'static'.But convertion to c# and the
piece of code is true, you will access the property after creating the
instance..if you send more part of code ,we can help you more..
Best Regards
Hakan Fatih YILDIRIM
MCP
Patrice - 24 Aug 2007 12:07 GMT
Do you cast the user control variable to the proper type ? Remember that C#
is always in "option strict on" mode (and you may want to do the same in
your VB projects).
---
Patrice
> Hi everyone,
>
[quoted text clipped - 33 lines]
>
> Regards
Mick Walker - 24 Aug 2007 12:16 GMT
> Do you cast the user control variable to the proper type ? Remember that C#
> is always in "option strict on" mode (and you may want to do the same in
> your VB projects).
>
> ---
> Patrice
Im not sure of how to do that.
Here is the code for my user control if that helps:
using System;
public partial class Controls_uc_MainMenu : System.Web.UI.UserControl
{
private static bool _UserIsLoggedIn;
private static bool _UserIsModerator;
private static bool _UserIsAdministrator;
public string Menu;
public static bool UserIsLoggedIn
{
get
{
return _UserIsLoggedIn;
}
set
{
_UserIsLoggedIn = value;
}
}
public static bool UserIsModerator
{
get
{
return _UserIsModerator;
}
set
{
_UserIsModerator = value;
}
}
public static bool UserIsAdministrator
{
get
{
return _UserIsAdministrator;
}
set
{
_UserIsAdministrator = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if ((UserIsLoggedIn == true))
{
Menu += "<h2>Menu</h2><ul>";
Menu += "<li><a title=\"Logout\" href
=\"/Logout.aspx\">Logout</a></li>";
Menu += "<li><a title=\"Search For Members\"
href=\"/Search.aspx\">Search</a></li>";
Menu += "<li><a title=\"Open Your HotList\"
href=\"/HotList.aspx\">HotList</a></li>";
Menu += "<li><a title=\"Messages\"
href=\"/Messages.aspx\">Messages</a></li>";
Menu += "<li><a title=\"Edit Your Profile\"
href=\"/MyProfile.aspx\">My Profile</a></li>";
Menu += "<li><a title=\"Manage My Photos\"
href=\"/Photos.aspx\">Manage Photos</a></li>";
Menu += "<li><a title=\"Modify Your Account Settings\"
href=\"/Account.aspx\">My Account</a></li>";
if ((UserIsModerator == true))
{
Menu += "<h2>Moderation Options</h2>";
Menu += "<ul>";
Menu +="<li><a title=\"Moderate Profiles\"
href=\"/Moderator/Profiles.aspx\">Moderate Profiles</a></li>";
Menu += "<li><a title=\"Moderate Photos\"
href=\"/Moderator/Photos.aspx\">Moderate Photos</a></li>";
Menu += "</ul>";
if ((UserIsAdministrator == true))
{
// Set administration options
}
}
Menu += "</ul>";
}
else
{
Menu += "<h2>Featured Members</h2><ul>";
Menu += "<li class=\"UserList\"><img src=\"mick.jpg\"
width=\"60px\" height=\"60px\"><br>Some Users Profile</li>";
Menu += "<li class=\"UserList\"><img src=\"mick.jpg\"
width=\"60px\" height=\"60px\"><br>Some Users Profile</li>";
Menu += "<li class=\"UserList\"><img src=\"mick.jpg\"
width=\"60px\" height=\"60px\"><br>Some Users Profile</li>";
Menu += "<li class=\"UserList\"><img src=\"mick.jpg\"
width=\"60px\" height=\"60px\"><br>Some Users Profile</li>";
Menu += "<li class=\"UserList\"><img src=\"mick.jpg\"
width=\"60px\" height=\"60px\"><br>Some Users Profile</li>";
Menu += "<li class=\"UserList\"><img src=\"mick.jpg\"
width=\"60px\" height=\"60px\"><br>Some Users Profile</li>";
Menu += "<li class=\"UserList\"><img src=\"mick.jpg\"
width=\"60px\" height=\"60px\"><br>Some Users Profile</li>";
Menu += "</ul>";
}
DataBind();
}
}
All I wish to accomplish is to be able to set various parameters from
the hosting page. such as MainMenu1.UserIsModerator = true; (as is
currently done on the visualbasic application)
Patrice - 24 Aug 2007 12:54 GMT
For now my guess would be that the problem is not in the user control but in
how you call this control property from the hosting page. How do you use
this property in your main code ?
What is the type of MainMenu1 ? If it's not Controls_uc_MainMenu try :
(Controls_uc_MainMenu)MainMenu1.UserIsLoggedIn=true;
Also C# is case sensitive of course.
If not yet solved please mention the exact error message you have (for now
my understanding is that you have a compile time message saying that
UserIsLoggedIn is not a member of the MainMenu1 variable ??).
Good luck.
---
Patrice
>> Do you cast the user control variable to the proper type ? Remember that
>> C# is always in "option strict on" mode (and you may want to do the same
[quoted text clipped - 116 lines]
> hosting page. such as MainMenu1.UserIsModerator = true; (as is currently
> done on the visualbasic application)
Peter Duniho - 24 Aug 2007 18:25 GMT
> [...]
> Here is the code for my user control if that helps:
>
> [...]
> public static bool UserIsLoggedIn
Um. Your original example showed an instance property, but the above
shows a static property.
First, you should know that a static property is shared among all
instances of the same class. Second, you should know that to access a
static property, you need to specify the class name, not an instance
name. That is:
Controls_uc_MainMenu.UserIsLoggedIn;
Rather than:
MainMenu1.UserIsLoggedIn;
Do you really intend for the properties to be static?
Pete
Dan Kelley - 24 Aug 2007 14:28 GMT
> Hi everyone,
>
[quoted text clipped - 33 lines]
>
> Regards
The answer is that you have changed the visibility of your property. In the
VB code you have defined it as Public, whereas you have used the private
keyword in the C# code. Change private to public and it will be visible.
HTH
Dan
David Anton - 24 Aug 2007 15:40 GMT
That's not the case here - in both the VB and C# versions, the field is
private and the property is public.

Signature
David Anton
http://www.tangiblesoftwaresolutions.com
Convert between VB, C#, C++, and Java
Instant C#
Instant VB
Instant C++
C++ to C# Converter
C++ to VB Converter
C++ to Java Converter
> > Hi everyone,
> >
[quoted text clipped - 40 lines]
> HTH
> Dan
rowe_newsgroups - 27 Aug 2007 13:44 GMT
On Aug 24, 9:28 am, Dan Kelley <DanKel...@discussions.microsoft.com>
wrote:
> > Hi everyone,
>
[quoted text clipped - 40 lines]
> HTH
> Dan
You didn't read the code or the previous posts very well did you? The
OP defined the variable to be returned as private and the property as
public so everything is fine. Second, David Anton has already
corrected another poster who said the same thing you just did...
Thanks,
Seth Rowe
rowe_newsgroups - 24 Aug 2007 14:58 GMT
> Hi everyone,
>
[quoted text clipped - 33 lines]
>
> Regards
I'd hate to state the obvious - but have you rebuilt the entire
project yet? Also, close all the tabs in the IDE and then reopen them.
C# and Asp.Net sometimes have difficulties keeping Intellisense up-to-
date. For C# you often must rebuild the project to get some changes to
show up, and with Asp.Net Intellisense sometimes won't refresh until
the code behind file has been reloaded.
Thanks,
Seth Rowe