0

I need to get a private string from a class, and count the frequency of words in the string.

the counting is the easy part... the bit I am struggling with is getting the string from the second class.

Heres what I am trying to get

public class GetString
{
  private string myText = "this is the string that i need to get"         
  private string text;

  public GetString()
  {
    text = myText
  }

any and all help would be very much appreciated. I am also told I cannot edit this class

Simon Price
  • 3,011
  • 3
  • 34
  • 98
  • 2
    How did you tried getting the string?You could return your private string from your getString() method – Joseph Jul 31 '15 at 07:29
  • It's not realy clear what you want to achieve. Can you provide your try and the expected result? – take Jul 31 '15 at 07:32
  • im beign told, I cannot edit this class where the string is held – Simon Price Jul 31 '15 at 07:34
  • 1
    Could you add some more information about where you are trying to get it? the obvious answer would be to make it a property with a private setter but if you can't change the class thats not possible.. – Sayse Jul 31 '15 at 07:35
  • 3
    `getString` as class-name? Seriously? Bad idea. Whoever designed this class should not have the right to tell you to NOT change it. – MakePeaceGreatAgain Jul 31 '15 at 07:36

6 Answers6

5

You have three options in my view:

  1. You can make myText public.
  2. You can return myText from another public member, preferably a property.
  3. You can access the value via reflection (see: How to get the value of private field in C#?).
Alex Booker
  • 10,487
  • 1
  • 24
  • 34
1

This seems rather fundemental, you have nothing in your getString class returning the string. Try something like

public class getString
{
  private string myText = "this is the string that i need to get"

  public String getString()
  {
    return myText
  }

getString a = new getString();
String hiddenString = a.getString();
BugFinder
  • 17,474
  • 4
  • 36
  • 51
  • I believe he was referring to the original class created by the user. His answer is legit, given the context. It would be better to question the original user's choice of wording – Draken Jul 31 '15 at 07:33
  • Edited to fix the () and no, I dont approve of the OP's function name, I was just making it work (btw Im female!) – BugFinder Jul 31 '15 at 07:35
  • Im being told that I cannot edit the class, the class in this question is a mock of what i have to work with – Simon Price Jul 31 '15 at 07:37
  • 1
    Apologies, should have used they for gender neutrality! – Draken Jul 31 '15 at 07:38
1

Use Properties (property is a member that provides a flexible mechanism to read, write, or compute the value of a private field.):

public string MyText
{
    get { return myText; }
    private set { myText = value; }
}
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
  • It should be a private string, so the setter should have a private setting and is set from within the class – Draken Jul 31 '15 at 07:39
1

It seems that your getString class (by the way - bad naming a class with lowercase letter) contains a text property. If this property is public you can use it to get the string. If it is not public, there might be a method that exposes it. Your code is not complete, so it cannot be said for certain.

If there are no public properties or methods that expose the string, then the only way to get is through reflection

stann1
  • 576
  • 3
  • 11
1

You cannot use the constructor for this purpose. Try this:

public class Foo
{
    private string myText = "this is the string that i need to get";

    public Foo()
    {
    }

    public String GetString()
    {
        return this.myText;
    }
}

Every method should either have the void keyword or the return type, which is in your case a String.

ar53nic
  • 91
  • 8
0

To follow on from @Petrichor, you could also potentially use interfaces:

public interface IHasText
{
  string GetPrivateText();
}

public class GetString : IHasText
{
  private string myText = "this is the string that i need to get";

  string IHasText.GetPrivateText()
  {
    return myText;
  }
}

var val = new GetString();
var asInterface = (IHasText)val;

string text = asInterface.GetPrivateText();
Matthew Abbott
  • 60,571
  • 9
  • 104
  • 129