locked
VB.Net Code Equivalent RRS feed

  • Question

  • User1122355199 posted

     Hello Everyone and thanks for your help in advance.  I am trying to convert a code block into VB.Net from C# but can't get it right.  Part of the problem is the use of Static methods which I can't seem to get the equivalent.  Here is the code:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Collections.Specialized;
    using System.Web;

    namespace RewriteModule
    {

        public class RewriteContext
        {
            // returns actual RewriteContext instance for
            // current request
            public static RewriteContext Current
            {
                get
                {
                    // Look for RewriteContext instance in
                    // current HttpContext. If there is no RewriteContextInfo
                    // item then this means that rewrite module is turned off
                    if(HttpContext.Current.Items.Contains("RewriteContextInfo"))
                        return (RewriteContext)HttpContext.Current.Items["RewriteContextInfo"];
                    else
                        return new RewriteContext();
                }
            }

            public RewriteContext()
            {
                _Params = new NameValueCollection();
                _InitialUrl = String.Empty;
            }

            public RewriteContext(NameValueCollection param, string url)
            {
                _InitialUrl = url;
                _Params = new NameValueCollection(param);
               
            }

            private NameValueCollection _Params;

            public NameValueCollection Params
            {
                get { return _Params; }
                set { _Params = value; }
            }

            private string _InitialUrl;

            public string InitialUrl
            {
                get { return _InitialUrl; }
                set { _InitialUrl = value; }
            }
        }
    }

     

    Any help is greatly appreciated.

     

    Friday, October 8, 2010 8:47 PM

Answers

  • User-609339402 posted

    Sorry, i have remove my code from here because i can't read your question carefully ( i understand that convert from vb.net to C#) so that i can put here c# code which is your question.


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, October 9, 2010 1:16 AM

All replies

  • User1937213714 posted

    You can use the following url.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/

    Ur converted code will look like this.

    Imports System.Collections.Generic
    Imports System.Text
    Imports System.Collections.Specialized
    Imports System.Web
    
    Namespace RewriteModule
    
    	Public Class RewriteContext
    		' returns actual RewriteContext instance for
    		' current request
    		Public Shared ReadOnly Property Current() As RewriteContext
    			Get
    				' Look for RewriteContext instance in
    				' current HttpContext. If there is no RewriteContextInfo
    				' item then this means that rewrite module is turned off
    				If HttpContext.Current.Items.Contains("RewriteContextInfo") Then
    					Return DirectCast(HttpContext.Current.Items("RewriteContextInfo"), RewriteContext)
    				Else
    					Return New RewriteContext()
    				End If
    			End Get
    		End Property
    
    		Public Sub New()
    			_Params = New NameValueCollection()
    			_InitialUrl = [String].Empty
    		End Sub
    
    		Public Sub New(param As NameValueCollection, url As String)
    			_InitialUrl = url
    
    			_Params = New NameValueCollection(param)
    		End Sub
    
    		Private _Params As NameValueCollection
    
    		Public Property Params() As NameValueCollection
    			Get
    				Return _Params
    			End Get
    			Set
    				_Params = value
    			End Set
    		End Property
    
    		Private _InitialUrl As String
    
    		Public Property InitialUrl() As String
    			Get
    				Return _InitialUrl
    			End Get
    			Set
    				_InitialUrl = value
    			End Set
    		End Property
    	End Class
    End Namespace
    


    Saturday, October 9, 2010 1:13 AM
  • User-609339402 posted

    Sorry, i have remove my code from here because i can't read your question carefully ( i understand that convert from vb.net to C#) so that i can put here c# code which is your question.


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, October 9, 2010 1:16 AM
  • User1122355199 posted

    Thanks for the response.  Really helpful tool.  So basically, a static method is the same a a read-only property in VB.Net? 

    Sunday, October 10, 2010 6:55 PM
  • User1122355199 posted

    BTW, I tried converting some other code:

    string sign = (path.IndexOf('?') == -1) ? "?" : "&";
    path = path + sign + HttpContext.Current.Request.QueryString.ToString();

    but the utility returns:

    Dim sign As String = If((path.IndexOf("?"C) = -1), "?", "&")
    path = path & sign & HttpContext.Current.Request.QueryString.ToString()
    

    Obviously not correct since it is not a conditional statement in C#, however, I'm not sure what the correct equivalent is.

    Thanks again.

     

     

    Sunday, October 10, 2010 8:30 PM
  • User397347636 posted

    That is absolutely correct.

    Since VB9 (VS 2008), VB has a true ternary conditional operator "If" which takes 3 arguments and is exactly equivalent to the C# ternary conditional operator (?:).

     

    Sunday, October 10, 2010 8:46 PM
  • User1122355199 posted

    Thanks for the response.  I am actually using VS 2005, so it is giving me an error.  Unfortunately, I don't understand what the C# equivalent is doing. 

    Sunday, October 10, 2010 9:17 PM
  • User397347636 posted

    For VS 2005, just use the "IIf" function instead - it's not ideal since it has to evaluate all the arguments (since it's just a function call instead of an operator), but it works.

    Alternatively, you can use:

    Dim sign As String
    If path.IndexOf("?"C) = -1)
        sign = "?"
    Else
        sign = "&"
    End If
    path = path & sign & HttpContext.Current.Request.QueryString.ToString()
    


     

    Sunday, October 10, 2010 9:22 PM
  • User1122355199 posted

    Again, thanks for the response.  Its becoming clearer, however, I don't know what the C is in If path.IndexOf("?"C) = -1. 

    Monday, October 11, 2010 5:59 AM