0

I created a new class with this function:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Management;

namespace ScreenVideoRecorder
{
    class GetMemory
    {
        private static void DisplayTotalRam()
        {
            string Query = "SELECT MaxCapacity FROM Win32_PhysicalMemoryArray";
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(Query);
            foreach (ManagementObject WniPART in searcher.Get())
            {
                UInt32 SizeinKB = Convert.ToUInt32(WniPART.Properties["MaxCapacity"].Value);
                UInt32 SizeinMB = SizeinKB / 1024;
                UInt32 SizeinGB = SizeinMB / 1024;
                //Console.WriteLine("Size in KB: {0}, Size in MB: {1}, Size in GB: {2}", SizeinKB, SizeinMB, SizeinGB);
            }
        }
    }
}

I want in Form1 to display the SizeinKB MB and GB on labels.

joneK
  • 241
  • 1
  • 4
  • 13
  • 1
    possible duplicate of [How can I return multiple values from a function in C#?](http://stackoverflow.com/questions/748062/how-can-i-return-multiple-values-from-a-function-in-c) – Andrew Savinykh Jun 10 '13 at 21:56
  • What does your C# book or tutorial series say about using objects and returning data? – Anthony Pegram Jun 10 '13 at 21:57

2 Answers2

3

EDIT

Since the conversion from KB to MB / GB is standard it can be moved out of this function, so I would just return a list of UInt32s, since you aren't displaying any other info to differentiate the numbers:

private static void DisplayTotalRam()
{
    string Query = "SELECT MaxCapacity FROM Win32_PhysicalMemoryArray";

    List<Uint32> sizes = new List<UInt32>();

    ManagementObjectSearcher searcher = new ManagementObjectSearcher(Query);
    foreach (ManagementObject WniPART in searcher.Get())
    {
        UInt32 SizeinKB = Convert.ToUInt32(WniPART.Properties["MaxCapacity"].Value);
        sizes.Add(SizeinKB);
    }
    return sizes;
}

Then just do the calculation in the form:

List<UInt32> sizes = GetMeMory.DisplayTotalRam();
foreach(UInt32 sizeInKB in sizes)
{
   // show sizeInKB on label

   UInt32 sizeInMB = sizeInKB / 1024;
   // show sizeInMB on label

   // ..etc.
}

There are several ways to do this; two of the easier ways are:

  1. Return an instance of a struct or class that contains these values (clean, must define the class, struct)
  2. Return an array of Int32s (simple, not clean)
D Stanley
  • 149,601
  • 11
  • 178
  • 240
0

You can add string parameters to the method:

DisplayTotalRam(ref String one, ref String two)

And use them in the method. So if you have 2 labels you want to set text of, you write:

DisplayTotalRam(ref label1.Text, ref label2.Text);
Janman
  • 698
  • 1
  • 9
  • 25
  • Yes, he can, but it doesn't solve his problem. –  Jun 10 '13 at 21:54
  • The questioner wants to get some integer values. Giving two strings to the method will not do the work. `label1.Text` and `label2.Text` will not change in the original place after You call the method this way. –  Jun 10 '13 at 22:01
  • That was just an example. You can pass references to integers as well using the "ref" keyword. – Janman Jun 10 '13 at 22:03
  • I know this, but Your answer is still incorrect. –  Jun 10 '13 at 22:06
  • You are right. I'm gonna let it stay anyways, so future users can have a look. – Janman Jun 10 '13 at 22:22