简体   繁体   中英

Instantiating an object of one class in another

Could some one let me know how to instantiate and object from within another class in C#.

The first 3 string variables here (secureID, EmailAddress and PhoneNum) I would like to place them in another class and assign values to them in this class. In c++ we would use friend class, couldn't find anything like that for C#.

Further clarification:

I would like to take this code:

static string SecureId;
  static string EmailAddress;
  static string PhoneNum;

and place it in it's own class. Lets call it public class myMsg. I would like to instanitate myMsg in the below class Program and be able to assign values to its fields as such myMsg.SecureId = strDataParse[0]. I am having issues accessing myMsg fields through class Program. Hope that helps.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;

namespace EmailSMSSocketApp
{

   class Program
    {

        static string SecureId;
        static string EmailAddress;
        static string PhoneNum;

        static byte[] Buffer { get; set; }
        static Socket _socket;
        static void Main(string[] args)
        {
            _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            _socket.Bind(new IPEndPoint(IPAddress.Any, 1234));
            _socket.Listen(100);

            Socket accepted = _socket.Accept();
            Buffer = new byte[accepted.SendBufferSize];
            int bytesRead = accepted.Receive(Buffer);
            byte[] formatted = new byte[bytesRead];

            for (int i = 0; i < bytesRead; i++)
            {
                formatted[i] = Buffer[i];

            }
            string strData = Encoding.ASCII.GetString(formatted);
            //Console.Write(strData + " ASCII to strData" + "\r\n");
            string[] strDataParse = strData.Split(',');
            foreach (string word in strDataParse)
            {
               // Console.WriteLine(word);
                SecureId = strDataParse[0];
                EmailAddress = strDataParse[1];
                PhoneNum = strDataParse[2];

            };

            Console.WriteLine(SecureId + " Outside loop");
            Console.WriteLine(EmailAddress + " Outside loop");
            Console.WriteLine(PhoneNum + " Outside loop");


            Console.Read();
            _socket.Close();
            accepted.Close();



        }
    }



}

Classes and members are private by default. You need to mark the class and the members as public or internal to be visible outside of the class.

You are right that there are no friend classes in C# although there are nested types (classes defined within other classes).

Why does C# not provide the C++ style 'friend' keyword?

The comment above that you would access it as Program.SecureId is correct if it were public or internal and it is common practice to make the field a property although not necessary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM