uwp c#, is it possible to refer single variable for different types of control?
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
add a comment |
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
What'sAutoSuggestionBox
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
add a comment |
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
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
c# uwp
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'sAutoSuggestionBox
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
add a comment |
What'sAutoSuggestionBox
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
add a comment |
2 Answers
2
active
oldest
votes
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);
}
add a comment |
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);
}
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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);
}
add a comment |
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);
}
add a comment |
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);
}
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);
}
edited Nov 26 '18 at 4:57
answered Nov 26 '18 at 3:41
D-ShihD-Shih
25.8k61531
25.8k61531
add a comment |
add a comment |
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);
}
add a comment |
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);
}
add a comment |
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);
}
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);
}
answered Nov 26 '18 at 4:23
EnigmativityEnigmativity
75.8k865132
75.8k865132
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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