Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have this code down.

C#
string check = textbox1.Text;

if (duplicate.Contains(check) == true)
{
    int remove = Math.Max(0, final.Count - 25);
    final.RemoveRange(0, remove);

    int delete = Math.Max(0, duplicate.Count - 5);
    duplicate.RemoveRange(5, delete);

    duplicate.Add(check);

    duplicate.AddRange(final);
}


This code is in a class and I'm using Singleton. I am still new to this. I was stuck in the code

string check = textbox1.Text;

It shows that textbox1 doesn't exists in the current context.

Help needed.

Thanks in advance.
Posted
Comments
Shai Vashdi 23-Apr-14 3:31am    
Can you write down all of the code and not just a segment?
Steve Van Lint 23-Apr-14 3:59am    
I'm assuming that your textbox is located on a form and that you are instantiating this code via your singleton?

The error you are receiving is because that class cannot access the textbox on your form.

What are you trying to accomplish here?
Sunasara Imdadhusen 23-Apr-14 6:50am    
Can you please provide entire function code over here!!
Alif Marz 24-Apr-14 0:16am    
This is my code in Singleton

namespace WpfCloud
{
public class WordCloudHelper
{
private static WordCloudHelper _instance;

ServiceSoapClient _service; //for cloud services

protected WordCloudHelper()
{
_words = new List<string>();
}

public static WordCloudHelper Instance
{
get
{
if (_instance == null)
{
_instance = new WordCloudHelper();
}
return _instance;
}
}

private List<string> _words;
private String _text;

public void EnterWordsInCloud(String text)
{
_service = new iDigitalService.ServiceSoapClient();

WordCloud cloud = _service.getLatestWordCloud();
text = _text.ToUpper();

int identity = cloud.cloudID;
_service.addWordToCloud(identity, text);
}

public List<string> GetWordCloud(String text)
{
if (text.Length > 0)
{
_words.Clear();

WordCloud cloud = _service.getLatestWordCloud();
PlayWord[] words = cloud.words;

Dictionary<string, int=""> arrange = new Dictionary<string, int="">();

foreach (PlayWord word in words)
{
text = word.word;
int number = word.count;

arrange.Add(text, number);
arrange.Remove("BITCH"); //profanity list

if (!arrange.ContainsKey(text))
{
arrange.Add(text, 0);
}

else if (text.Length > 11)
{
arrange.Remove(text);
}

else
{
int count = arrange[text];
arrange[text] = count + 1;
}
}

var sortDict = from entry in arrange orderby entry.Value descending select entry;

//var result = arrange.Select(element => element.Value > 50);

//arrange = arrange.OrderBy(k => k.Key).ToDictionary(k => k.Key, k => k.Value);

List<string> final = new List<string>();
foreach (KeyValuePair<string, int=""> entry in sortDict)
final.Add(entry.Key);
final.RemoveAt(2);

List<string> duplicate = final.ToList();

string check = textbox1.Text;

if (duplicate.Contains(check) == true)
{
int remove = Math.Max(0, final.Count - 25);
final.RemoveRange(0, remove);

int delete = Math.Max(0, duplicate.Count - 5);
duplicate.RemoveRange(5, delete);

duplicate.Add(check);

duplicate.AddRange(final);
}

else
{
int remove = Math.Max(0, final.Count - 25);
final.RemoveRange(0, remove);

int delete = Math.Max(0, duplicate.Count - 5);
duplicate.RemoveRange(5, delete);

duplicate.AddRange(final);
}

_words = duplicate.Distinct().ToList();

}

else if (_words.Count == 0)
{
WordCloud cloud = _service.getLatestWordCloud();
string header = cloud.header;
PlayWord[] words = cloud.words;

Dictionary<string, int=""> d = new Dictionary<string, int="">();

foreach (PlayWord word in words)
{
text = word.word;
int number = word.count;

d.Add(text, number);
d.Remove("BITCH"); //profanity

1 solution

As the error message tells you textbox1 is not accessible from where you want to access it. It is neither a field or property of the containing class WordCloudHelper, nor a parameter to the method GetWordCloud.

I think you have to pass in check as String parameter (assigned from textbox1.text somewhere where textbox1.text is accessible).
 
Share this answer
 
Comments
Alif Marz 25-Apr-14 1:37am    
Thank you for the solution. I am still new to WPF.

Can you guide me through to do that step?

I found this link below but how to check for string?

Link -> http://stackoverflow.com/questions/21607925/c-sharp-return-variable-from-child-window-to-parent-window-in-wpf

Thanks in advance.

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