Tatiana Gancheva
Tatiana Gancheva

Reputation: 33

dictionaryApp,MVVM,WPF,reading from textFile

i am building a dictionary WPF application where you write a word and than the application tells u in what language the word is and also gives you a description of the word. Using MVVM. Here is how it looks:

enter image description here

I have problem with finding out how to get the language and the description of word from a text file and put them in the text box, i dont mean the binding, but the exact method of getting the info. Here is my Model:

using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;

namespace dictionary
{
    public class dictionaryModel
    {
        private string word;
        private string language;
        private string description;

        public string Word
        {
            get
            {
                return word;
            }
            set
            {
                word = value;
            }
        }

            public string Language
        {
            get
            {
                return language;
            }
            set
            {
                language = value;
            }

        }

            public string Description
        {
            get
            {
                return description;
            }
            set
            {
                description = value;
            }

        }

        public dictionaryModel(string describedWord, string WordLanguage, string WordDescription) 
        {
            this.word = describedWord;
            this.language = WordLanguage;
            this.description = WordDescription;
        }

        public dictionaryModel() 
        { }

    }







and here is my View Model, so far:
namespace dictionary
{
    using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Markup;



    public class dictionaryViewModel : INotifyPropertyChanged
    {
        private ICommand _getAnswer;
        private dictionaryModel m;
        private string w;
        private string retLanguage;
        private string retDescription;
        private bool _canExecute;

        public dictionaryViewModel()
        {
            _canExecute = true;
        }


        public string retLang
        {
            get
            {
                return retLanguage;
            }
            set
            {
                retLanguage = value;
                NotifyPropertyChanged();
            }

        }

        public string retDescr
        {
            get
            {
                return retDescription;
            }
            set
            {
                retDescription = value;
                NotifyPropertyChanged();

            }
        }

        public string word
        {
            get
            {
                return w;
            }
            set
            {
                w = value;
                NotifyPropertyChanged();
            }
        }

        public dictionaryModel model
        {
            get
            {
                return m;
            }
            set
            {
                m = value;
                NotifyPropertyChanged();
            }
        }

        public ICommand getAnswer
        {
            get
            {
                return _getAnswer ?? (_getAnswer = new RelayCommand(() => getWholeAnswer(word), _canExecute));
            }
        }

        public dictionaryViewModel(dictionaryModel model, string word, string retLang, string retDescr,
            ICommand getAnswer)
        {
            m = model;
            w = word;
            retLang = retLanguage;
            retDescr = retDescription;
            _getAnswer = getAnswer;

        }


        public ObservableCollection<dictionaryModel> readTxtFile()
        {
            ObservableCollection<dictionaryModel> dictObj = new ObservableCollection<dictionaryModel>();
            string word;
            string language;
            string description;

            var file = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "dictionary.txt");
            StreamReader read = new StreamReader(file);
            string line;

            string[] item;
            while((line=read.ReadLine())!=null)
            {
                item = line.Split(';');
                word = item[0];
                language = item[1];
                description = item[2];
                dictionaryModel object1 = new dictionaryModel(word, language, description);

                dictObj.Add(object1);

            }
            read.Close();
            return dictObj;
        }

        public void getWholeAnswer(string w)
        {

            string descr, lang;
            dictionaryModel obj = null;
            ObservableCollection<dictionaryModel> rdF = readTxtFile();
            try
            {
                foreach(dictionaryModel a in rdF)
                {
                    if(w.Equals(a))
                    {
                        descr = retDescr;
                        lang = retLang;
                        obj = a;
                    }
                    obj.Language = retLang;
                    obj.Description = retDescription;
                }
            }catch(Exception e)
            {
                ExceptionInfo();
            }

        }

        private void ExceptionInfo()
        {
            throw new NotImplementedException();
        }

        private void NotifyPropertyChanged()
        {
            throw new NotImplementedException();
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}

The method is getWholeAnswer(). You see what i have tried, but no success. If you have any ideas, please help me out...

Upvotes: 0

Views: 292

Answers (2)

Tatiana Gancheva
Tatiana Gancheva

Reputation: 33

i found my mistake, i am posting the solution of the method, it could be helpful to someone:

public void getWholeAnswer(string w) {

        dictionaryModel obj = null;
        ObservableCollection<dictionaryModel> rdF = readTxtFile();
        bool find = false;
        try
        {
            foreach(dictionaryModel a in rdF)
            {
                if(w.Equals(a.Word))
                {

                    obj = a;
                    retLang = a.Language;
                    retDescr = a.Description;
                    find = true;
                    break;
                }
             }
            if(false == find)
            {
                AskTheQuestion();
            }
        }catch(Exception e)
        {
            AskTheQuestion();
        }

    }

Upvotes: 0

Francesco Bonizzi
Francesco Bonizzi

Reputation: 5302

I think the problem is here:

if(w.Equals(a))

w is a string, while a is dictionaryModel, you are comparing two different kind of types without defining an equality logic.

Maybe you would replace that line with this?

if(string.Compare(w.Trim(), a.word, true) == 0)

Upvotes: 1

Related Questions