0

This may be a really basic question to experienced programmers but I started on VB6, and now I'm trying to accomplish same stuff on C# which is object oriented.

Suppose I have a class with a method to add two numbers on textboxes and I run that in the click handler of a button (Doesn't matter if it is static or not), then I have the result and I display it on the screen (maybe in another textbox), the user click another button, how do I recover the result on the other button handler?, what's the best practice?, I know I can read the result on the textbox, but if the result was displayed on a Messagebox.Show or in console?.

What's the best practice to save results instead of using helper textboxes or global variables?

On VB6 I use invisible textboxes, so my forms looks really messy, but thats the way to there.

Using C# + XAML + WPF

2
  • Not helping friend, what I really want is to know if there's a way to pass data from one scope to another like I see it there is only: 1) Global variables or 2)reading UI elements.... Thanks !
    – alexOtano
    Nov 16, 2015 at 21:22
  • Can you point me in the right direction?, here's what I said to @H.B.: I'm working with the MVVM but if I have let's say a DataTable bind DataGrid, my DataTable only exist in the scope where I generate it, so to manipulate the data in the DataGrid from other scope I need to use the DataGrid which is accessible from the other scope. then generate a DataTable, load the info from the DataGrid, manipulate the DT and then put all the changed data back into the Datagrid again. is there a way to keep that original DataTable? instead to have to load the info from the DataGrid?, how do you do that?
    – alexOtano
    Nov 17, 2015 at 0:21

1 Answer 1

1

Usually that is what a model is for. It is the data-state of what is shown (and more, as some information may not be displayed at all times or just used as utility). The view often has a reference to the model which you then can access in the handlers and manipulate.

I'd suggest reading up on design patterns like Model-View-Controller and for WPF specifially Model-View-ViewModel.

Also, WPF has a few powerful mechanics like data binding, which makes synchronizing your data with the view a lot easier, do not treat your view as a model.

4
  • I'm working with the MVVM but if I have let's say a DataTable bind DataGrid, my DataTable only exist in the scope where I generate it, so to manipulate the data in the DataGrid from other scope I need to use the DataGrid which is accessible from the other scope. then generate a DataTable, load the info from the DataGrid, manipulate the DataTable and then put all the changed data back into the Datagrid again. is there a way to keep that original DataTable? instead to have to load the info from the DataGrid?
    – alexOtano
    Nov 16, 2015 at 21:20
  • @OTANO: You use data binding to synchronize your data table with the DataGrid, then you just need to access the data table. You should never get data from UI elements directly. Also if the data table is not accessible you are doing something fundamentally wrong.
    – H.B.
    Nov 17, 2015 at 5:30
  • I think that my problem comes at the scope level, because I don't know how to retrive data from an object that was created in another scope, buttons handlers for example. if Ui elements and global variables are not the answer I need to look for something else, it will be very difficult to develop multiwindow apps fighting with this every time, I'll check to see if there's a way to keep the binding ...
    – alexOtano
    Nov 19, 2015 at 4:23
  • 1
    @OTANO: If you need a global model, then i usually create an instance variable on the App class. The current app can always be accessed via App.Current (returns an Application by default, so you need to cast it to your App class to access the property).
    – H.B.
    Nov 20, 2015 at 2:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.