locked
How to create and use classes in Visual Studio LightSwitch 2013 RRS feed

  • Question

  • I have a problem with creating and using classes in LightSwitch 2013. I used the example by Beth on sending smtp mail to try to send a mail in the 2013 version but is not working. I created the class and stored it in a folder  I named utilitypack under the server section.

    So, I went back to consume it under one of the entities class but it flagged error -smtpmailhelper not defined!

    Please help me, how can I declare the class in the consuming area?

    Akin-Fred(TerraByte Corps)

    Wednesday, November 19, 2014 2:57 PM

Answers

  • The problem here is that the calling code is directly on the Entity so it is present in both the Server and Desktop Client projects (if you look in the UserCode\Shared folder in the Client project, you will see the Loans.lsml.vb file was added as a "linked" file, meaning the physical file exists only in the Server folder, but is also compiled in the Client project).  The utility class however, only exists in the Server project.  You can fix this buy adding your 2 utility files as links to the Client project.  To do this, right-click on the UserCode\Shared folder in the Client project and select "Add >> Existing Item", then browse to the location of the 2 utility classes in the Server project's folder and select the files.  Then you need to change the drop-down in the lower right corner to be "Add as link".  After this your code should compile.


    David Kidder | Senior SDET | Microsoft | LightSwitch

    • Proposed as answer by Dave KidderModerator Tuesday, November 25, 2014 3:51 PM
    • Marked as answer by Angie Xu Friday, November 28, 2014 2:15 AM
    Tuesday, November 25, 2014 3:51 PM
    Moderator

All replies

  • You probably need to include the namespace where the helper is located, for example, in C#

    using LightSwitchApplication.UtilityPack;


    Nicolás Lope de Barrios
    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, please remember to "Mark as Answer". This will help other people find answers to their problems more quickly.

    Wednesday, November 19, 2014 3:42 PM
  • Thanks Nicolas. I've tried the stuff before and it did not work. I have used the imports statement to import the class, it is still flagging the same error that SMTPmailhelper not defined. I tried to switch to file view in order to see the usercode folder under the server but behold, it is not found there and that was the reason I created the folder - utitlitypack

    Akin-Fred(TerraByte Corps)

    Thursday, November 20, 2014 7:52 AM
  • Hi,

    Name the folder to UserCode

    Put the helper inside that folder and it will work.

    Good luck

    Sven


    Sven Elm


    • Edited by Sven Elm Thursday, November 20, 2014 3:45 PM
    Thursday, November 20, 2014 3:44 PM
  • Hi,

    Name the folder to UserCode

    Put the helper inside that folder and it will work.

    Good luck

    Sven


    Sven Elm


    You don't have to use import. It will find it anyway.

    Sven


    Sven Elm

    Thursday, November 20, 2014 3:46 PM
  • You can even create a separate project if want.  Call it <SolutionName>.Utilities or something and add a reference to it in the lightswitch server project.  To call the code from inside lightswitch, add the using or import statement as described above. 

    If you can post your code, it will be much easier to help you.  Is your class static or does it need a new instance?  Does it have public methods exposed?  These are the kinds of things we need to know from looking at the code.

    Thursday, November 20, 2014 4:17 PM
  • You can even create a separate project if want.  Call it <SolutionName>.Utilities or something and add a reference to it in the lightswitch server project.  To call the code from inside lightswitch, add the using or import statement as described above. 

    If you can post your code, it will be much easier to help you.  Is your class static or does it need a new instance?  Does it have public methods exposed?  These are the kinds of things we need to know from looking at the code.


    Exactly.

    Nicolás Lope de Barrios
    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, please remember to "Mark as Answer". This will help other people find answers to their problems more quickly.

    Thursday, November 20, 2014 10:00 PM
  • VB is case sensitive, make sure that you are casing the class name correctly by copying the class name from the class declaration.

    David Kidder | Senior SDET | Microsoft | LightSwitch

    Friday, November 21, 2014 10:49 PM
    Moderator
  • Below are two of the classes  I created in the utilitypack folder:

    Imports

    System.Net


    Imports

    System.Net.Mail


    Imports

    System.Configuration



    Public

    ClassSMTPMailHelper


       

    PrivateSharedReadOnlySMTPServer AsString= My.Settings.SMTPServer

       

    PrivateSharedReadOnlySMTPPort AsInteger= My.Settings.SMTPPort

       

    PrivateSharedReadOnlySMTPUserId AsString= My.Settings.SMTPUsername

       

    PrivateSharedReadOnlySMTPPassword AsString= My.Settings.SMTPPassword

       

    PrivateSharedReadOnlySMTPFromAddress AsString= My.Settings.SMTPFromAddress

       

    PublicSharedSubSendMail(ByValsendFrom AsString,

                                  

    ByValsendTo AsString,

                                  

    ByValsubject AsString,

                                  

    ByValbody AsString)

           

    DimfromAddress = NewMailAddress(sendFrom)

           

    DimtoAddress = NewMailAddress(sendTo)

           

    Dimmail AsNewMailMessage


           

    Withmail

                .From = fromAddress

                .To.Add(toAddress)

                .Subject = subject

               

    Ifbody.ToLower.Contains("<html>") Then


                    .IsBodyHtml =

    True


               

    EndIf


                .Body = body

           

    EndWith


           

    Dimsmtp AsNewSmtpClient(SMTPServer, SMTPPort)

            smtp.Credentials =

    NewNetworkCredential(SMTPUserId, SMTPPassword)

            smtp.Send(mail)

       

    EndSub

    End

    Class

    Here is another one;

    Imports

    System.Configuration

     


    Public

    ClassCreditProcessor


       

    PrivateSharedReadOnlyLoanProcessingFee AsDecimal= My.Settings.LoanProcessingFee

       

    PublicFunctionProcessingFee() AsDecimal


           

    'get the default value stored in the Apps setting and return it to the calling point.


           

    ReturnLoanProcessingFee

       

    EndFunction

    End

    Class

    This is how I tried to consume the classes in the loan entity

    Imports

    System


    Imports

    System.Configuration


    'Imports System.Runtime.CompilerServices


     


    Namespace

    LightSwitchApplication

       

    PublicClassLoan


           

    ' Dim LP As New CreditProcessor


           

    PrivateClsCreditProcesor AsCreditProcessor


           

    PublicMailHelper AsSMTPMailHelper


           

    'Private Shared ReadOnly LoanProcessingFee As Decimal = My.Settings.LoanProcessingFee           'Me..Settings.LoanProcessingFee


           

    PrivateSubInterestonLoan_Compute(ByRefresult AsDecimal)

               

    ' Set result to the desired field value


                result = (Principal * (RepaymentPeriod + 1)) / (200)

               

    ' Me.TotalLoanAmount = Me.Principal + result + 200


               

    ' ClsCreditProcesor = New CreditProcessor


               

    'Get the Loan processing fee from the application configuration setting


               

    Me.TotalLoanAmount = Me.Principal + result + ClsCreditProcesor.ProcessingFee

     

     

               

    'Calculate what to be paid monthly to service the loan


               

    IfRepaymentPeriod > 0 Then


                   

    Me.MonthlyRepayment = FormatCurrency((Me.TotalLoanAmount / RepaymentPeriod), 2)

               

    EndIf


           

    EndSub

    After all and the renaming the folder as UserCode as suggested, It is still flagging the same error that smtpmailhelper or CreditProcessor not defined.

    What more can I do?


    Akin-Fred(TerraByte Corps)

    Tuesday, November 25, 2014 1:58 PM
  • The problem here is that the calling code is directly on the Entity so it is present in both the Server and Desktop Client projects (if you look in the UserCode\Shared folder in the Client project, you will see the Loans.lsml.vb file was added as a "linked" file, meaning the physical file exists only in the Server folder, but is also compiled in the Client project).  The utility class however, only exists in the Server project.  You can fix this buy adding your 2 utility files as links to the Client project.  To do this, right-click on the UserCode\Shared folder in the Client project and select "Add >> Existing Item", then browse to the location of the 2 utility classes in the Server project's folder and select the files.  Then you need to change the drop-down in the lower right corner to be "Add as link".  After this your code should compile.


    David Kidder | Senior SDET | Microsoft | LightSwitch

    • Proposed as answer by Dave KidderModerator Tuesday, November 25, 2014 3:51 PM
    • Marked as answer by Angie Xu Friday, November 28, 2014 2:15 AM
    Tuesday, November 25, 2014 3:51 PM
    Moderator