Solved

VB.net Problem Getting info from class

Posted on 2014-01-19
7
598 Views
Last Modified: 2014-01-19
Hi. I am trying use the following code in a standard module to get the information
from the class further on but it doesn't work. Is it something about "Friend Function"?

   Public Function oGet()
        NsgBox GetProcessorId()
    End Function


Imports System
Imports System.Management
Imports System.Windows.Forms


Public Class clsComputerInfo

    Friend Function GetProcessorId() As String
        Dim strProcessorId As String = String.Empty
        Dim query As New SelectQuery("Win32_processor")
        Dim search As New ManagementObjectSearcher(query)
        Dim info As ManagementObject

        For Each info In search.Get()
            strProcessorId = info("processorId").ToString()
        Next
        Return strProcessorId

    End Function

    Friend Function GetMACAddress() As String

        Dim mc As ManagementClass = New ManagementClass("Win32_NetworkAdapterConfiguration")
        Dim moc As ManagementObjectCollection = mc.GetInstances()
        Dim MACAddress As String = String.Empty
        For Each mo As ManagementObject In moc

            If (MACAddress.Equals(String.Empty)) Then
                If CBool(mo("IPEnabled")) Then MACAddress = mo("MacAddress").ToString()

                mo.Dispose()
            End If
            MACAddress = MACAddress.Replace(":", String.Empty)

        Next
        Return MACAddress
    End Function

    Friend Function GetVolumeSerial(Optional ByVal strDriveLetter As String = "C") As String

        Dim disk As ManagementObject = New ManagementObject(String.Format("win32_logicaldisk.deviceid=""{0}:""", strDriveLetter))
        disk.Get()
        Return disk("VolumeSerialNumber").ToString()
    End Function

    Friend Function GetMotherBoardID() As String

        Dim strMotherBoardID As String = String.Empty
        Dim query As New SelectQuery("Win32_BaseBoard")
        Dim search As New ManagementObjectSearcher(query)
        Dim info As ManagementObject
        For Each info In search.Get()

            strMotherBoardID = info("SerialNumber").ToString()

        Next
        Return strMotherBoardID

    End Function

End Class
0
Comment
Question by:murbro
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 40
ID: 39792403
It can be the Friend.

A Friend method can be called only inside of the project in which it is defined. This means inside the project you can use it as a Public, but outside of the project if is treated as a Private and cannot be called.

So, if your module is in application and your Friend method is in a dll, you cannot call it. If both are in the same application or both are in the same dll, then your module should be able to call the Friend functions.

What do you mean exactly by "but it doesn't work"? Is it that you cannot call it, that you do not get the expected result or that you get an exception?
0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 39792415
NsgBox GetProcessorId()???

Most likely MsgBox.
0
 

Author Comment

by:murbro
ID: 39792426
Ha ha..just a typo. Had tried other techniques
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:murbro
ID: 39792431
Hi James
If I put the following code in a standard module in the same project as the class
I get the error: "Reference to a non-shared member requires an object reference"
  Public Function oGet() As String
        oGet = clsComputerInfo.GetProcessorId
    End Function
0
 
LVL 12

Expert Comment

by:ktaczala
ID: 39792447
put all your functions inside the module
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 39792448
This code is a class.

Except for very special cases, you do not put a class in a module. A class usually stands alone in its own file.

And normally, you need to create an object to use it:

Dim yourObject As New clsComputerInfo()
oget = yourObject.GetProcessorID

Or maybe easier, paste the code into your module, but remove the Public Class clsComputerInfo and the End Class lines. This way, the methods will become part of your module and you will be able to call them directly as you usually do.

Note that the 3 Imports lines at the top should not be included in the module, but should be put before the module, as they are located before the Class in the original code.
0
 

Author Closing Comment

by:murbro
ID: 39792464
Thanks James. I very rarely use classes. Should have know this. Appreciate the help
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.net and sql server 4 54
Expression Evaluater 3 45
VB.net Invalid Cast From 'Enum' to 'DateTime' Couldn't store <Null> in oDate Column 4 37
Need help with another query 10 36
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question