Define Class?

I dont know if thats the right thing to say or not being I'm still green, but

If I had a calculation that I needed to do often, is there a way t o make that (Public(Dont know if thats right or not))

I am wanting not to have to type the calculation over and over

just wanting to put it in one place and refer to it if possible.

Then how would you call it

Davids Learning



Answer this question

Define Class?

  • triciameza

    Or you can use a module and method in VB

    Module Calculator

    Public Function Add(ByVal val1 As Integer, ByVal val2 As Integer) As Integer

    Return val1 + val2

    End Function

    End Module

    The important thing is that you create a method (function) which allows you to reuse the functionality by simply calling it with different parameters.

    A Shared method in a class does pretty well the same thing as a method in a module. You dont need to create a separate module if you already have one in you project - that you can place the method into.

    If you want to reuse this in a number of different projects - then you can create a class Library Project - put this module/class method into it and compile up a DLL. You can then add this dll to your projects by adding a reference to this dll and then you can use this canned functionality in all you projects by simply adding a reference to the class library.



  • Pawel J

    Console.WriteLine(Calculator.Add(10, 5))

    whats the 10,5

    If its what I think it is add 10 +5

    What if I need to use a datasets info to do my calcs, with some if statements

    Then ,heres another use I can think of for something else totally different

    what if i needed to compare strings and modify controls on a form based on a dataset.

    Thanks, didnt think anyone would be here on a sunday

    Davids Learning


  • Chris Forrester

    Hi, you can do like this:

    Class Calculator

    Public Shared Function Add(ByVal val1 As Integer, ByVal val2 As Integer) As Integer

    Return val1 + val2

    End Function

    End Class

    And call it like this:

    Console.WriteLine(Calculator.Add(10, 5))

    Hope this helps


  • Dany V

    Thats what I was looking for!

    Thanks

    Davids Learning


  • Deis

    1) 10,5 -> these are the parameters to pass to a method of a class. So in this case, the method "Add" will add the parameters passed together (10+5)

    Val1 = 10

    Val2 = 5

    2) if you have a dataset which contains the values you wish to perform some calculation on, you will need to go through each row and get the specific column containing the numeric values, adding them up as you go along on each row

     

    here is a close example, except this one is going through a datagridview directly:

    http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=593468&SiteID=1

     

     

     



  • Define Class?