1

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



        }
    }



}
Greg G
  • 11
  • 3
  • Pass them in on the constructor. I only see one class though; could you narrow the code down to that which is relevant to your question? – BradleyDotNET Oct 21 '14 at 00:54
  • The variables that you mentioned are static. They are class variables so you don't need to create an instance of Program class. You should be able to access them using something like Program.SecureId but you will need to define getter and setter. – h-rai Oct 21 '14 at 00:59
  • I tried to clarify this a bit more. Let me know if that helps. – Greg G Oct 21 '14 at 19:41

1 Answers1

0

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.

Community
  • 1
  • 1
jy247
  • 1
  • 1
  • I had the class as public but still wouldn't let me access the fields from the program class. I updated the above post to clarify. Would the get/set method help in this instance? – Greg G Oct 21 '14 at 19:55
  • ok, make the msg class public (public class msg), make the fields in the class public (public static string SecureId), i see no reason to make them static but if you want to you access them as classname.fieldname (msg.SecureId). To instantiate an instance of a non-static class (msg myMsg = new msg()) the access them as class variables (myMsg.SecureId). You can make them properties (get/set) but it doesn't matter in this case – jy247 Oct 22 '14 at 00:51