none
How to create a partial-class .vb file RRS feed

  • Question

  • Am trying to create a partial-class file set without success.
    I have these three files:
    ==========
    *** frmBatRen.Designer.vb
    <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>
    Partial Class frmBatRen
    #Region "Windows Form Designer generated code "
    ...
    #End Region
    End Class

    *** frmBatRen.vb
    Imports VB = Microsoft.VisualBasic
    Friend Class frmBatRen
    Inherits System.Windows.Forms.Form
    ...
    End Class

    (So far, so good. But then I added)

    *** AudioImageCode.vb
    Imports VB = Microsoft.VisualBasic
    Friend Class frmBatRen
     Dim Teststring As String = "ABC"
    End Class
    ==========

    In a nutshell, no objects like Teststring in the third file can be referenced by code in the second file, although the book seems to say it's all one class split into multiple files. The answer is probably a simple one but it eludes me.

    Both of the manuals I read say that only one Class statement needs to say Partial; in this case it's in the auto-generated  frmBatRen.Designer.vb file, which is typical. The file frmBatRen.vb lacks a Partial and has always worked well. I've tried a variety of alternatives, including the addition of Partial to all three files, making all the class statements Public, etc. No dice.
    I suspect the problem is due to the way I created the third file with
     File | New File | Visual Basic Class   - then changed the generated Class1 name.
    That is to say, the project file may be unaware of the linkage. But I don't see an alternative way to create it.
    Thursday, January 4, 2018 5:16 PM

Answers

  • Thanks! I just retrieved the zip file and I'll take a look at it.

    How did you create the second .vb file in the project?

    With File | New file | Text file, or some other way?

    Each class I created was by right clicking on the project, select add -> class

    Gave the class name say Demo1. Then opened the class and change from Public class Demo1 to 

    Partial Public Class Form1

    That's it.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by ronks Friday, January 5, 2018 6:08 PM
    Friday, January 5, 2018 5:39 PM
    Moderator
  • Bingo! It cross-compiles now with the references resolved. Thanks!

    I used a slightly different approach based on yours. Right-click on Project, then

    Add | New Item | Code file   - to avoid creating another class (probably harmless, but...)

    At first I got a warning about a conflict in the namespace, but when I added the Partial keyword explicitly it cleared.

    • Marked as answer by ronks Friday, January 5, 2018 6:30 PM
    Friday, January 5, 2018 6:08 PM

All replies

  • A partial class is always compiled into one single class:

    Public Class MyNewClass
        ' Code here...
    End Class
    
    
    Partial Public Class MyNewClass
        ' Maybe another method or two here...
    End Class


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Thursday, January 4, 2018 5:19 PM
  • Thanks for the reply; but I'm unclear on how that differs from my example of the three files.

    Or to put it another way, what I should be doing differently.

    Thursday, January 4, 2018 5:50 PM
  • Thanks for the reply; but I'm unclear on how that differs from my example of the three files.

    Or to put it another way, what I should be doing differently.

    I'm not sure either but it looks like you're trying to re-define it.

    A partial class is, essentially, telling the compiler that what follows is code to continue the definition of the class:

    https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/modifiers/partial


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Thursday, January 4, 2018 5:56 PM
  • As Frank has pointed out use his syntax.  What you have shown uses Friend Class, use Partial Class in your 3rd file.

    Lloyd Sheen

    Thursday, January 4, 2018 5:57 PM
  • Hi Ronks,

    I think the key thing which you need explained is that a rule of Partial classes is that they must all have the same access level.  Since the initial Form class is declared Public, any partial classes you want to add must also be declared public.  That's what Frank and Lloyd are saying.

    What surprises me is that the language analyzer didn't catch that and give you an error with the specific description; you must be using an old version of VS.  You should have gotten:

    Severity Code Description Project File Line Suppression State
    Error BC30925 Specified access 'Friend' for 'Form1' does not match the access 'Public' specified on one of its other partial types.

    The other rule is that only 1 class does NOT need the Partial keyword.  All other classes of the same name require the Partial keyword and the same access level as the initial class declaration.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Thursday, January 4, 2018 7:49 PM
    Moderator
  • Good to see you around again Reed. :)

    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Thursday, January 4, 2018 7:51 PM
  • Good to see you around again Reed. :)

    "A problem well stated is a problem half solved.” - Charles F. Kettering


    Thanks, good to be back. :)

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Thursday, January 4, 2018 7:53 PM
    Moderator
  • And to add also something. 

    A partial class is not a real class. It is a text extension to the coding of a class (which has not "partial" in its declaration). 


    Success Cor

    Thursday, January 4, 2018 7:56 PM
  • There should be a valid reason for using a partial class in the first place. Generally speaking one would write one or more classes that a form or any form that has access to said classes can instantiate and use methods within these classes rather than use partial classes for a form as currently attempting.  

    No matter, you can split things up as per below be the partial classes be in separate files or the same file for proof of concept.

    Public Class Form1
        Private mKaren As String
        Protected mSomeValue As Integer
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            ApplicationSettings.Instance.CurrentEnvironment = "PRODUCTION"
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Console.WriteLine(ApplicationSettings.Instance.CurrentEnvironment)
        End Sub
    End Class
    Partial Public Class Form1
        Public Sub Demo1()
            mKaren = "I'm Karen"
            mSomeValue = 1
            Button1.Text = "B1"
        End Sub
    End Class
    Partial Public Class Form1
        Public Sub Demo2()
            mKaren = "I'm Karen"
            mSomeValue = 2
            Button2.PerformClick()
        End Sub
    End Class

    Other options are using a singleton pattern (which is overkill) as it's scope is broader then you want.

    I like to keep everything confine to the smallest scope possible. Kind of like how controls are scoped in C# windows forms, private rather than friend (in vb.net)

    So the questions is, does a partial class offer something you can't do without partial classes that is realistic.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, January 4, 2018 8:37 PM
    Moderator
  • Thanks for all the suggestions. Perhaps I should mention that I said initially "I've tried a variety of alternatives, including the addition of Partial to all three files, making all the class statements Public, etc." (I didn't want to burden the topic with every failed variant, I have plenty.)

    To go back to another line of inquiry, "I suspect the problem is due to the way I created the third file with  File | New File | Visual Basic Class   - then changed the generated Class1 name. "

    All the examples I see are of code; and I assume they are meant to be separate files.

    But how does one create such a file and include it in the project so as to ensure its code is part of the class?

    Maybe a snip of the project structure would help. Notice that the new file AudioImageCode.vb is not listed under the class frmBatRen.vb, but outside it. Dragging and dropping in Solution Explorer fails. How can I get that file inside the class or alternatively create a new file within it?

    Thursday, January 4, 2018 9:24 PM
  • It doesn't matter...

    When I recreated the error in order to grab the analyzer message from the output window, I did a right click, add class, and left it named Class1.  Then in that code file, under the Class1 definition, I added the Partial Form1 class.

    Try one more time.  Make sure that you do not edit anything in the frmBatRen.vb or frmBatRen.Designer.vb - they are fine the way they are generated.  Just add your new class (in ANY code file) and be sure to use Partial Public Class frmBatRen.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Thursday, January 4, 2018 9:28 PM
    Moderator
  • Ronks,

    It's rare that you'll ever need a partial class. About the only time there's need would be if the class needs to be extended using another file - an example being "Form1.vb" and "Form1.designer.vb". If you look at the latter it's a partial class.

    I think you're better off starting another question and stating the problem you're running into.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Thursday, January 4, 2018 9:28 PM
  • Something is odd here. I re-created the class with Partial and Public in all three and added a reference in the third file back to the second; and that compiles.

    But the reference from the second file to Teststring in the third file throws a "Not declared" error. I'm unclear why this works only in one direction. I've heard of old compilers that did this but not current ones.

    As to "stating the problem you're running into", I'm not sure what more I can say. I have a large .vb file of a single Form class that contains frequently-modified code and a large bulk of rarely-modified code which I would like to break out into a separate file for convenience. For background, I'm running VS 15.4.2 on a 64-bit Windows 10 desktop.

    Thanks to everyone for their help; but it looks like living with the single large file is a better alternative than fighting this issue.

    Friday, January 5, 2018 4:52 AM
  • Ronks,

    I'm glad you did not pay any attention to my message. You are just creating in 3 parts a long text  file. Some odd things people can do are solved by Visual Studio but then you get warnings, not errors. 

    A Class is before it is build just a textfile but that I wrote already. 


    Success Cor

    Friday, January 5, 2018 6:55 AM
  • Something is odd here. I re-created the class with Partial and Public in all three and added a reference in the third file back to the second; and that compiles.

    But the reference from the second file to Teststring in the third file throws a "Not declared" error. I'm unclear why this works only in one direction. I've heard of old compilers that did this but not current ones.

    As to "stating the problem you're running into", I'm not sure what more I can say. I have a large .vb file of a single Form class that contains frequently-modified code and a large bulk of rarely-modified code which I would like to break out into a separate file for convenience. For background, I'm running VS 15.4.2 on a 64-bit Windows 10 desktop.

    Thanks to everyone for their help; but it looks like living with the single large file is a better alternative than fighting this issue.

    What do you consider the "first, second, and third" files?  The main file, frmBatRen.vb should have the class declared:

       Public Class frmBatRen

    The designer class should be declared as:

       Partial Public Class frmBatRen

    There should be NO CHANGES made to the designer class.  That entire code file could be regenerated by the IDE so you must not write any of your own code within it.  About the only time you open this file and fiddle with it is when the IDE messes up and corrupts the form in the designer (which rarely occurs anymore with the latest VS).

    Your new class which extends the other two should be declared as:

       Partial Public Class frmBatRen

    Any members you add to this class should be accessible from the code in frmBatRen.vb (and vice versa).  frmBatRen.Designer.vb is irrelevant as you should not be trying to write any code in that file.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Friday, January 5, 2018 2:02 PM
    Moderator
  • Here is an example

    https://1drv.ms/u/s!AtGAgKKpqdWjiRzeaNIehQ8N9ZPN

    Project


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, January 5, 2018 2:23 PM
    Moderator
  • > What do you consider the "first, second, and third" files?

    I mean the order of the file snippets in my initial post.

    #1 - frmBatRen.Designer.vb, which I do know enough to leave my hands off of.

    #2 - frmBatRen.vb, where my code is.

    #3 - AudioImageCode.vb, where I thought I could stash the less frequently modified lines from frmBatRen.vb.

    I have declared the two files I put code in (#2 & 3) to be Public, and let the VS-generated one untouched, to default.

    I agree that "Any members you add to this class should be accessible from the code in frmBatRen.vb (and vice versa)."

    But they ain't, and what I thought would be a simple convenience is turning into a rabbit hole, so I think I'll just live with one long .vb file that I know works. Thanks for your help.

    Friday, January 5, 2018 5:05 PM
  • Thanks! I just retrieved the zip file and I'll take a look at it.

    How did you create the second .vb file in the project?

    With File | New file | Text file, or some other way?

    Friday, January 5, 2018 5:11 PM
  • Thanks! I just retrieved the zip file and I'll take a look at it.

    How did you create the second .vb file in the project?

    With File | New file | Text file, or some other way?

    Each class I created was by right clicking on the project, select add -> class

    Gave the class name say Demo1. Then opened the class and change from Public class Demo1 to 

    Partial Public Class Form1

    That's it.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by ronks Friday, January 5, 2018 6:08 PM
    Friday, January 5, 2018 5:39 PM
    Moderator
  • Bingo! It cross-compiles now with the references resolved. Thanks!

    I used a slightly different approach based on yours. Right-click on Project, then

    Add | New Item | Code file   - to avoid creating another class (probably harmless, but...)

    At first I got a warning about a conflict in the namespace, but when I added the Partial keyword explicitly it cleared.

    • Marked as answer by ronks Friday, January 5, 2018 6:30 PM
    Friday, January 5, 2018 6:08 PM