Problem getting a string from a Combobox into a class variable [C#]












0















The button function, should take whatever text is in the combo box and place it within sleeper.traintype



 private void Btn_Apply_Click(object sender, RoutedEventArgs e)
{
try
{
sleeper.trainType = CmbBox_TrainType.Text;
if (CmbBox_TrainType.Text == "Sleeper")
{
// instantiate the sleeper train
sleeper.trainType = CmbBox_TrainType.Text;
}
}


My sleeper train class (inheriting from overall train class)



public class Sleeper : Train
{
private string _intermediate, _intermediate1, _intermediate2, _intermediate3;
private bool _cabin;
public string intermediate
{
get
{
return _intermediate;
}
set
{
_intermediate = value;
}
}

public string intermediate1
{
get
{
return _intermediate1;
}
set
{
_intermediate1 = value;
}
}

public string intermediate2
{
get
{
return _intermediate2;
}
set
{
_intermediate2 = value;
}
}

public string intermediate3
{
get
{
return _intermediate3;
}
set
{
_intermediate3 = value;
}
}


The train class:



 public class Train
{
private string _trainID, _departureDay, _departureStation, _destinationStation, _departureTime, _trainType;
private bool _firstClass;

public string timePunctuation = ":";
public string dayPunctuation = "/";

public string trainID
{
get
{
return _trainID;
}
set
{
// check if the vlaue has letters & numbers and that the length is correct
if(value.Length == 4 && Regex.IsMatch(value, "[A-Z][0-9]"))
{
_trainID = value;
}
else
{
throw new FormatException("That train ID is not valid! (Example: AA11)");
}
}
}

public string departureDay
{
get
{
return _departureDay;
}
set
{
if(value.Length == 0)
{
throw new FormatException("You need to choose a departure day!");
} else
{
_departureDay = value;
}
}
}

public string departureTime
{
get
{
return _departureTime;
}
set
{
if(value.Length != 5 || value.Contains(timePunctuation) == false)
{
throw new FormatException("The time must be in this format: (11:11 or 03:22)");
} else
{
_departureTime = value;
}
}
}

public string departureStation
{
get
{
return _departureStation;
}
set
{
if(value.Length == 0)
{
throw new FormatException("You must enter a departure station!");
} else
{
_departureStation = value;
}
}
}

public string destinationStation
{
get
{
return _destinationStation;
}
set
{
if(value.Length == 0)
{
throw new FormatException("You must enter a destination!");
} else
{
_departureStation = value;
}
}
}

public string trainType
{
get
{
return _trainType;
}
set
{
value = _trainType;
}
}
}


I'm using a combobox with three options "Sleeper", "Stopping" and "Express". When using breakpoints next to sleeper.trainType = CmbBox_TrainType.Text; it creates my class but states that my sleeper.trainType variable is null. But it says that




CmbBox_TrainType = "Sleeper"




Instantiate sleeper at the start with Sleeper sleeper = new Sleeper();
but have also tried to put it in the if and just before sleeper.trainType = CmbBox_TrainType.Text;










share|improve this question




















  • 3





    WPF/UWP and XAML were designed with the MVVM pattern in mind. While you can use other approaches, doing so misses about 90% of it's pwoer and runs into issues at any other point. This does not look like the MVVM pattern. With it, this is just a two way binding. Maybe with a converter and/or operation between ViewModel and Model.

    – Christopher
    Nov 19 '18 at 16:28













  • @Christopher thanks for the reply, I basically just copied this code from an old project I had, and it worked fine with exactly the same setup any glaring issues with it?

    – Tomhass
    Nov 19 '18 at 16:33








  • 1





    Please reduce the code in your question to a minimal example that demonstrates/exhibits the issue you are asking about. What have _intermediate1, _intermediate2, ..., departure day and station, etc... to do with your problem?

    – elgonzo
    Nov 19 '18 at 16:53








  • 2





    Also, what do you mean with "When using breakpoints next to sleeper.trainType = CmbBox_TrainType.Text"? Is the breakpoint above, below, or on the line? If you put the breakpoint above or on the line itself, and the breakpoint is being hit, then the line is not yet been executed -- in other words, sleeper.trainType would still have its initial value (null?). Step over this line in the debugger to see the assignment in this line changing the value of sleeper.trainType.

    – elgonzo
    Nov 19 '18 at 16:58













  • Check also that you have instantiated your sleeper object at the point where you assign to its 'traintype' attribute. You can't assign a value to an attribute of an object you have just declared and not instantiated.

    – heap1
    Nov 19 '18 at 17:12
















0















The button function, should take whatever text is in the combo box and place it within sleeper.traintype



 private void Btn_Apply_Click(object sender, RoutedEventArgs e)
{
try
{
sleeper.trainType = CmbBox_TrainType.Text;
if (CmbBox_TrainType.Text == "Sleeper")
{
// instantiate the sleeper train
sleeper.trainType = CmbBox_TrainType.Text;
}
}


My sleeper train class (inheriting from overall train class)



public class Sleeper : Train
{
private string _intermediate, _intermediate1, _intermediate2, _intermediate3;
private bool _cabin;
public string intermediate
{
get
{
return _intermediate;
}
set
{
_intermediate = value;
}
}

public string intermediate1
{
get
{
return _intermediate1;
}
set
{
_intermediate1 = value;
}
}

public string intermediate2
{
get
{
return _intermediate2;
}
set
{
_intermediate2 = value;
}
}

public string intermediate3
{
get
{
return _intermediate3;
}
set
{
_intermediate3 = value;
}
}


The train class:



 public class Train
{
private string _trainID, _departureDay, _departureStation, _destinationStation, _departureTime, _trainType;
private bool _firstClass;

public string timePunctuation = ":";
public string dayPunctuation = "/";

public string trainID
{
get
{
return _trainID;
}
set
{
// check if the vlaue has letters & numbers and that the length is correct
if(value.Length == 4 && Regex.IsMatch(value, "[A-Z][0-9]"))
{
_trainID = value;
}
else
{
throw new FormatException("That train ID is not valid! (Example: AA11)");
}
}
}

public string departureDay
{
get
{
return _departureDay;
}
set
{
if(value.Length == 0)
{
throw new FormatException("You need to choose a departure day!");
} else
{
_departureDay = value;
}
}
}

public string departureTime
{
get
{
return _departureTime;
}
set
{
if(value.Length != 5 || value.Contains(timePunctuation) == false)
{
throw new FormatException("The time must be in this format: (11:11 or 03:22)");
} else
{
_departureTime = value;
}
}
}

public string departureStation
{
get
{
return _departureStation;
}
set
{
if(value.Length == 0)
{
throw new FormatException("You must enter a departure station!");
} else
{
_departureStation = value;
}
}
}

public string destinationStation
{
get
{
return _destinationStation;
}
set
{
if(value.Length == 0)
{
throw new FormatException("You must enter a destination!");
} else
{
_departureStation = value;
}
}
}

public string trainType
{
get
{
return _trainType;
}
set
{
value = _trainType;
}
}
}


I'm using a combobox with three options "Sleeper", "Stopping" and "Express". When using breakpoints next to sleeper.trainType = CmbBox_TrainType.Text; it creates my class but states that my sleeper.trainType variable is null. But it says that




CmbBox_TrainType = "Sleeper"




Instantiate sleeper at the start with Sleeper sleeper = new Sleeper();
but have also tried to put it in the if and just before sleeper.trainType = CmbBox_TrainType.Text;










share|improve this question




















  • 3





    WPF/UWP and XAML were designed with the MVVM pattern in mind. While you can use other approaches, doing so misses about 90% of it's pwoer and runs into issues at any other point. This does not look like the MVVM pattern. With it, this is just a two way binding. Maybe with a converter and/or operation between ViewModel and Model.

    – Christopher
    Nov 19 '18 at 16:28













  • @Christopher thanks for the reply, I basically just copied this code from an old project I had, and it worked fine with exactly the same setup any glaring issues with it?

    – Tomhass
    Nov 19 '18 at 16:33








  • 1





    Please reduce the code in your question to a minimal example that demonstrates/exhibits the issue you are asking about. What have _intermediate1, _intermediate2, ..., departure day and station, etc... to do with your problem?

    – elgonzo
    Nov 19 '18 at 16:53








  • 2





    Also, what do you mean with "When using breakpoints next to sleeper.trainType = CmbBox_TrainType.Text"? Is the breakpoint above, below, or on the line? If you put the breakpoint above or on the line itself, and the breakpoint is being hit, then the line is not yet been executed -- in other words, sleeper.trainType would still have its initial value (null?). Step over this line in the debugger to see the assignment in this line changing the value of sleeper.trainType.

    – elgonzo
    Nov 19 '18 at 16:58













  • Check also that you have instantiated your sleeper object at the point where you assign to its 'traintype' attribute. You can't assign a value to an attribute of an object you have just declared and not instantiated.

    – heap1
    Nov 19 '18 at 17:12














0












0








0








The button function, should take whatever text is in the combo box and place it within sleeper.traintype



 private void Btn_Apply_Click(object sender, RoutedEventArgs e)
{
try
{
sleeper.trainType = CmbBox_TrainType.Text;
if (CmbBox_TrainType.Text == "Sleeper")
{
// instantiate the sleeper train
sleeper.trainType = CmbBox_TrainType.Text;
}
}


My sleeper train class (inheriting from overall train class)



public class Sleeper : Train
{
private string _intermediate, _intermediate1, _intermediate2, _intermediate3;
private bool _cabin;
public string intermediate
{
get
{
return _intermediate;
}
set
{
_intermediate = value;
}
}

public string intermediate1
{
get
{
return _intermediate1;
}
set
{
_intermediate1 = value;
}
}

public string intermediate2
{
get
{
return _intermediate2;
}
set
{
_intermediate2 = value;
}
}

public string intermediate3
{
get
{
return _intermediate3;
}
set
{
_intermediate3 = value;
}
}


The train class:



 public class Train
{
private string _trainID, _departureDay, _departureStation, _destinationStation, _departureTime, _trainType;
private bool _firstClass;

public string timePunctuation = ":";
public string dayPunctuation = "/";

public string trainID
{
get
{
return _trainID;
}
set
{
// check if the vlaue has letters & numbers and that the length is correct
if(value.Length == 4 && Regex.IsMatch(value, "[A-Z][0-9]"))
{
_trainID = value;
}
else
{
throw new FormatException("That train ID is not valid! (Example: AA11)");
}
}
}

public string departureDay
{
get
{
return _departureDay;
}
set
{
if(value.Length == 0)
{
throw new FormatException("You need to choose a departure day!");
} else
{
_departureDay = value;
}
}
}

public string departureTime
{
get
{
return _departureTime;
}
set
{
if(value.Length != 5 || value.Contains(timePunctuation) == false)
{
throw new FormatException("The time must be in this format: (11:11 or 03:22)");
} else
{
_departureTime = value;
}
}
}

public string departureStation
{
get
{
return _departureStation;
}
set
{
if(value.Length == 0)
{
throw new FormatException("You must enter a departure station!");
} else
{
_departureStation = value;
}
}
}

public string destinationStation
{
get
{
return _destinationStation;
}
set
{
if(value.Length == 0)
{
throw new FormatException("You must enter a destination!");
} else
{
_departureStation = value;
}
}
}

public string trainType
{
get
{
return _trainType;
}
set
{
value = _trainType;
}
}
}


I'm using a combobox with three options "Sleeper", "Stopping" and "Express". When using breakpoints next to sleeper.trainType = CmbBox_TrainType.Text; it creates my class but states that my sleeper.trainType variable is null. But it says that




CmbBox_TrainType = "Sleeper"




Instantiate sleeper at the start with Sleeper sleeper = new Sleeper();
but have also tried to put it in the if and just before sleeper.trainType = CmbBox_TrainType.Text;










share|improve this question
















The button function, should take whatever text is in the combo box and place it within sleeper.traintype



 private void Btn_Apply_Click(object sender, RoutedEventArgs e)
{
try
{
sleeper.trainType = CmbBox_TrainType.Text;
if (CmbBox_TrainType.Text == "Sleeper")
{
// instantiate the sleeper train
sleeper.trainType = CmbBox_TrainType.Text;
}
}


My sleeper train class (inheriting from overall train class)



public class Sleeper : Train
{
private string _intermediate, _intermediate1, _intermediate2, _intermediate3;
private bool _cabin;
public string intermediate
{
get
{
return _intermediate;
}
set
{
_intermediate = value;
}
}

public string intermediate1
{
get
{
return _intermediate1;
}
set
{
_intermediate1 = value;
}
}

public string intermediate2
{
get
{
return _intermediate2;
}
set
{
_intermediate2 = value;
}
}

public string intermediate3
{
get
{
return _intermediate3;
}
set
{
_intermediate3 = value;
}
}


The train class:



 public class Train
{
private string _trainID, _departureDay, _departureStation, _destinationStation, _departureTime, _trainType;
private bool _firstClass;

public string timePunctuation = ":";
public string dayPunctuation = "/";

public string trainID
{
get
{
return _trainID;
}
set
{
// check if the vlaue has letters & numbers and that the length is correct
if(value.Length == 4 && Regex.IsMatch(value, "[A-Z][0-9]"))
{
_trainID = value;
}
else
{
throw new FormatException("That train ID is not valid! (Example: AA11)");
}
}
}

public string departureDay
{
get
{
return _departureDay;
}
set
{
if(value.Length == 0)
{
throw new FormatException("You need to choose a departure day!");
} else
{
_departureDay = value;
}
}
}

public string departureTime
{
get
{
return _departureTime;
}
set
{
if(value.Length != 5 || value.Contains(timePunctuation) == false)
{
throw new FormatException("The time must be in this format: (11:11 or 03:22)");
} else
{
_departureTime = value;
}
}
}

public string departureStation
{
get
{
return _departureStation;
}
set
{
if(value.Length == 0)
{
throw new FormatException("You must enter a departure station!");
} else
{
_departureStation = value;
}
}
}

public string destinationStation
{
get
{
return _destinationStation;
}
set
{
if(value.Length == 0)
{
throw new FormatException("You must enter a destination!");
} else
{
_departureStation = value;
}
}
}

public string trainType
{
get
{
return _trainType;
}
set
{
value = _trainType;
}
}
}


I'm using a combobox with three options "Sleeper", "Stopping" and "Express". When using breakpoints next to sleeper.trainType = CmbBox_TrainType.Text; it creates my class but states that my sleeper.trainType variable is null. But it says that




CmbBox_TrainType = "Sleeper"




Instantiate sleeper at the start with Sleeper sleeper = new Sleeper();
but have also tried to put it in the if and just before sleeper.trainType = CmbBox_TrainType.Text;







c# wpf






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 16:32









Dragonthoughts

1,63741016




1,63741016










asked Nov 19 '18 at 16:26









TomhassTomhass

33




33








  • 3





    WPF/UWP and XAML were designed with the MVVM pattern in mind. While you can use other approaches, doing so misses about 90% of it's pwoer and runs into issues at any other point. This does not look like the MVVM pattern. With it, this is just a two way binding. Maybe with a converter and/or operation between ViewModel and Model.

    – Christopher
    Nov 19 '18 at 16:28













  • @Christopher thanks for the reply, I basically just copied this code from an old project I had, and it worked fine with exactly the same setup any glaring issues with it?

    – Tomhass
    Nov 19 '18 at 16:33








  • 1





    Please reduce the code in your question to a minimal example that demonstrates/exhibits the issue you are asking about. What have _intermediate1, _intermediate2, ..., departure day and station, etc... to do with your problem?

    – elgonzo
    Nov 19 '18 at 16:53








  • 2





    Also, what do you mean with "When using breakpoints next to sleeper.trainType = CmbBox_TrainType.Text"? Is the breakpoint above, below, or on the line? If you put the breakpoint above or on the line itself, and the breakpoint is being hit, then the line is not yet been executed -- in other words, sleeper.trainType would still have its initial value (null?). Step over this line in the debugger to see the assignment in this line changing the value of sleeper.trainType.

    – elgonzo
    Nov 19 '18 at 16:58













  • Check also that you have instantiated your sleeper object at the point where you assign to its 'traintype' attribute. You can't assign a value to an attribute of an object you have just declared and not instantiated.

    – heap1
    Nov 19 '18 at 17:12














  • 3





    WPF/UWP and XAML were designed with the MVVM pattern in mind. While you can use other approaches, doing so misses about 90% of it's pwoer and runs into issues at any other point. This does not look like the MVVM pattern. With it, this is just a two way binding. Maybe with a converter and/or operation between ViewModel and Model.

    – Christopher
    Nov 19 '18 at 16:28













  • @Christopher thanks for the reply, I basically just copied this code from an old project I had, and it worked fine with exactly the same setup any glaring issues with it?

    – Tomhass
    Nov 19 '18 at 16:33








  • 1





    Please reduce the code in your question to a minimal example that demonstrates/exhibits the issue you are asking about. What have _intermediate1, _intermediate2, ..., departure day and station, etc... to do with your problem?

    – elgonzo
    Nov 19 '18 at 16:53








  • 2





    Also, what do you mean with "When using breakpoints next to sleeper.trainType = CmbBox_TrainType.Text"? Is the breakpoint above, below, or on the line? If you put the breakpoint above or on the line itself, and the breakpoint is being hit, then the line is not yet been executed -- in other words, sleeper.trainType would still have its initial value (null?). Step over this line in the debugger to see the assignment in this line changing the value of sleeper.trainType.

    – elgonzo
    Nov 19 '18 at 16:58













  • Check also that you have instantiated your sleeper object at the point where you assign to its 'traintype' attribute. You can't assign a value to an attribute of an object you have just declared and not instantiated.

    – heap1
    Nov 19 '18 at 17:12








3




3





WPF/UWP and XAML were designed with the MVVM pattern in mind. While you can use other approaches, doing so misses about 90% of it's pwoer and runs into issues at any other point. This does not look like the MVVM pattern. With it, this is just a two way binding. Maybe with a converter and/or operation between ViewModel and Model.

– Christopher
Nov 19 '18 at 16:28







WPF/UWP and XAML were designed with the MVVM pattern in mind. While you can use other approaches, doing so misses about 90% of it's pwoer and runs into issues at any other point. This does not look like the MVVM pattern. With it, this is just a two way binding. Maybe with a converter and/or operation between ViewModel and Model.

– Christopher
Nov 19 '18 at 16:28















@Christopher thanks for the reply, I basically just copied this code from an old project I had, and it worked fine with exactly the same setup any glaring issues with it?

– Tomhass
Nov 19 '18 at 16:33







@Christopher thanks for the reply, I basically just copied this code from an old project I had, and it worked fine with exactly the same setup any glaring issues with it?

– Tomhass
Nov 19 '18 at 16:33






1




1





Please reduce the code in your question to a minimal example that demonstrates/exhibits the issue you are asking about. What have _intermediate1, _intermediate2, ..., departure day and station, etc... to do with your problem?

– elgonzo
Nov 19 '18 at 16:53







Please reduce the code in your question to a minimal example that demonstrates/exhibits the issue you are asking about. What have _intermediate1, _intermediate2, ..., departure day and station, etc... to do with your problem?

– elgonzo
Nov 19 '18 at 16:53






2




2





Also, what do you mean with "When using breakpoints next to sleeper.trainType = CmbBox_TrainType.Text"? Is the breakpoint above, below, or on the line? If you put the breakpoint above or on the line itself, and the breakpoint is being hit, then the line is not yet been executed -- in other words, sleeper.trainType would still have its initial value (null?). Step over this line in the debugger to see the assignment in this line changing the value of sleeper.trainType.

– elgonzo
Nov 19 '18 at 16:58







Also, what do you mean with "When using breakpoints next to sleeper.trainType = CmbBox_TrainType.Text"? Is the breakpoint above, below, or on the line? If you put the breakpoint above or on the line itself, and the breakpoint is being hit, then the line is not yet been executed -- in other words, sleeper.trainType would still have its initial value (null?). Step over this line in the debugger to see the assignment in this line changing the value of sleeper.trainType.

– elgonzo
Nov 19 '18 at 16:58















Check also that you have instantiated your sleeper object at the point where you assign to its 'traintype' attribute. You can't assign a value to an attribute of an object you have just declared and not instantiated.

– heap1
Nov 19 '18 at 17:12





Check also that you have instantiated your sleeper object at the point where you assign to its 'traintype' attribute. You can't assign a value to an attribute of an object you have just declared and not instantiated.

– heap1
Nov 19 '18 at 17:12












0






active

oldest

votes











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%2f53378892%2fproblem-getting-a-string-from-a-combobox-into-a-class-variable-c%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















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%2f53378892%2fproblem-getting-a-string-from-a-combobox-into-a-class-variable-c%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

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)