0

How do I require that all of the objects in a parameter array be passed as out? In my following code, I know that it's failing because my original parameters never get assigned and are thus null. Even though I would think they'd be passed as reference without having to explicitly say so, they don't seem to be. As a result, I've had to try and pass all of it as an out parameter.

Here's the parts of my code that I think matter.

ChatController(some elements removed)

class ChatController : ControllerBase
{
    #region Views

    ChatAreaView viewChatArea;
    UserListView viewUserArea;
    MessageView viewMessageArea;
    LoginPromptView viewLoginPrompt;

    #endregion

    #region ViewModels

    ChatAreaViewModel viewModelChatArea;
    UserAreaViewModel viewModelUserArea;
    MessageAreaViewModel viewModelMessageArea;
    LoginPromptViewModel viewModelLoginPrompt;

    #endregion
    public override void CreateViewsAndViewModels()
    {
        //InitializeViewAndViewModel(out viewChatArea, out viewModelChatArea);
        //InitializeViewAndViewModel(out viewMessageArea, out viewModelMessageArea);
        //InitializeViewAndViewModel(out viewUserArea, out viewModelUserArea);
        //InitializeViewAndViewModel(out viewLoginPrompt, out viewModelLoginPrompt);

        InitializeViewsAndViewModels(new FrameworkElement[] { viewChatArea, viewUserArea, viewMessageArea, viewLoginPrompt },
                                     new object[] { viewModelChatArea, viewModelUserArea, viewModelMessageArea, viewModelLoginPrompt });
    }
}

ControllerBase(some elements removed)

public abstract class ControllerBase : PrismBase
{
    public void InitializeViewAndViewModel<TView, TViewModel>(out TView view, out TViewModel viewModel)
        where TView : FrameworkElement, new()
        where TViewModel : new()
    {
        view = new TView();
        viewModel = new TViewModel();

        view.DataContext = viewModel;
    }

    public void InitializeViewsAndViewModels(FrameworkElement[] views, object[] viewModels)
    {
        if (views.Length != viewModels.Length)
            throw new ArgumentOutOfRangeException("views and viewModels must have the same number of elements.");

        for (int i = 0; i < views.Length -1 ; i++)
            InitializeViewAndViewModel(out views[i], out viewModels[i]);
    }
}

The problem area is here

public void InitializeViewsAndViewModels(FrameworkElement[] views, object[] viewModels)
{
}

All these objects here that I pass in to the method remain null when the method returns.

InitializeViewsAndViewModels(new FrameworkElement[] { viewChatArea, viewUserArea, viewMessageArea, viewLoginPrompt },
                             new object[] { viewModelChatArea, viewModelUserArea, viewModelMessageArea, viewModelLoginPrompt });

I want to require passing all of the views and viewModels array objects as out, but I can only seem to require the entire array itself be passed as out.

What can I do here?

EDIT: The entire problem seems to stem from the Plural form of the method, as I described above. If I got rid of that and I simply did this...

public override void CreateViewsAndViewModels()
{
    InitializeViewAndViewModel(out viewChatArea, out viewModelChatArea);
    InitializeViewAndViewModel(out viewMessageArea, out viewModelMessageArea);
    InitializeViewAndViewModel(out viewUserArea, out viewModelUserArea);
    InitializeViewAndViewModel(out viewLoginPrompt, out viewModelLoginPrompt);

    //InitializeViewsAndViewModels(new FrameworkElement[] { viewChatArea, viewUserArea, viewMessageArea, viewLoginPrompt },
    //                             new object[] { viewModelChatArea, viewModelUserArea, viewModelMessageArea, viewModelLoginPrompt });
}

everything works just swimmingly.

Cowman
  • 678
  • 7
  • 25

2 Answers2

2

You can't have arrays of out values in C#.

Now what you are trying to do is very strange and even if arrays of out would be supported you will still endup with trobles - you are loosing types of variable in you arrays, so there is no reasoanble way to new them up correctly.

Your current generic methods have multiple implementations in derived classes - one per pair of types (i.e. your ChatController have what you can view as 4 distinct implementation of InitializeViewAndViewModel each for specific pair of views). Your newly proposed code would have to find that missing type information somehow...

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • If I get rid of the InitializeViewsAndViewModels(plural) method, and instead call on the InitializeViewAndviewModel(singular) method for each pair of views and ViewModels like you see in the commented out region, everything works fine. The reason I want the plural form is so that I can reduce even further the amount of code in my Controller class so that it's a single line instead of x number for every pair of view/viewmodel I have. How can I accomplish that? The singular one works well enough, so why wouldn't the plural one? After all, it's just calling the singular method. – Cowman Sep 05 '12 at 05:26
  • @Cowman, I tried to explain why singular method works and can't be converted into single "plural" method you want, but I'm not good at that... so read on generics to see how it actually 4 logical methods in your current class. As for initialization - I think the easiest would be to use reflection to set all fields of particular base type (or with particular attributes). Side note that what you are trying to do may not have practical value in making things better/more usable (if indeed you are trying to implement MVC controller class). – Alexei Levenkov Sep 05 '12 at 16:55
  • I'm not fully clear on what you're saying... but I think I'm getting somewhat the gist of it. You're saying my idea of using generics to create and initialize my different views and viewmodels is a bad idea? I thought it was nice, because it helped me avoid a bunch of duplicated code that I had before in newing up and assigning the datacontext to the view. – Cowman Sep 05 '12 at 23:46
  • @Cowman, no, using generics is nice and efficient idea. It just does not translate well to case where you have no type information. What I don't see value in is initializing (or even storing) multiple ViewModel and Views on controller, but it may be perfectly valid for your particular case. – Alexei Levenkov Sep 05 '12 at 23:55
  • Well... I'm doing that because I don't know any better. I'm taking a MVVM approach, with controllers, that I learned from HiTechMagic on stackoverflow. Except... I don't know enough about it in execution. I figured my controller needed to know about all of the views and viewmodels... since it has the responsibility of creating them and assigning their properties. For thew view it assigns the datacontext(viewmodel) and for the viewmodel, it assigns all of the properties and commands. And over the course of execution it keeps updating the properties of the viewmodel. I just do not know better. – Cowman Sep 06 '12 at 00:07
  • You are in MVVM/WPF...need more coffee :) forget whatever I've said about view models - somehow I thought you are in ASP.Net MVC where it would look strange... – Alexei Levenkov Sep 06 '12 at 00:10
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/16338/discussion-between-cowman-and-alexei-levenkov) – Cowman Sep 06 '12 at 00:12
1

Use ref. See below:

class ChatController : ControllerBase
    {
        #region Views

        ChatAreaView viewChatArea;
        //UserListView viewUserArea;
        //MessageView viewMessageArea;
        //LoginPromptView viewLoginPrompt;

        #endregion

        #region ViewModels

        ChatAreaViewModel viewModelChatArea;
        //UserAreaViewModel viewModelUserArea;
        //MessageAreaViewModel viewModelMessageArea;
        //LoginPromptViewModel viewModelLoginPrompt;

        #endregion
        public void CreateViewsAndViewModels()
        {
            //InitializeViewAndViewModel(out viewChatArea, out viewModelChatArea);
            //InitializeViewAndViewModel(out viewMessageArea, out viewModelMessageArea);
            //InitializeViewAndViewModel(out viewUserArea, out viewModelUserArea);
            //InitializeViewAndViewModel(out viewLoginPrompt, out viewModelLoginPrompt);
            var a  = new FrameworkElement[] { viewChatArea };
            var b = new object[] { viewModelChatArea};
            InitializeViewsAndViewModels(a,
                                         b);
        }
    }

public abstract class ControllerBase 
    {
        public void InitializeViewAndViewModel<TView, TViewModel>(ref TView view, ref TViewModel viewModel)
            where TView : FrameworkElement, new()
            where TViewModel : new()
        {
            view = new TView();
            viewModel = new TViewModel();

            view.DataContext = viewModel;
        }

        public void InitializeViewsAndViewModels(FrameworkElement[] views, object[] viewModels)
        {
            if (views.Length != viewModels.Length)
                throw new ArgumentOutOfRangeException("views and viewModels must have the same number of elements.");

            for (int i = 0; i < views.Length; i++)
                InitializeViewAndViewModel(ref views[i], ref viewModels[i]);
        }
    }
ncfuncion
  • 227
  • 1
  • 3
  • 11
  • I tried this, but it has the same result. Within all the methods the view and ViewModel get assigned as their respective types. See http://i.imgur.com/nAie8.jpg When the method returns however... This happens http://i.imgur.com/5GdBd.jpg But wait... I think I see a problem. It's actually returning all of the values correctly except one of them... Let me see if I can figure out why. – Cowman Sep 05 '12 at 05:45
  • Turns out that was me not removing my views.Length - 1 to reflect yours... But still, the views and viewModels show correct, but the actual backing fields are still null... See the following http://i.imgur.com/WhF10.jpg So I would have to go back and manually assign all of my fields to the appropriate elements of the arrays. Why is that? Shouldn't it all be passed by reference? – Cowman Sep 05 '12 at 05:52