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();
}
}
}