Results 1 to 7 of 7

Thread: Can my.settings be used in a class?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2006
    Posts
    607

    Can my.settings be used in a class?

    I have made a error logging class which simply just writes to a text file. the mthod is called "log(text as string)"

    The user has a option to "add timestamp" to begining of each line.

    Instead of doing if statements on every exception, or activity to see if I should send the string with a timestamp or not, cant I just put the if statement in my class method?

    for ex:

    Code:
    Public Class
    
    Sub log(text as string)
    
    if my.settings.addTimeStamp = true then
    'add time 
    else
    'leave it
    end if
     
    'log it
    
    end sub
    end Clas

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Can my.settings be used in a class?

    Of course it can be used in a class. Just about everything IS a class. All your forms are classes, etc., etc. If you're using My.Settings there then you're already using it in a class.

    Are you actually asking whether it can be accessed in a class library, i.e. a DLL? If so then the answer is "no". 'My' is a namespace under the root namespace of each project. Each application and library project has it's own My namespace. The Settings property you access in your application is a member declared in your application, therefore it only exists in your app.

    What you could do is have your Log method take a Boolean parameter to indicate whether to add a timestamp. You can then simply pass My.Settings.AddTimeStamp as a parameter instead of having to use an If statement before calling it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2006
    Posts
    607

    Re: Can my.settings be used in a class?

    Thanks for the reply.

    Whats the difference between a class library and a class.
    I thought all classes that I add to my solution turn into a .dll after compiling.

    my log class is just right clicking on the solution and adding a class.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Can my.settings be used in a class?

    You don't add classes to the solution. The solution contains projects. You add classes to projects. Each project is compiled to an executable file; either an EXE or a DLL. An EXE is an application and a DLL is a library.

    What would you say the difference is between a book and a library of books? So, what do you think the difference is between a class and a class library?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Can my.settings be used in a class?

    If you want to avoid doing if statements to find out whether you should include the timestamp or not on the log string, the answer is no, it's not possible. It doesn't matter where you build your log string, somehow you still have to check to see if the timestamp is needed every time you build the log string to write to the file.

  6. #6
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Can my.settings be used in a class?

    I've used the My.Settings object in a class library.
    Code:
        Public Class SqlCode
    
            Public Shared Function SelectCurrentAndPreviousEnrollments() As String
    
                Dim sql As String = "Select * from PlanDataName.dbo.table"
    
                Return sql.Replace("PlanDataName", My.Settings.PlanDataName)
    
            End Function
    
        End Class

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Can my.settings be used in a class?

    I should qualify what I posted earlier. Yes you can use My.Settings in a class library, but the settings it accesses must be created in the class library project. The My you will be referring to is a namespace under your library project. It is NOT the same My that you refer to in any application that references that library. That means that you cannot create a setting in an application and then access that setting via My.Settings in a class library. That makes sense because the class library has no idea what application(s) might reference it. More than one could and they might not have the same settings defined.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width