?
Solved

C# Get Set Properties - Concatenate string

Posted on 2010-09-20
6
Medium Priority
?
573 Views
Last Modified: 2012-05-10
Hi!

I am very new to C# and OOP and are facing a problem which I was hoping could be resolved by using properties.

I wanted to build classes for all my API commands, so that I could quickly fill in the xml nodes, optional and mandatory ones without writing the xml every time.

Example:
    public class MyClass
    {
        public string DeviceID { get { return DeviceID; } set { DeviceID = "<DEVICEID>" + value + "</DEVICEID>"; } }
        public string CommandID { get { return CommandID; } set { CommandID = "<COMMANDID>" + value + "</COMMANDID>"; } }
        public string CreationDateTime{ get { return CreationDateTime; } set { CreationDateTime= "<CREATIONDATETIME>" + value + "</CREATIONDATETIME>"; } }
}

Open in new window


From this I could quickly build a parameter string for the webservice.

The project compiles, but when i run it, my memory goes up to 1,5 GB and then it crashes without any exceptions.

Are there any other ways that you guys could help me with? Alternative help me with the syntax here.

The webservice expects the call to be in the following format:
<PARAMETERS>
<MANDATORY>....</MANDATORY>
<OPTIONAL1>...</OPTIONAL1>
...........
<OPTIONAL9>...</OPTIONAL9>
</PARAMETERS>

Open in new window


Thank you,
/Mikael
0
Comment
Question by:amr-it
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 16

Expert Comment

by:Vikram Singh Saini
ID: 33716376
The project  compiles, but when i run it, my memory goes up to 1,5 GB and then it  crashes without any exceptions.

(1) This line made me to think that the problem is of some memory leak in your program.

(2) Make sure that you are closing all resources, file handles and all types of connections in your program. There seems to some instance or open resource that is consuming your memory too much.

(3) If program have some sort of infinite loop then also program can go out of memory and hence can crash.

Cross check all points. There is no issue in properties.

Regards,
VSS
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 260 total points
ID: 33716439
This line:

    public string CommandID { get { return CommandID; } set { CommandID = "<COMMANDID>" + value + "</COMMANDID>"; } }

Is an infinite loop because you are assigning a value to the property within the property itself. You cannot put logic inside of automatic properties. Automatic properties are properties ones where you allow the runtime to generate internal members for you. For example,

   public string MyVariable { get; set;}

is an automatic property because the runtime will create a hidden member to store the value MyVariable points to.

For your purpose, declare a private member and have your property point to it. Here is an example
private string _commandID;

public string CommandID { get { return this._commdandID; } set { this._commandID = "<COMMANDID>" + value + "</COMMANDID>"; } }

Open in new window

0
 
LVL 33

Assisted Solution

by:Todd Gerbert
Todd Gerbert earned 240 total points
ID: 33716453
Your get and set's are circular...that is, your public string DeviceID { get { return DeviceID; } creates an endless loop - when I use MyClass.DeviceID, the get block is executed which calls DeviceID again, which executes the get block, which calls DeviceID again, which executes the get block, and so on.
I think you want your properties to look something like this...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
	class MyClass
	{
		private string _deviceId;
		private string _commandId;
		private string _creationDateTime;

		public string DeviceID
		{
			set
			{
				// Assign the "plain" device ID to _deviceId string var
				_deviceId = value;
			}

			get
			{
				// Return the "plain" device ID plus XML
				return String.Format("<DEVICEID>{0}</DEVICEID>", _deviceId);
			}
		}

		public string CommandID
		{
			set { _commandId = value; }
			get { return String.Format("<COMMANDID>{0}</COMMANDID>", _commandId); }
		}

		public string CreationDateTime
		{
			set { _creationDateTime = value; }
			get { return String.Format("<CREATIONDATETIME>{0}</CREATIONDATETIME>", _creationDateTime); }
		}
	}
}

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:Vikram Singh Saini
ID: 33716511
Hi,

I didn't paid right attention to your properties code. kaufmed & tgerbert points are correct.

You are declaring your property in wrong ways. Rectify the mistake. However I pointed out for some infinite loop there. And this property declaration was the infinite loop.

Happy Programming!

Regards,
VSS
0
 
LVL 1

Author Comment

by:amr-it
ID: 33718377
Thank you for your comments! If there are other "nice" ways to solve this, I would appreciate suggestions of other ways of doing it.

Again, thank you.
0
 
LVL 1

Author Comment

by:amr-it
ID: 33741519
Just to add, after this I found out that XmlSerializer could do my classes.



 

using System;

public class clsPerson
{
  public  string FirstName;
  public  string MI;
  public  string LastName;
}

class class1
{ 
   static void Main(string[] args)
   {
      clsPerson p=new clsPerson();
      p.FirstName = "Jeff";
      p.MI = "A";
      p.LastName = "Price";
      XmlSerializer x = new XmlSerializer(p.GetType());
      x.Serialize(Console.Out, p);
      Console.WriteLine();
      Console.ReadLine();
   }
}    

//Output

<?xml version="1.0" encoding="IBM437"?>
 <clsPerson xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3
 .org/2001/XMLSchema">
<FirstName>Jeff</FirstName>
 <MI>A</MI>
<LastName>Price</LastName>
</clsPerson>

Open in new window

0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month7 days, 21 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question