0

How can i return a value from a a function in a Sealed Partial class?

I use the usercontrols like this. I have a usercontrol that calls another one that is a list. When i selected a row from this list, i call SelectionChanged="RadGrid1_SelectedIndexChanged" and it saves on a variable of type Templates the row i want to save. (Until here there is no problem)

When in the mainpage i try to access to that variable, it always returns me null. (Problem here)

UserControls:

<UserControl
x:Class="Stuff.Grouping.TableControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Stuff.Grouping"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">

<Grid>
    <SemanticZoom ScrollViewer.HorizontalScrollMode="Disabled" ScrollViewer.VerticalScrollMode="Disabled">
        <SemanticZoom.ZoomedInView>
            <local:GroupingZoomedInView Margin="0 0 30 50"/>
        </SemanticZoom.ZoomedInView>
        <SemanticZoom.ZoomedOutView>
            <GridView Margin="30 30 30 50">
                <GridView.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapGrid Orientation="Horizontal" MaximumRowsOrColumns="7"/>
                    </ItemsPanelTemplate>
                </GridView.ItemsPanel>
                <GridView.ItemTemplate>
                    <DataTemplate>
                        <Border Background="#FF3399FF" MinWidth="100" MinHeight="100">
                            <TextBlock Text="{Binding}" FontSize="60" Margin="10"/>
                        </Border>
                    </DataTemplate>
                </GridView.ItemTemplate>
            </GridView>
        </SemanticZoom.ZoomedOutView>
    </SemanticZoom>
</Grid>
</UserControl>

GroupingZoomedInView.xaml

<UserControl
x:Class="Stuff.Grouping.GroupingZoomedInView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Stuff.Grouping.Data"
xmlns:telerikGrid="using:Telerik.UI.Xaml.Controls.Grid"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">

<Grid>
    <Grid.Resources>
        <local:PeopleViewModel x:Key="Model"/>
    </Grid.Resources>
    <telerikGrid:RadDataGrid x:Name="dataGrid" ItemsSource="{Binding Data,Source={StaticResource Model}}" AutoGenerateColumns="False" FontSize="{StaticResource ControlContentThemeFontSize}" SelectionChanged="RadGrid1_SelectedIndexChanged">
        <telerikGrid:RadDataGrid.GroupDescriptors>
            <telerikGrid:DelegateGroupDescriptor>
                <telerikGrid:DelegateGroupDescriptor.KeyLookup>
                    <local:AlpabeticGroupKeyLookup/>
                </telerikGrid:DelegateGroupDescriptor.KeyLookup>
            </telerikGrid:DelegateGroupDescriptor>
        </telerikGrid:RadDataGrid.GroupDescriptors>
        <telerikGrid:RadDataGrid.Columns>
            <telerikGrid:DataGridTextColumn PropertyName="Template"/>
            <telerikGrid:DataGridTextColumn PropertyName="data"/>
            <telerikGrid:DataGridTextColumn PropertyName="info"/>
            <telerikGrid:DataGridTextColumn PropertyName="score"/>
            <telerikGrid:DataGridTextColumn PropertyName="result"/>
            <telerikGrid:DataGridTextColumn PropertyName="repeats"/>
        </telerikGrid:RadDataGrid.Columns>
    </telerikGrid:RadDataGrid>
</Grid>
</UserControl>

GroupingZoomedInView.xaml.cs

public sealed partial class GroupingZoomedInView : UserControl, ISemanticZoomInformation
{
    public void RadGrid1_SelectedIndexChanged(object sender, DataGridSelectionChangedEventArgs e)
    {
        template = (Templates)dataGrid.SelectedItem;
    }

    public void StartViewChangeFrom(SemanticZoomLocation source, SemanticZoomLocation destination)
    {
        source.Item = this.dataGrid.GetDataView().Items.OfType<IDataGroup>().Select(c => c.Key);
    }

    public void StartViewChangeTo(SemanticZoomLocation source, SemanticZoomLocation destination)
    {
        var dataview = this.dataGrid.GetDataView();
        var group = dataview.Items.OfType<IDataGroup>().Where(c => c.Key.Equals(source.Item)).FirstOrDefault();

        var lastGroup = dataview.Items.Last() as IDataGroup;
        if (group != null && lastGroup != null)
        {
            this.dataGrid.ScrollItemIntoView(lastGroup.ChildItems[lastGroup.ChildItems.Count - 1], () =>
            {
                this.dataGrid.ScrollItemIntoView(group.ChildItems[0]);
            });
        }
    }

    public Func<Templates> GetTemplateMethod()
    {
        return () => this.template;
    }
}

Here i need to return the template value to MainPage. How can i do that?

public MainPage()
{
        GroupingZoomedInView gView = new GroupingZoomedInView();
        Func<Templates> method = gView.GetTemplateMethod();
        Templates temp = method();
 }

 public class Templates(){
    public String filename { get; set; }
    public String data { get; set; }
 }
Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
  • 2
    How would you do it if you *weren't* using a sealed partial class? (Neither of those affect this...) – Jon Skeet Mar 08 '13 at 04:15
  • So `GroupingZoomedInView` is a control that allows a control that allows the user to pick a template from a data-bound list. Within your `MainPage` method you instantiate a new instance and immediately try to get the selected template. You never placed `gView` on the screen anywhere or allowed the user to select the template before attempting to get it from `gView`. Instead of creating a new `gView` you probably need to reference an existing one. – p.s.w.g Mar 09 '13 at 00:16

1 Answers1

3

You cannot "return" a value from a class. That only applies to functions. However, you can access the data within a class in several ways.

Use a public field (works, but bad practice)

public string template;

Use a public property (better)

public string Template { get; set; }

Use a public method

public string GetTemplate()
{
    return this.template;
}

For any technique listed here, in your MainPage class you will only be able to access the template value from within the body of a method.

public class MainPage
{
    private GroupingZoomedInView gView;

    public void SomeMethod()
    {
        String temp = gView.GetTemplate();
    }
}

BTW, the same goes for any kind of class. The fact that this one is sealed or partial makes no difference.


Update

In a comment, you said you wanted to return a method from a function. In that case, what you need to do use any of the above techniques, but change the return type from string to Func<string> (or some custom delegate type, which I won't go into here).

From member method

private string GetTemplate() 
{
    return this.template;
}

public Func<string> GetTemplateMethod()
{
    return new Func<string>(this.GetTemplate);
}

From a lambda expression

private string template;

public Func<string> GetTemplateMethod()
{
    return () => this.template;
}

From either of these techniques, in your MainPage class you can use the GetTemplateMethod like this.

public class MainPage
{
    private GroupingZoomedInView gView;

    public void SomeMethod()
    {
        Func<string> method = gView.GetTemplateMethod();
        string temp = method(); // executes the method
    }
}
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
  • i'm sorry, I explained myself badly. I want to know how can i return a method from a function in a sealed partial class. I tried before access to the template value with all those ways i said before and didn´t work. Including, i tried to do like this: public string Template { get; internal set; } , but it didn't work too. Can you help me? – Kelianosevis Mar 08 '13 at 11:51
  • @Kelianosevis I've updated my answer to include what I think you're asking for. I hope this helps. – p.s.w.g Mar 08 '13 at 13:53
  • It continues to return null value. I give a value to template in the partial class, and then when i'm trying to access to that value on MainPage, it returns me null. – Kelianosevis Mar 08 '13 at 14:05
  • @Kelianosevis are you setting `template` anywhere in your code? If it's compiling and returning a value, but that value is `null`, then you probably simply haven't set the value. – p.s.w.g Mar 08 '13 at 14:10
  • my template is a value that its returned from a selecting a row in a listbox.. like it was explained in the first topic. From using debug, i can check that the value is selected, because the template is equal to the value. But when i''m trying to access to that value from the mainpage, it returns me null. – Kelianosevis Mar 08 '13 at 14:24
  • @Kelianosevis I'm sorry but you have to provide more code if you want us to be able to help you figure out what's going wrong. – p.s.w.g Mar 08 '13 at 15:42