locked
generics in c# web application using silverlight (name 'InitializeComponent' does not exist in the current context) RRS feed

  • Question

  • I am trying to create a generics in c# web application and using silverlight-5 and i am **badly stuck in this problem since last week**. This i have already implemented in c# console application.

    I am trying to do same in webdevelopment using asp.net,c# and silverlight (and GUI using xaml) in Vs-2010. Whose GUI is displayed on internet explorer on running the code (by button click events).

    In console application i do so by following code : (The code is to read a binary file as sole argument on console application and read the symbol in that file, These symbol could be int32/int16/int64/UInt32 etc.). So have to make this Symbol variable as "generic"(<T>). And in console application this code works fine.

       using System;
        using System.IO;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using System.Runtime.InteropServices;
    
    
    
        namespace check 
        {
    LINE:1  public class Huffman < T > where T: struct,IComparable < T >,IEquatable < T > 
            {
                public int data_size, length, i, is_there;
                public class Node 
                {
                    public Node next;
    line:2          public T symbol; // This symbol is of generic type.
                    public int freq;
                 }
                public Node front, rear;
    LINE:3         public Huffman(string[] args, Func < byte[], int, T > converter) 
                {
                    front = null;
                    rear = null;
                    int size = Marshal.SizeOf(typeof (T));
                    using(var stream = new BinaryReader(System.IO.File.OpenRead(args[0]))) 
                    {
                        long length = stream.BaseStream.Length;
                        for (long position = 0; position + size < length; position += size)
                        {
                            byte[] bytes = stream.ReadBytes(size);
    LINE:4                  T processingValue = converter(bytes, 0); //**Here I read that symbol and store in processing value which is of type <T>** 
                            //Then  further i use this processingValue and "next" varible(which is on Node type)
                        }
                    }
                }
            }
    
            public class MyClass
            {
                public static void Main(string[] args)
                {
        line:5            Huffman < long > ObjSym = new Huffman < long > (args, BitConverter.ToInt64); 
                        // It could be "ToInt32"/"ToInt16"/"UInt16"/"UInt32"/"UInt64" with respective 
                        //change in <int>/<short> etc.
    
    
    
                    //Then i further use this ObjSym object to call function(Like Print_tree() here and there are many more function calls)   
                    ObjSym.Print_tree(ObjSym.front);
                }
            }
        }

    The same thing i have to achieve in C# silverlight(web application) with a difference that i have already uploaded and stored the file by button click (By Browsing it)(whereas i was uploading/reading file as sole argument in console application), This file upload part i have already done.

    The problem now is how to make this "symbol" variable generic(<T>) here because i am not able to see any Object creation (In main(string[] args) method) where i could pass parameter BitConverter.ToInt32/64/16 (as i am doing in console application, please see code).

    NOTE: please see that i have used in LINE 1,2,3,4,5 in my code (so that the same(or different if you have other approach) has to be achieved in the code below to make "symbol" of type )

    Because in c# i get body of code like 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;
    using System.IO;
    using System.Text;
    using System.Runtime.InteropServices;
    using System.Diagnostics;
    namespace check
    {
        public partial class MainPage : UserControl
        {
             public class Node
            {
                public Node next;
                public long symbol;   // This symbol is of generic type.
                public int freq;
            }
            public Node front, rear;
    
           public MainPage()
            {
                InitializeComponent();
            }
    
        }
    }

    Could some one please help me in changing the code of this web application exactly similar to that console application code (I mean making "Symbol variable as generic(<T>)")

    EDIT: When i do this:

    (1) public partial class MainPage <T> : UserControl, IComparable < T > where T: struct,IEquatable < T > 
    (2) public T symbol; (In Node class)

    then it gives error

    Error :The name 'InitializeComponent' does not exist in the current context

    Could some one please help me in achieving the same in c# silverlight web application ? Would be a big help,thanks.




    Friday, April 4, 2014 12:54 PM

Answers

  • Hi,

    I saw that you have solved your problem in here with the following:

    Here is a Example which may what you are looking for:

    namespace check
        {    
           public partial class MainPage : UserControl
           {
                public MainPage()
                {
                    InitializeComponent();
                    // Use the generic type Test with an int type parameter.
                    Test<int> Test1 = new Test<int>(5);
                    // Call the Write method.
                    Test1.Write();
    
                    // Use the generic type Test with a string type parameter.
                    Test<string> Test2 = new Test<string>("cat");
                    Test2.Write();
                }
           }
    
           class Test<T>
           {
               T _value;    
               public Test(T t)
               {
                  // The field has the same type as the parameter.
                  this._value = t;
               }    
               public void Write()
               {
                  MessageBox.Show(this._value);
               }
           }
       }

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, April 7, 2014 2:51 AM
    Moderator