Home > DeveloperSection > Forums > what is managed way to use a variable's pointer in structure?
Monaj Singh
Monaj Singh

Total Post:41

Points:287
Posted on    November-03-2014 11:31 PM

 C# ASP.Net 
Ratings:


 1 Reply(s)
 637  View(s)
Rate this:

Hi Guys

i'm trying to write a generic function which should be able to parse a xml file

here is the code

        public struct XmlArg

        {

            public string Name;

            public Type T;

            public object Value;

        };

 

        static bool ParseXmlArgs(XmlReader xml, params XmlArg[] args)

        {

            for (int i = 0; i < args.Length; ++i)

            {

                if (xml.MoveToContent() != XmlNodeType.Element || xml.Name != args[i].Name)

                {

                    return false;

                }

                args[i].Value = xml.ReadElementContentAs(args[i].T, null);

            }

            return true;

        }

 

        static void Main(string[] args)

        {

            int a = 0;

 

            ParseXmlArgs(

                XmlTextReader.Create("C:\\Users\\Yazilim\\Desktop\\XML.xml"),

                new XmlArg[]{

            new XmlArg() { Name = "ErrorCode", T = typeof(int), Value = a}});

        }

i know that i should pass a's pointer to Value ( it's type should be some different type other than object of course )

but i don't want it to be non-managed way.

is there any managed way to use a variable's pointer in structure ?

Thanks



sandy taylor
sandy taylor

Total Post:46

Points:324
Posted on    November-04-2014 2:31 AM

create an array, pass it to a method, and then read its contents afterwards:

You need to construct the XmlArg[] array before calling the method, and forget about the a variable until the parsing if finished:

var reader = XmlTextReader.Create("C:\\Users\\Yazilim\\Desktop\\XML.xml");
var results = new []
{
    new XmlArg() { Name = "ErrorCode", T = typeof(int) },
    new XmlArg() { Name = "ErrorMessage", T = typeof(string) }
};

if (ParseXmlArgs(reader, results) == true)
{
    // results were passed by reference, so do something with these values
}

Modified On Nov-04-2014 02:32:19 AM

Don't want to miss updates? Please click the below button!

Follow MindStick