Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / October 2007

Tip: Looking for answers? Try searching our database.

To C#

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
RP - 24 Oct 2007 07:54 GMT
Please help converting following VB code to C#. My attempted C# code
is given below:

[VB Code]
If Session("Cart") Is Nothing Then
    Session.Add("Cart",New SortedList)
End If
Return CType(Session("Cart"),SortedList)
----------------------------------------------------------------
[Attempted C# Code]
If (Session("Cart") == "")
    Session.Add("Cart",new SortedList)

return SortedList Session("Cart");
Christof Nordiek - 24 Oct 2007 08:17 GMT
> Please help converting following VB code to C#. My attempted C# code
> is given below:
[quoted text clipped - 7 lines]
> [Attempted C# Code]
> If (Session("Cart") == "")

1. C# is case sensitive. If must be if.
2. Session("Cart") in your VB-Examble is not a fuction call, but an access
to a default property, wich is an indexer in C#. You have to call it with []
instead of () (like array element access).

(I assume Session here is the Session-object of ASP.NET. If it is a method
here, then calling with () is right.)
> Session.Add("Cart",new SortedList)

3. Constructors (as well as methods) have allways to be called with
parantheses, even if the parameterlist ist empty.

> return SortedList Session("Cart");

4. To Cast to another type, set the typename in parantheses before the
expression.

[Good C# Code]
if (Session["Cart"] == null)
   Session.Add("Cart", new SortedList);

return (SortedList)Session("Cart");

HTH
Christof
RP - 24 Oct 2007 08:31 GMT
Christof,

Your code needed some correction. I modified like this and it worked:
=======================================
if (Session["Cart"]==null)
       {
           Session.Add("Cart",new SortedList());
       }
return (SortedList) Session["Cart"];
=======================================
Thanks for your help.
..............................................................................................................
> [Good C# Code]
> if (Session["Cart"] == null)
>     Session.Add("Cart", new SortedList);
>
> return (SortedList)Session("Cart");
Michael S - 24 Oct 2007 12:36 GMT
My take:

{
SortedList list = (SortedList)Session["Cart"];
if (list == null)
{
list = new SortedList();
Session["Cart"] = list;
}
return list;
}

- Michael Starberg

> Christof,
>
[quoted text clipped - 13 lines]
>>
>> return (SortedList)Session("Cart");

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.