locked
Reference to a class inside another class RRS feed

  • Question

  • User661479005 posted

    When I am working in a webform, I can add custom namespaces and classe like that:

    <%@ Import Namespace="Elementrelation" %>
    <%@ Assembly src="/classes/relation.vb" %>

    I collect most of my custom classes in the folder /classes and only few in the app_code folder, because I can edit the custom classes without that my clients loose their session when they are online.

    My problem comes when I have to use more classes in a class like for example:

    Namespace A
       
     Public Class Aclass
    	    Inherits myfunctionlibrary
    
                Dim objB as new Bclass
     End class
    End namespace
    
    
    

    How do I add a reference to Bclass if the code file is in the folder /classes?

    Tuesday, March 17, 2015 6:32 AM

Answers

  • User1644755831 posted

    Hi Danish Woman,

    I created a demo sample for you. Sample Project. Download zip file named 2040781.zip.  As you will see in the demo I have put both classes under the folder called model and in Aclass.cs file  I have Imported the Namespace B.

    Hope this helps.

    With Regards,

    Krunal Parekh

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 18, 2015 4:43 AM

All replies

  • User1644755831 posted


    Hello Danish woman.

    From what I understand for this issue I think you need to Import the namespace in which Bclass resides.

    For example.

    In Namespace A class Aclass reside

    Namespace A
       
     Public Class Aclass
    	    Inherits myfunctionlibrary
    
                Dim objB as new Bclass
     End class
    End namespace

    and in Namespace B class named Bclass reside.

    Namespace B
       
     Public Class Bclass
    	    Inherits myfunctionlibrary
    
     End class
    End namespace

    Now to use Bclass in Namespace A you need to Import Namespace B in Namespace A like this.

    Imports B
    
    Namespace A
       
     Public Class Aclass
    	    Inherits myfunctionlibrary
    
                Dim objB as new Bclass
     End class

    Please Have a look the Import Statement in VB.NET.

    Hope this helps.

    With Regards,

    Krunal Parekh

    Tuesday, March 17, 2015 10:30 PM
  • User661479005 posted

    Thank you for answering

    How can I import the namespace, if it is not in the app_code folder? How do I write the path to the Namespace B in the Aclass?
    Like I wrote, I want to avoid to many classes in the app_code folder.

    Wednesday, March 18, 2015 4:34 AM
  • User1644755831 posted

    Hi Danish Woman,

    I created a demo sample for you. Sample Project. Download zip file named 2040781.zip.  As you will see in the demo I have put both classes under the folder called model and in Aclass.cs file  I have Imported the Namespace B.

    Hope this helps.

    With Regards,

    Krunal Parekh

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 18, 2015 4:43 AM
  • User661479005 posted

    Hi Krunal

    Thank you very much for your Sample Project.

    I am not using Visual Studio for my project, but I have Visual Studio to open your project.
    I can see that you imported namespace B as _2040781.B  - the project name + . + B

    I can also see that Visual Studio while building the project creates 2040781.dll in the bin-folder.
    Updating the bin folder would still make my clients loose their session while updating the class.

    If I do not use Visual studio, is there still a way to import B to the Aclass and what can I use instead of the project name?

    Wednesday, March 18, 2015 8:32 AM
  • User1644755831 posted

    Hello Danish Woman,

    What IDE you are using to compile and publish your website? For issue about maintaining session you need to change your mode of how IIS stores the Session.

    ASP.NET supports three modes of  session state:

    • InProc: In-Proc mode stores values in the memory of the ASP.NET  worker process. Thus, this mode offers  the fastest access to these values.  However, when the ASP.NET worker  process recycles, the state data is  lost.
    • StateServer: Alternately, StateServer mode uses a stand-alone  Microsoft Windows service to store  session variables. Because this  service is independent of Microsoft  Internet Information Server (IIS), it  can run on a separate server. You can  use this mode for a load-balancing  solution because multiple Web servers  can share session variables. Although  session variables are not lost if you  restart IIS, performance is impacted  when you cross process boundaries.
    • SqlServer: If you are greatly concerned about the persistence of  session information, you can use  SqlServer mode to leverage Microsoft  SQL Server to ensure the highest level  of reliability. SqlServer mode is  similar to out-of-process mode, except  that the session data is maintained in  a SQL Server. SqlServer mode also  enables you to utilize a state store  that is located out of the IIS process  and that can be located on the local  computer or a remote server.

    To achieve what you want you might want to switch your session mode from In Proc to stateServer or SqlServer.

    Here is information about Session-State Modes.

    Hope this helps.

    With Regards,

    Krunal Parekh

    Wednesday, March 18, 2015 9:33 PM
  • User661479005 posted

    Hi Krunal

    Thank you for your reply

    The project uses InProc, that means that the sessions are lost when we update bin folder and app_code folder. We have already evaluated if we should use Stateserver or InProc, and for now InProc is the choice for us.

    We often need to make small updates of certain files, and mostly we can do it during the daytime, but when we have to update files in bin folder or in app_code we have to plan to do it during early morning hours. Therefor it is important for us that most files can be outside app_code folder and bin folder.

    Thursday, March 19, 2015 2:49 AM
  • User1644755831 posted

    Hello Danish,

    What you want to achieve would be possible if you are making changes to an Aspx page or JavaScript file. Their effects would be seen on the page as soon as you make the changes in the files. but since you are changing a code behind file here. It will need to recompiled and your website will need to be published again after recompiling the code. From my point of view it should not matter if your class is in app_code or other folder the complied dll of code behind class will always go to bin folder(or the directory specified in the project properties).

    Hope this helps.

    With Regards,

    Krunal Parekh

    Thursday, March 19, 2015 10:30 PM
  • User661479005 posted

    Hi Krunal

    Thanks for your good answers.

    One of the reason that I do not use Visual Studio is that I in that way do not have to rebuild the whole solution when I make changes to code behind. As long as the classes are outside the app_code and the bin folder my clients do not loose their sessions. I have a folder named classes and all the code behind I can change and upload directly. There is not created any dll file from it.

    But I understand that in the case that I want to import a A class into another class B, the class A has to be global.

    Friday, March 20, 2015 1:01 AM