initializecomponent() don't exist in current context while using generics in c# web application

我的未来我决定 提交于 2019-12-12 02:26:17

问题


I am trying to create a generics in c# web application and using silverlight-5. 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)
(3) And all the buttons and boxes i created are given not existing in current context.

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.


回答1:


Here is a Example.

    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);
           }
       }
   }

I think you asking this kind of example.




回答2:


You can use generic as if you don’t use XAML. But if you want to use XAML to define your control, you can’t use generic. That's why the problem is occurs.

Create a another class and use it. I think It's help you.



来源:https://stackoverflow.com/questions/22863338/initializecomponent-dont-exist-in-current-context-while-using-generics-in-c-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!