uwp c#, is it possible to refer single variable for different types of control?












2















var _textbox;
if (datatype == "STRING") {
_textbox = new TextBox();
_textbox.Text = "text";
}
else if (dataype == "ACTOR") {
_textbox = new AutoSuggestBox();
}

_textbox.Tag = "custom tag name";
grid.Children.Add(_textbox);
Grid.SetRow(_textbox, row);


I don't want to repeat same code for all control types (setting tag and appending child to grid).
Is there a way for that?










share|improve this question

























  • What's AutoSuggestionBox class?

    – D-Shih
    Nov 26 '18 at 3:35











  • It would be great if you used real code. There's no .text property, for example.

    – Enigmativity
    Nov 26 '18 at 3:43











  • Sorry, updated the code

    – SHASHI KUMAR S
    Nov 26 '18 at 4:02











  • @SHASHIKUMARS Ok I wrote an answer :)

    – D-Shih
    Nov 26 '18 at 5:00
















2















var _textbox;
if (datatype == "STRING") {
_textbox = new TextBox();
_textbox.Text = "text";
}
else if (dataype == "ACTOR") {
_textbox = new AutoSuggestBox();
}

_textbox.Tag = "custom tag name";
grid.Children.Add(_textbox);
Grid.SetRow(_textbox, row);


I don't want to repeat same code for all control types (setting tag and appending child to grid).
Is there a way for that?










share|improve this question

























  • What's AutoSuggestionBox class?

    – D-Shih
    Nov 26 '18 at 3:35











  • It would be great if you used real code. There's no .text property, for example.

    – Enigmativity
    Nov 26 '18 at 3:43











  • Sorry, updated the code

    – SHASHI KUMAR S
    Nov 26 '18 at 4:02











  • @SHASHIKUMARS Ok I wrote an answer :)

    – D-Shih
    Nov 26 '18 at 5:00














2












2








2








var _textbox;
if (datatype == "STRING") {
_textbox = new TextBox();
_textbox.Text = "text";
}
else if (dataype == "ACTOR") {
_textbox = new AutoSuggestBox();
}

_textbox.Tag = "custom tag name";
grid.Children.Add(_textbox);
Grid.SetRow(_textbox, row);


I don't want to repeat same code for all control types (setting tag and appending child to grid).
Is there a way for that?










share|improve this question
















var _textbox;
if (datatype == "STRING") {
_textbox = new TextBox();
_textbox.Text = "text";
}
else if (dataype == "ACTOR") {
_textbox = new AutoSuggestBox();
}

_textbox.Tag = "custom tag name";
grid.Children.Add(_textbox);
Grid.SetRow(_textbox, row);


I don't want to repeat same code for all control types (setting tag and appending child to grid).
Is there a way for that?







c# uwp






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 3:59







SHASHI KUMAR S

















asked Nov 26 '18 at 3:30









SHASHI KUMAR SSHASHI KUMAR S

8911




8911













  • What's AutoSuggestionBox class?

    – D-Shih
    Nov 26 '18 at 3:35











  • It would be great if you used real code. There's no .text property, for example.

    – Enigmativity
    Nov 26 '18 at 3:43











  • Sorry, updated the code

    – SHASHI KUMAR S
    Nov 26 '18 at 4:02











  • @SHASHIKUMARS Ok I wrote an answer :)

    – D-Shih
    Nov 26 '18 at 5:00



















  • What's AutoSuggestionBox class?

    – D-Shih
    Nov 26 '18 at 3:35











  • It would be great if you used real code. There's no .text property, for example.

    – Enigmativity
    Nov 26 '18 at 3:43











  • Sorry, updated the code

    – SHASHI KUMAR S
    Nov 26 '18 at 4:02











  • @SHASHIKUMARS Ok I wrote an answer :)

    – D-Shih
    Nov 26 '18 at 5:00

















What's AutoSuggestionBox class?

– D-Shih
Nov 26 '18 at 3:35





What's AutoSuggestionBox class?

– D-Shih
Nov 26 '18 at 3:35













It would be great if you used real code. There's no .text property, for example.

– Enigmativity
Nov 26 '18 at 3:43





It would be great if you used real code. There's no .text property, for example.

– Enigmativity
Nov 26 '18 at 3:43













Sorry, updated the code

– SHASHI KUMAR S
Nov 26 '18 at 4:02





Sorry, updated the code

– SHASHI KUMAR S
Nov 26 '18 at 4:02













@SHASHIKUMARS Ok I wrote an answer :)

– D-Shih
Nov 26 '18 at 5:00





@SHASHIKUMARS Ok I wrote an answer :)

– D-Shih
Nov 26 '18 at 5:00












2 Answers
2






active

oldest

votes


















1














You can try to use the extract method to avoid duplicate code.



If you use children it might be UIElementCollection which add method need to pass System.Windows.UIElement class.



Control _textbox = GetTextBox();
_textbox.Tag = "custom tag name";

grid.Children.Add(_textbox);
Grid.SetRow(_textbox, row);


public Control GetControl(){
Control _textbox;
if (condition1) {
_textbox = GetTextBox();
}
else if (condition2) {
_textbox = new AutoSuggestBox();
}
return _textbox;
}

public TextBox GetTextBox(){
TextBox _textbox = new TextBox();
_textbox.Text = "text";
return _textbox;
}


Or you can try to use I saw you edit your question, you can try to use Dictionary be a mapper table then use TryGetValue to make it.



Dictionary<string, Control> dict = new Dictionary<string, Control>();
dict.Add("STRING", new TextBox() { Text = "text" });
dict.Add("ACTOR", new AutoSuggestBox());

Control _textbox;
if (dict.TryGetValue(datatype, out _textbox))
{
_textbox.Tag = "custom tag name";
grid.Children.Add(_textbox);
Grid.SetRow(_textbox, row);
}





share|improve this answer

































    0














    Yes, just do this:



    Control _textbox = null;
    if (datatype == "STRING")
    {
    _textbox = new TextBox();
    _textbox.Text = "text";
    }
    else if (dataype == "ACTOR")
    {
    _textbox = new AutoSuggestBox();
    }

    if (_textbox != null)
    {
    _textbox.Tag = "custom tag name";
    grid.Children.Add(_textbox);
    Grid.SetRow(_textbox, row);
    }





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53474444%2fuwp-c-is-it-possible-to-refer-single-variable-for-different-types-of-control%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      You can try to use the extract method to avoid duplicate code.



      If you use children it might be UIElementCollection which add method need to pass System.Windows.UIElement class.



      Control _textbox = GetTextBox();
      _textbox.Tag = "custom tag name";

      grid.Children.Add(_textbox);
      Grid.SetRow(_textbox, row);


      public Control GetControl(){
      Control _textbox;
      if (condition1) {
      _textbox = GetTextBox();
      }
      else if (condition2) {
      _textbox = new AutoSuggestBox();
      }
      return _textbox;
      }

      public TextBox GetTextBox(){
      TextBox _textbox = new TextBox();
      _textbox.Text = "text";
      return _textbox;
      }


      Or you can try to use I saw you edit your question, you can try to use Dictionary be a mapper table then use TryGetValue to make it.



      Dictionary<string, Control> dict = new Dictionary<string, Control>();
      dict.Add("STRING", new TextBox() { Text = "text" });
      dict.Add("ACTOR", new AutoSuggestBox());

      Control _textbox;
      if (dict.TryGetValue(datatype, out _textbox))
      {
      _textbox.Tag = "custom tag name";
      grid.Children.Add(_textbox);
      Grid.SetRow(_textbox, row);
      }





      share|improve this answer






























        1














        You can try to use the extract method to avoid duplicate code.



        If you use children it might be UIElementCollection which add method need to pass System.Windows.UIElement class.



        Control _textbox = GetTextBox();
        _textbox.Tag = "custom tag name";

        grid.Children.Add(_textbox);
        Grid.SetRow(_textbox, row);


        public Control GetControl(){
        Control _textbox;
        if (condition1) {
        _textbox = GetTextBox();
        }
        else if (condition2) {
        _textbox = new AutoSuggestBox();
        }
        return _textbox;
        }

        public TextBox GetTextBox(){
        TextBox _textbox = new TextBox();
        _textbox.Text = "text";
        return _textbox;
        }


        Or you can try to use I saw you edit your question, you can try to use Dictionary be a mapper table then use TryGetValue to make it.



        Dictionary<string, Control> dict = new Dictionary<string, Control>();
        dict.Add("STRING", new TextBox() { Text = "text" });
        dict.Add("ACTOR", new AutoSuggestBox());

        Control _textbox;
        if (dict.TryGetValue(datatype, out _textbox))
        {
        _textbox.Tag = "custom tag name";
        grid.Children.Add(_textbox);
        Grid.SetRow(_textbox, row);
        }





        share|improve this answer




























          1












          1








          1







          You can try to use the extract method to avoid duplicate code.



          If you use children it might be UIElementCollection which add method need to pass System.Windows.UIElement class.



          Control _textbox = GetTextBox();
          _textbox.Tag = "custom tag name";

          grid.Children.Add(_textbox);
          Grid.SetRow(_textbox, row);


          public Control GetControl(){
          Control _textbox;
          if (condition1) {
          _textbox = GetTextBox();
          }
          else if (condition2) {
          _textbox = new AutoSuggestBox();
          }
          return _textbox;
          }

          public TextBox GetTextBox(){
          TextBox _textbox = new TextBox();
          _textbox.Text = "text";
          return _textbox;
          }


          Or you can try to use I saw you edit your question, you can try to use Dictionary be a mapper table then use TryGetValue to make it.



          Dictionary<string, Control> dict = new Dictionary<string, Control>();
          dict.Add("STRING", new TextBox() { Text = "text" });
          dict.Add("ACTOR", new AutoSuggestBox());

          Control _textbox;
          if (dict.TryGetValue(datatype, out _textbox))
          {
          _textbox.Tag = "custom tag name";
          grid.Children.Add(_textbox);
          Grid.SetRow(_textbox, row);
          }





          share|improve this answer















          You can try to use the extract method to avoid duplicate code.



          If you use children it might be UIElementCollection which add method need to pass System.Windows.UIElement class.



          Control _textbox = GetTextBox();
          _textbox.Tag = "custom tag name";

          grid.Children.Add(_textbox);
          Grid.SetRow(_textbox, row);


          public Control GetControl(){
          Control _textbox;
          if (condition1) {
          _textbox = GetTextBox();
          }
          else if (condition2) {
          _textbox = new AutoSuggestBox();
          }
          return _textbox;
          }

          public TextBox GetTextBox(){
          TextBox _textbox = new TextBox();
          _textbox.Text = "text";
          return _textbox;
          }


          Or you can try to use I saw you edit your question, you can try to use Dictionary be a mapper table then use TryGetValue to make it.



          Dictionary<string, Control> dict = new Dictionary<string, Control>();
          dict.Add("STRING", new TextBox() { Text = "text" });
          dict.Add("ACTOR", new AutoSuggestBox());

          Control _textbox;
          if (dict.TryGetValue(datatype, out _textbox))
          {
          _textbox.Tag = "custom tag name";
          grid.Children.Add(_textbox);
          Grid.SetRow(_textbox, row);
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 26 '18 at 4:57

























          answered Nov 26 '18 at 3:41









          D-ShihD-Shih

          25.8k61531




          25.8k61531

























              0














              Yes, just do this:



              Control _textbox = null;
              if (datatype == "STRING")
              {
              _textbox = new TextBox();
              _textbox.Text = "text";
              }
              else if (dataype == "ACTOR")
              {
              _textbox = new AutoSuggestBox();
              }

              if (_textbox != null)
              {
              _textbox.Tag = "custom tag name";
              grid.Children.Add(_textbox);
              Grid.SetRow(_textbox, row);
              }





              share|improve this answer




























                0














                Yes, just do this:



                Control _textbox = null;
                if (datatype == "STRING")
                {
                _textbox = new TextBox();
                _textbox.Text = "text";
                }
                else if (dataype == "ACTOR")
                {
                _textbox = new AutoSuggestBox();
                }

                if (_textbox != null)
                {
                _textbox.Tag = "custom tag name";
                grid.Children.Add(_textbox);
                Grid.SetRow(_textbox, row);
                }





                share|improve this answer


























                  0












                  0








                  0







                  Yes, just do this:



                  Control _textbox = null;
                  if (datatype == "STRING")
                  {
                  _textbox = new TextBox();
                  _textbox.Text = "text";
                  }
                  else if (dataype == "ACTOR")
                  {
                  _textbox = new AutoSuggestBox();
                  }

                  if (_textbox != null)
                  {
                  _textbox.Tag = "custom tag name";
                  grid.Children.Add(_textbox);
                  Grid.SetRow(_textbox, row);
                  }





                  share|improve this answer













                  Yes, just do this:



                  Control _textbox = null;
                  if (datatype == "STRING")
                  {
                  _textbox = new TextBox();
                  _textbox.Text = "text";
                  }
                  else if (dataype == "ACTOR")
                  {
                  _textbox = new AutoSuggestBox();
                  }

                  if (_textbox != null)
                  {
                  _textbox.Tag = "custom tag name";
                  grid.Children.Add(_textbox);
                  Grid.SetRow(_textbox, row);
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 26 '18 at 4:23









                  EnigmativityEnigmativity

                  75.8k865132




                  75.8k865132






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53474444%2fuwp-c-is-it-possible-to-refer-single-variable-for-different-types-of-control%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Contact image not getting when fetch all contact list from iPhone by CNContact

                      count number of partitions of a set with n elements into k subsets

                      A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks