Accessing a public byte[] array from another class

RuvenS

In the program I'm writing I have a class that has a public byte array in it that I want to access and use.

class HasByte
{
  public byte[] theByteArray = new byte[4];

  public HasByte(IPAddress someAddress)
  {
    theByteArray = someAddress.GetAddressBytes();
  }
}

class WantsByte
{ 
  IPAddress address = IPAddress.Parse("192.168.1.1");
  HasByte theInstance = new HasByte(address);
  //do something with theInstance.theByteArray[2] for example
}

Currently, the byte array I access through theInstance.theByteArray is all 0's for some reason that I would like to know.

Thanks.

Bojan Komazec

In your class WantsByte you are trying to initialize member theInstance via another non-static member address and compiler must be complaining with Error CS0236. You can move theInstance initialization to a constructor:

class WantsByte
{ 
    IPAddress address = IPAddress.Parse("192.168.1.1");
    HasByte theInstance;

    public WantsByte()
    {
        theInstance = new HasByte(this.address);
    }
}

Demo example:

using System;
using System.Net;
using System.Linq;

public class Program
{
    public static void Main()
    {
        var wants = new WantsByte();    
    }
}

class HasByte
{
    public byte[] theByteArray = new byte[4];

    public HasByte(IPAddress someAddress)
    {
        theByteArray = someAddress.GetAddressBytes();
    }
}

class WantsByte
{ 
    IPAddress address = IPAddress.Parse("192.168.1.1");
    HasByte theInstance;

    public WantsByte()
    {
        theInstance = new HasByte(this.address);

        // do something with theInstance.theByteArray[2] for example
        // Let's print all elements of the array
        Console.WriteLine(String.Join(",", theInstance.theByteArray.Select(o => o.ToString()).ToArray()));
    }
}

gives output:

192,168,1,1

Alternatively, in class WantsByte, you can make address to be static member which would guarantee that it will be initialized before first use of a class. You can then reference it in theInstance initializer:

using System;
using System.Net;
using System.Linq;

public class Program
{
    public static void Main()
    {
        var wants = new WantsByte();
        wants.DoSomethingWithHasByte();
    }
}

class HasByte
{
    public byte[] theByteArray = new byte[4];

    public HasByte(IPAddress someAddress)
    {
        theByteArray = someAddress.GetAddressBytes();
    }
}

class WantsByte
{ 
    static IPAddress address = IPAddress.Parse("192.168.1.1");

    HasByte theInstance = new HasByte(WantsByte.address);

    public void DoSomethingWithHasByte()
    {
        Console.WriteLine(String.Join(",", theInstance.theByteArray.Select(o => o.ToString()).ToArray()));
    }
}

also gives the same output:

192,168,1,1

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Accessing public class from another class in wpf

From Dev

Accessing public method of another class

From Dev

Incorrect array values when accessing an array from another class?

From Dev

Accessing a public final constant in another class?

From Dev

Accessing IBOutlet from another class

From Dev

Accessing non public class from other package

From Dev

Accessing a public method from a class inside a SurfaceListBox

From Dev

accessing the member of a class of pointer array of another class

From Dev

Accessing Companion Class variable from another class

From Dev

Accessing variables in a class from another class

From Dev

Accessing class variable from another class

From Dev

Accessing class private members from another class

From Dev

Accessing Companion Class variable from another class

From Dev

Accessing class variable from another class

From Dev

Accessing class private members from another class

From Dev

Accessing a class' member variable from another class

From Dev

Generating RSA public key from byte array

From Dev

Generating RSA public key from byte array

From Dev

Accessing class variables from an array

From Dev

Accessing static variable from another class

From Dev

accessing variables from another class without instantiating

From Dev

Accessing MainForm from another class file

From Dev

Accessing Protected Functions from Another Class - Flash

From Dev

Accessing another class column from a Pointer in Parse

From Dev

accessing struct from one class to another

From Dev

Accessing variables from another class Android Studio

From Dev

Accessing and changing text in jTextField from another class

From Dev

Accessing MySQLi connection from another class

From Dev

Accessing static variable from another class

Related Related

  1. 1

    Accessing public class from another class in wpf

  2. 2

    Accessing public method of another class

  3. 3

    Incorrect array values when accessing an array from another class?

  4. 4

    Accessing a public final constant in another class?

  5. 5

    Accessing IBOutlet from another class

  6. 6

    Accessing non public class from other package

  7. 7

    Accessing a public method from a class inside a SurfaceListBox

  8. 8

    accessing the member of a class of pointer array of another class

  9. 9

    Accessing Companion Class variable from another class

  10. 10

    Accessing variables in a class from another class

  11. 11

    Accessing class variable from another class

  12. 12

    Accessing class private members from another class

  13. 13

    Accessing Companion Class variable from another class

  14. 14

    Accessing class variable from another class

  15. 15

    Accessing class private members from another class

  16. 16

    Accessing a class' member variable from another class

  17. 17

    Generating RSA public key from byte array

  18. 18

    Generating RSA public key from byte array

  19. 19

    Accessing class variables from an array

  20. 20

    Accessing static variable from another class

  21. 21

    accessing variables from another class without instantiating

  22. 22

    Accessing MainForm from another class file

  23. 23

    Accessing Protected Functions from Another Class - Flash

  24. 24

    Accessing another class column from a Pointer in Parse

  25. 25

    accessing struct from one class to another

  26. 26

    Accessing variables from another class Android Studio

  27. 27

    Accessing and changing text in jTextField from another class

  28. 28

    Accessing MySQLi connection from another class

  29. 29

    Accessing static variable from another class

HotTag

Archive