Solved

VB.net Problem Getting info from class

Posted on 2014-01-19
7
611 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:Murray Brown
[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:Murray Brown
ID: 39792426
Ha ha..just a typo. Had tried other techniques
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:Murray Brown
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:Murray Brown
ID: 39792464
Thanks James. I very rarely use classes. Should have know this. Appreciate the help
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

617 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