Solved

VB.net Problem Getting info from class

Posted on 2014-01-19
7
572 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
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 26

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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now