Domanda

I am c# web application beginner using silverlight 5. before i was working on c# console applications and there i had body of code working like this.

    namespace check
    {
     public class main
     {
      public void FunctionDefinition1()
      {
      //Inside something
      }

     }

     public class second
     {
      public static void Main(string[] args)
      {
       main object = new main();//this is how the objects were created and here the constructor was called.
       object1.FunctionDefinition1(); //I was calling the FunctionDefinition1 like this here 
      }

     }

    }

But when i tried to create a web application in c# silverlight (asp.net web application). I don't know how to call a function definition from other class and how and where the objects are created in that class ?

I have created a small project names as "Fun". see the code below


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;

    namespace Fun
    {
        public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();
            }

            public void merge(int head)
            {
                MessageBox.Show("check1");
            }
        }
        public class she
        {
            MainPage obj1 = new MainPage();
            obj1.merge(1); //It is not working gives red line, means error.
        }
    }

Could some one please explain me using this code silver light c# code that:

(1) How the Objects of the classes are created (I mean constructor is initialized when we create the object of that class, Is it same here, If yes then why it is giving red line in VS 2010 here on merge() function call using Object of MainPage class).

(2) what does this InitializeComponent(); do ?

(3) Why there is no public static void Main(string[] args) function inside it ? So how the control passes by compiler.

(4) Suppose if i inserted one button in GUI so now the code changed to this :

 using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;

    namespace Fun
    {
        public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();
            }

            public void merge_sort(int head)
            {
                MessageBox.Show("check1");
            }

            private void button1_Click(object sender, RoutedEventArgs e)
            {

            }
        }
        public class she
        {
            MainPage obj1 = new MainPage();
            obj1.button1_Click(....);//It don't recognize "obj1" object. Why ?
        }
    }

Now is it possible to call the button1_Click() function from another class "she" ? Thanks if some one could explain me how the compiler's control go on this c# code whereas i am not able to see any " public static void Main(string[] args)" function.

Big thanks for answering these question.

È stato utile?

Soluzione

UPDATED

I think I found your error.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Fun
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            merge_sort();
        }

        public void merge_sort()
        {
            she Object1 = new she();
            Object1.DoMethod(2);//Now this will do your work.
        }

    }
    public class she
    {
        public void DoMethod(int head)
        {
           //Do what do you want
        }

    }
}

Please Explain how do access the Class she?

2). InitializeComponent();

In my words I think it has the all your output, and also it's exists in the compiled assembly .

3). public static void Main(string[] args);

In WPF Silverlight WP8 application do not need this, Because when App.Xaml is build Main method will automatically generated.If you look at the project properties you'll find there's a setting for you to choose the startup object. So if you want, you can provide your own class that implements Main().

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top