1

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.

Sss
  • 1,519
  • 8
  • 37
  • 67

1 Answers1

2

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().

Harshana Narangoda
  • 775
  • 1
  • 8
  • 23
  • In your code where do you invoke merge_sort() function ? You have never called it as a result DoMethod will never be called. And if you invoke merge_sort by calling it then where you will call it in MainPage class ? – Sss Apr 04 '14 at 08:29
  • And she.DoMethod(); will give :Error :An object reference is required for the non-static field, method, or property 'Fun.she.DoMethod()' – Sss Apr 04 '14 at 08:34
  • You can try this code in c# silverlight webapplication it will give error, Not working. I am still not able to understand the basic concepts of c# web application, in console application everything was fine. – Sss Apr 04 '14 at 08:38
  • 1
    I made a Mistake and I'll Update the Answer – Harshana Narangoda Apr 04 '14 at 09:44
  • 2
    Try this one it'll be OK. 2nd error because of the she class is not a static class so you can't access like that, Now it will be ok. In WPF and Silverlight Applications default Constructor will execute first. that means InitializeComponent() method included code block.In now when the page load merge_sort() method will execute. – Harshana Narangoda Apr 04 '14 at 09:49
  • Thanks you mean structure of c# in console application is different then c# web application since i am not able to use main() function. Actually i will further use generics in this c# web application code. – Sss Apr 04 '14 at 10:41
  • 2
    defensively Both has different structures. You can't use all of Console application Functions in Silverlight. Silverlight using .net framework but it hasn't all functions of the .net Framework. So you have some limitations in Silverlight rather that Console Applications.What did you mean main() function? is that Main Method? – Harshana Narangoda Apr 04 '14 at 10:48
  • yes main method()(where the first control go in console application , where we create object for constructor). And is it possible to create generic type in c# silver light web application ? – Sss Apr 04 '14 at 11:03
  • could you please tell me : is it possible to create generic type in c# silver light web application ? Any hint how to do that ? – Sss Apr 04 '14 at 11:28
  • Yes You can create the generic types objects. Main method will be create automatically in Silverlight and WPF. in app.xaml you can mentioned what is the first loading control. – Harshana Narangoda Apr 04 '14 at 11:29
  • I need a example for it, then I can help you easily. – Harshana Narangoda Apr 04 '14 at 11:35
  • ,Thanks Bhai :) , OK i am asking another question on stackoverflow and will give you link of that question soon. I would have tears of joy once it is done :) . – Sss Apr 04 '14 at 12:00
  • here is my new question , could you please help me there (about generics) : http://stackoverflow.com/questions/22863338/how-to-create-generics-in-c-sharp-web-application-using-silverlight-5 – Sss Apr 04 '14 at 12:51