''does not contain a definition for 'Session' and no extension method '' accepting a first argument of type 'object' could be found

StackOverflow https://stackoverflow.com/questions/21743648

  •  10-10-2022
  •  | 
  •  

Question

i am working on VB.net to C# project conversion. i am getting errors, any solution?

1--'object' does not contain a definition for 'Session' and no extension method 'Session' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?) 2--'object' does not contain a definition for 'Request' and no extension method 'Session' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

I am using online free conversion tool; my converted code is below.

public bool DisplayMessageWindow(ref object Caller, string MessageToDisplay)
{
    bool functionReturnValue = false;
    long P = 0;

    functionReturnValue = false;

    if (IsCalledByMessageURL(ref Caller)) {
        return functionReturnValue;
    }

    LogMessage("Displaying Message Window (Called From): " + Caller.Request.RequestUrl.ToString());


    LogMessage("                    Message To Display : " + MessageToDisplay, false);

    Caller.Session["MessageToDisplay"] = MessageToDisplay;

    Caller.Session["RedirectURL"] = Caller.Request.Url.ToString();
    PriorURL = Caller.Session["RedirectURL"];
    SetRedirectURL(ref Caller);

    LogMessage("Modified Redirect URL to : " + Caller.Session["RedirectURL"]);

    if ((Caller.Request.Form("partssn") != null)) {
        Caller.Session["SRTSessionId"] = Caller.Request.Form("SRTSessionId");
        Caller.Session["partssn"] = Caller.Request.Form("partssn");
        Caller.Session["erid"] = Caller.Request.Form("erid");
    } else {
        Caller.Session["SessionID"] = Session.SessionID.ToString();
        Caller.Session["partssn"] = Caller.Request.Form("txtUserName");
        //Caller.Session["partssn"] = Session["loginUser"]
    }

    LogMessage("Redirect To: " + Caller.Session["RedirectURL"]);
    functionReturnValue = true;
    return functionReturnValue;
    //Caller.Response.Redirect("LoadPartData.aspx")
}

my vb.net code is

Function DisplayMessageWindow(ByRef Caller As Object, ByVal MessageToDisplay As String) As Boolean
    Dim P As Long

    DisplayMessageWindow = False

    If IsCalledByMessageURL(Caller) Then
        Exit Function
    End If

    LogMessage("Displaying Message Window (Called From): " + Caller.Request.Url.ToString)
    LogMessage("                    Message To Display : " + MessageToDisplay)

    Caller.Session("MessageToDisplay") = MessageToDisplay

    Caller.Session("RedirectURL") = Caller.Request.Url.ToString
    PriorURL = Caller.Session("RedirectURL")
    SetRedirectURL(Caller)

    LogMessage("Modified Redirect URL to : " + Caller.Session("RedirectURL"))

    If Not (Caller.Request.Form("partssn") Is Nothing) Then
        Caller.Session("SRTSessionId") = Caller.Request.Form("SRTSessionId")
        Caller.Session("partssn") = Caller.Request.Form("partssn")
        Caller.Session("erid") = Caller.Request.Form("erid")
    Else
        Caller.Session("SessionID") = Session.SessionID.ToString()
        Caller.Session("partssn") = Caller.Request.Form("txtUserName")
        'Caller.Session("partssn") = Session("loginUser")
    End If

    LogMessage("Redirect To: " + Caller.Session("RedirectURL"))
    DisplayMessageWindow = True
    'Caller.Response.Redirect("LoadPartData.aspx")
End Function
Was it helpful?

Solution

@jmcilhinney is right on both counts: Option Strict is a good idea and there's no reason for ByRef parameter passing. However, moving to Option Strict is likely to light up your compiler like a Christmas tree, given the general state of this code and the fact that the original author(s) seem to like gratuitous late binding.

So, if you're just trying to make it through this port, you could try declaring Caller as dynamic rather than object. That would be an accurate translation of how the parameters are passed in VB.

OTHER TIPS

That VB code is relying on late-binding if the Caller parameter is type Object in the VB code too, which makes it bad VB code. You should have Option Strict On in VB so that code should fail to compile there too. If that argument is going to be a specific type then declare it as that type, in the VB code as well as the C# code.

Also, why is it being passed by reference? Where are you assigning anything to that parameter inside the method? Nowhere that I can see, so there's no reason to pass it by reference, i.e. pass it by value. Again, that applies to VB as well as C#.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top