El Jono Cenaro El Jono Cenaro - 2 months ago 7
C# Question

How to use the class string of another class?

I have 2 .cs file. One contains string and the other post the string. I want to use the string from the class but when i add "public" to the string, the code messed up.
Here is the class file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
namespace DreamJ
{
public class second
{
public void Test()
{
WebClient webClient = new WebClient();
webClient.Headers.Add("User-Agent: Other");
webClient.Headers.Add("Accept", "*/*");
string content = webClient.DownloadString("https://example.com");
string result = content.Replace("\"", " ");
string more = result.Replace("},", "},\n");
}


}
}


I want to get the "more" to the main class file, example: a.Send(more) (can't show it). I'm new to c# so help me pls. (Sorry for my bad english)

Answer

Just create more as a public member. Either that or return more from your function.

Solution 1:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
namespace DreamJ
{
    public class second
    {
        public void Test()
        {
            WebClient webClient = new WebClient();
            webClient.Headers.Add("User-Agent: Other");
            webClient.Headers.Add("Accept", "*/*");
            string content = webClient.DownloadString("https://example.com");
            string result = content.Replace("\"", " ");
            more = result.Replace("},", "},\n");
        }

       public string more
       {
           get;
           set;
       }
    }
}

Solution 2:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
namespace DreamJ
{
    public class second
    {
        public string Test()
        {
            WebClient webClient = new WebClient();
            webClient.Headers.Add("User-Agent: Other");
            webClient.Headers.Add("Accept", "*/*");
            string content = webClient.DownloadString("https://example.com");
            string result = content.Replace("\"", " ");
            more = result.Replace("},", "},\n");
            return more;
        }

    }
}