Click here to Skip to main content
15,884,981 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
how to get the string value in class file
C#
string str=comboBox.SelectedItem.ToString();


this is my class file

C#
class FTClientCode
        {
            public static string curMsg = "Idle";
            public static void SendFile(string fileName)
            {
                try
                {
                   
                    IPAddress[] ipAddress = Dns.GetHostAddresses(str);
                    IPEndPoint ipEnd = new IPEndPoint(ipAddress[0], 5656);
                    Socket clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);


                    string filePath = "";

                    fileName = fileName.Replace("\\", "/");
                    while (fileName.IndexOf("/") > -1)
                    {
                        filePath += fileName.Substring(0, fileName.IndexOf("/") + 1);
                        fileName = fileName.Substring(fileName.IndexOf("/") + 1);
                    }


                    byte[] fileNameByte = Encoding.ASCII.GetBytes(fileName);
                    if (fileNameByte.Length > 850 * 1024)
                    {
                        curMsg = "File size is more than 850kb, please try with small file.";
                        return;
                    }

                    curMsg = "Buffering ...";
                    byte[] fileData = File.ReadAllBytes(filePath + fileName);
                    byte[] clientData = new byte[4 + fileNameByte.Length + fileData.Length];
                    byte[] fileNameLen = BitConverter.GetBytes(fileNameByte.Length);

                    fileNameLen.CopyTo(clientData, 0);
                    fileNameByte.CopyTo(clientData, 4);
                    fileData.CopyTo(clientData, 4 + fileNameByte.Length);

                    curMsg = "Connection to server ...";
                    clientSock.Connect(ipEnd);

                    curMsg = "File sending...";
                    clientSock.Send(clientData);

                    curMsg = "Disconnecting...";
                    clientSock.Close();
                    curMsg = "File transferred.";

                }
                catch (Exception ex)
                {
                    if (ex.Message == "No connection could be made because the target machine actively refused it")
                        curMsg = "File Sending fail. Because server not running.";
                    else
                        curMsg = "File Sending fail." + ex.Message;
                }

            }
        }
Posted
Comments
Dean Oliver 28-Feb-12 0:47am    
are you looking to override ToString() method?
_Maxxx_ 28-Feb-12 0:48am    
I'm not clear on 'where' you want to get the value from your combo into your class? Essentially, you probably need a property in the class that you set to the selected value, when the combo changes value.
saravana__ 28-Feb-12 0:50am    
get the string value in str
try
{

IPAddress[] ipAddress = Dns.GetHostAddresses(str);
Sergey Alexandrovich Kryukov 28-Feb-12 0:51am    
Not clear. How about using "improve question" above?
--SA
saravana__ 28-Feb-12 1:05am    
IPAddress[] ipAddress = Dns.GetHostAddresses(str);

in this above line i want to get the combobox selected item in string[str]

In your class add a property

public static string HostAddressString {get;set;}


Write an event handler for the IndexChanged event on the combo - and in that handler

FTClientCode.HostAddressString = combobox.SelectedItem.ToString();


so, in the class you can access HostAddressString

IPAddress[] ipAddress = Dns.GetHostAddresses(HostAddressString);
 
Share this answer
 
Have a look at this link... from CodeProject

An INI file handling class using C#[^]

This may give you an idea
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900