raf maester

raf maester

  • NA
  • 5
  • 1.6k

How to convert Shared sub (vb.net) to C#?

Apr 12 2021 1:50 PM
Hi
 
I'm new with C# and I want to convert this vb.net code to C# but i get two errors like "a namespace doesn't not contain members ..." on the line "Public Class check" and "expected classes ..." on the line "Public Shared"
 
Thanks for help.
 
Here the vb.net code:
  1. Imports Microsoft.VisualBasic  
  2. Public Class check  
  3. Public Shared Sub ok()  
  4. Dim ok As HttpCookie  
  5. ok = HttpContext.Current.Request.Cookies("ok")  
  6. Try  
  7. If Not ok.Value = "y" Then  
  8. End If  
  9. Catch ex As Exception  
  10. Dim jv As String  
  11. jv = "<script language='javascript'>" _  
  12. " alert('First log in.');" _  
  13. " window.location.href='start.aspx';" _  
  14. "</script>"  
  15. HttpContext.Current.Response.Write(jv)  
  16. End Try  
  17. End Sub  
  18. End Class  
And here my C# code:
  1. using System;  
  2. using System.Web.UI.WebControls;  
  3. Public Class check  
  4. {  
  5. public static void ok()  
  6. {  
  7. HttpCookie ok;  
  8. ok = HttpContext.Current.Request.Cookies("ok");  
  9. try  
  10. {  
  11. if (!(ok.Value == "y"))  
  12. {}  
  13. }  
  14. catch (Exception ex)  
  15. {  
  16. string jv;  
  17. jv = "<script language='javascript'>" + " alert('First log in.');" + " window.location.href='start.aspx';" + "</script>";  
  18. HttpContext.Current.Response.Write(jv);  
  19. }  
  20. }  
  21. }  

Answers (3)