Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How to use a partial class?

How to use a partial class?

Scheduled Pinned Locked Moved ASP.NET
helptutorialquestion
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    N Surendra Prasad
    wrote on last edited by
    #1

    Hi, I am trying to split a class(Class1) to different files, hence i have changed the access specifier for the Class1 to Partial. I have created one more class with the same name(Class1) and same namespace. From the second class file which i created, i am able to access the procedures / functions from the first class file, but i am not able to access the variables which are used there. Can anyone help me to access the variables from the first class file. Regards N.Surendra Prasad

    C 1 Reply Last reply
    0
    • N N Surendra Prasad

      Hi, I am trying to split a class(Class1) to different files, hence i have changed the access specifier for the Class1 to Partial. I have created one more class with the same name(Class1) and same namespace. From the second class file which i created, i am able to access the procedures / functions from the first class file, but i am not able to access the variables which are used there. Can anyone help me to access the variables from the first class file. Regards N.Surendra Prasad

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      That's just not possible. If you have a partial class, properly declared, then the variables and methods are all together in the one class. Having said that, when I do a partial class, I tend to make it so that variables are used in methods in the same file. That just makes sense of the whole concept

      Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

      N 1 Reply Last reply
      0
      • C Christian Graus

        That's just not possible. If you have a partial class, properly declared, then the variables and methods are all together in the one class. Having said that, when I do a partial class, I tend to make it so that variables are used in methods in the same file. That just makes sense of the whole concept

        Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

        N Offline
        N Offline
        N Surendra Prasad
        wrote on last edited by
        #3

        I have the variables and methods inside one class. I want to break that class into two classes so that the size of the class is reduced. That is the reason why i went for partial class concept. I have few variables which will be used throughout the class, so they have been declared globally in that class. When i tried moving few methods to the other class file, i am not able to access those global variables which are there in the first class file. Can you help me on this? Regards N.Surendra Prasad

        C N 2 Replies Last reply
        0
        • N N Surendra Prasad

          I have the variables and methods inside one class. I want to break that class into two classes so that the size of the class is reduced. That is the reason why i went for partial class concept. I have few variables which will be used throughout the class, so they have been declared globally in that class. When i tried moving few methods to the other class file, i am not able to access those global variables which are there in the first class file. Can you help me on this? Regards N.Surendra Prasad

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          N.Surendra Prasad wrote:

          so they have been declared globally in that class.

          I don't know what this means, how is something declared globally, but within a class ? Do you mean a member variable ?

          N.Surendra Prasad wrote:

          Can you help me on this?

          You need to post some code, so we can see what's going on.

          Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

          N 1 Reply Last reply
          0
          • C Christian Graus

            N.Surendra Prasad wrote:

            so they have been declared globally in that class.

            I don't know what this means, how is something declared globally, but within a class ? Do you mean a member variable ?

            N.Surendra Prasad wrote:

            Can you help me on this?

            You need to post some code, so we can see what's going on.

            Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

            N Offline
            N Offline
            N Surendra Prasad
            wrote on last edited by
            #5

            Hi Christian, Here is the code which i have used. File1: Partial Class TEWebClass Inherits UI.Page Protected cnn As New ADODB.Connection Protected rs As ADODB.Recordset Protected rs2 As ADODB.Recordse End Class File2: Partial Class TEWebClass Inherits UI.Page Public Sub clientadd_ProcessForm() Dim formError As Boolean Dim itemOK As Boolean Dim errortext As String ... End Sub End Class These are two different files with the same class name. From File2, i need to access the variable cnn which is declared in File1. I tried, but not able to do it. Can you help me. N.Surendra Prasad Winning doesn't always mean being first, winning means you're doing better than you've done before.

            N J 2 Replies Last reply
            0
            • N N Surendra Prasad

              I have the variables and methods inside one class. I want to break that class into two classes so that the size of the class is reduced. That is the reason why i went for partial class concept. I have few variables which will be used throughout the class, so they have been declared globally in that class. When i tried moving few methods to the other class file, i am not able to access those global variables which are there in the first class file. Can you help me on this? Regards N.Surendra Prasad

              N Offline
              N Offline
              N a v a n e e t h
              wrote on last edited by
              #6

              N.Surendra Prasad wrote:

              I have the variables and methods inside one class. I want to break that class into two classes so that the size of the class is reduced.

              I presume you need to keep variables in one file and methods in other, right ? So your partial class would be like file name would be MyClass.Variables

              partial class MyClass
              {
              public int a;
              private string b;
              //etc
              }

              file name would be MyClass.Methods

              partial class MyClass
              {
              //Methods goes here
              }

              when compiled, this will act as a single class. You can access variables as you do in normal class.

              All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions

              1 Reply Last reply
              0
              • N N Surendra Prasad

                Hi Christian, Here is the code which i have used. File1: Partial Class TEWebClass Inherits UI.Page Protected cnn As New ADODB.Connection Protected rs As ADODB.Recordset Protected rs2 As ADODB.Recordse End Class File2: Partial Class TEWebClass Inherits UI.Page Public Sub clientadd_ProcessForm() Dim formError As Boolean Dim itemOK As Boolean Dim errortext As String ... End Sub End Class These are two different files with the same class name. From File2, i need to access the variable cnn which is declared in File1. I tried, but not able to do it. Can you help me. N.Surendra Prasad Winning doesn't always mean being first, winning means you're doing better than you've done before.

                N Offline
                N Offline
                N a v a n e e t h
                wrote on last edited by
                #7

                Your code looks OK. Are you getting any error ?

                All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions

                N 1 Reply Last reply
                0
                • N N a v a n e e t h

                  Your code looks OK. Are you getting any error ?

                  All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions

                  N Offline
                  N Offline
                  N Surendra Prasad
                  wrote on last edited by
                  #8

                  Hi Navaneeth, I tried using the cnn variable from File2, it throws the error "variable cnn not declared" N.Surendra Prasad Winning doesn't always mean being first, winning means you're doing better than you've done before.

                  N 1 Reply Last reply
                  0
                  • N N Surendra Prasad

                    Hi Navaneeth, I tried using the cnn variable from File2, it throws the error "variable cnn not declared" N.Surendra Prasad Winning doesn't always mean being first, winning means you're doing better than you've done before.

                    N Offline
                    N Offline
                    N a v a n e e t h
                    wrote on last edited by
                    #9

                    If you have any namespace defined, make sure it is same for both files.

                    All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions

                    1 Reply Last reply
                    0
                    • N N Surendra Prasad

                      Hi Christian, Here is the code which i have used. File1: Partial Class TEWebClass Inherits UI.Page Protected cnn As New ADODB.Connection Protected rs As ADODB.Recordset Protected rs2 As ADODB.Recordse End Class File2: Partial Class TEWebClass Inherits UI.Page Public Sub clientadd_ProcessForm() Dim formError As Boolean Dim itemOK As Boolean Dim errortext As String ... End Sub End Class These are two different files with the same class name. From File2, i need to access the variable cnn which is declared in File1. I tried, but not able to do it. Can you help me. N.Surendra Prasad Winning doesn't always mean being first, winning means you're doing better than you've done before.

                      J Offline
                      J Offline
                      J4amieC
                      wrote on last edited by
                      #10

                      N.Surendra Prasad wrote:

                      Protected cnn As New ADODB.Connection Protected rs As ADODB.Recordset Protected rs2 As ADODB.Recordse

                      Why? Just why?

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups