|
S. Senthil Kumar wrote: Actually yes, there is always the default parameterless constructor, regardless of whether you overload it or not.
Yes, in theory the value type has a parameterless constructor. It never exists as an actual method.
This source code:
int x = new int();
actually results in this code being created by the compiler:
int x = 0;
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
George_George wrote:
1. For value type, even if we provide a customized constructor, there is also the default one (co-exist with customized one)?
Yes. Read the spec[^] again.
George_George wrote: 2. For reference type, there is also a default constructor even if we provide a customized one (co-exist with default one)?
Read the spec (http://www.jaggersoft.com/csharp_standard/8.7.8.htm[^]) again.
"If no instance constructor is supplied for a class, then an empty one with no parameters is automatically provided."
|
|
|
|
|
Thanks Senthil,
S. Senthil Kumar wrote: "If no instance constructor is supplied for a class, then an empty one with no parameters is automatically provided."
And even if we provide a customized one, the default parameter-less one still exists.
regards,
George
|
|
|
|
|
George_George wrote: And even if we provide a customized one, the default parameter-less one still exists.
Huh? You do realize that statement directly contradicts the spec?
Try compiling this
class S
{
int x;
public S(int x)
{
this.x = x;
}
public int X
{
get { return x; }
}
}
class Program
{
static void Main(string[] args)
{
S s = new S();
Console.WriteLine(s.X);
}
}
|
|
|
|
|
Sorry, Senthil! It will not compile, my mistake to understand the statement.
So, for statement like below, the memory for new instance of abc is actually allocated even if we do not use new + constructor explicitly. But for reference type, without using new, no instances' space are allocated. Correct?
StructValueType abc;
regards,
George
|
|
|
|
|
George_George wrote: the memory for new instance of abc is actually allocated even if we do not use new + constructor explicitly
George_George wrote: StructValueType abc;
If you use abc before initializing, your code won't compile. Value types also need to be initialized.
|
|
|
|
|
Thanks ForumExpertOnLine,
I agree. I mean the memory space is allocated, right?
regards,
George
|
|
|
|
|
George_George wrote: So, for statement like below, the memory for new instance of abc is actually allocated even if we do not use new + constructor explicitly. But for reference type, without using new, no instances' space are allocated. Correct?
Depends on what you call memory? In both cases, stack space will be reserved for holding the local variable. In the case of reference types, space on the heap will not be allocated until you call new. That doesn't apply for value types obviously, so stack space will be reserved right away.
|
|
|
|
|
S. Senthil Kumar wrote: so stack space will be reserved right away.
I doubt. AFAIK, it depends on the context where the struct variable is used. If it is declared with a instance field, I guess it will be kept in the heap along with the instance. I am not sure though. This[^] is worth reading.
|
|
|
|
|
Good point, do you have any ways to test or prove whether memory is allocated or not for value type variables used as a member field for a reference type?
regards,
George
|
|
|
|
|
Which is why I said "In both cases, stack space will be reserved for holding the local variable.
|
|
|
|
|
Thanks Senthil,
Question answered.
regards,
George
|
|
|
|
|
George_George wrote: And even if we provide a customized one, the default parameter-less one still exists.
NO.
class Program
{
static void Main(string[] args)
{
Foo foo = new Foo();
}
}
class Foo
{
public Foo(int param)
{
}
} The above code won't compile. When you provide a constructor, comoiler won't generate a default one.
|
|
|
|
|
Thanks ForumExpertOnLine!
1.
Good to learn from you value type and reference type treat this scenario differently -- whether or not the default constructor exists if we provide a customized constructor -- for value type, it exists and for reference type it does not exists. Right?
2.
Any comments to?
http://www.codeproject.com/script/Forums/View.aspx?fid=1649&msg=2598697[^]
regards,
George
|
|
|
|
|
George_George wrote: for value type, it exists and for reference type it does not exists. Right?
Yes. For structs, default parameterless constructor is created automatically. You can't write a parameterless constructor for a struct.
|
|
|
|
|
Thanks ForumExpertOnLine,
Question answered.
regards,
George
|
|
|
|
|
Hi,
I have a XML document (that I've got from an InfoPath form) and it has its namespaces.I'd like to deserialize this XML document into a type and send it to a WebService.The type has been generated when I referenced the WebService in my project.
When I try to deserialize the XML document I get a "xmlns was not expected" error thrown by XmlSerializer.
What should I do to either remove namespaces from the XML document or introduce them to XmlSerailizer so It can recognize QNames?
Regards
|
|
|
|
|
I want to make sure I understand...
You have some XML from an InfoPath form and want to pass that to a Web Service. The Web Service takes a parameter (likely a class) that looks something like your XML, but when you try to cast your XML into the Web Service's type by attempting to deserialize your XML you get the error.
If this is the case, you are getting the error because the InfoPath type is different from the type that the Web Service is expecting.
The easiest way to get this to work is to compare what the web service expects with the XML from infopath. If only the namespace, or namespace prefix, or both is/are different, you could take the quick route and use basic string manipulation to remove/edit as necessary.
Manipulating XML strings directly is not recommended in most cases, but it does make sense in some since some transformations are really that simple.
The correct way of accomplishing this is to transform the XML InfoPath into the serialized version of the type that the web service expects and then send deserialze it into the WS's type. You transform from one type into another using XSL and a bit of code to do the transformation.
The problems with transforming using XSL include:
- You need to know at least some XSL
- Storage of the XSL
- Changes in either the input or output require changes to the XSL
If you don't want to go with either approach, you do have one more option: extract the values you need from the InfoPath XML and copy the values into the type that the Web Service uses - sort of a manual transformation. You cold extract values from the InfoPath XML using XPath queries or just grab them using regular expressions or brute force string matches.
So, you have a few options. Your choice depends on how comfortable you are with the underlying approaches and whether you'll end up maintaining whatever you choose
Erik Westermann - wWorkflow.net - Consulting Services
SOA * ESB * BPI ...and lots of other cool TLAs related to integration and architecture.
|
|
|
|
|
Thank you very much for you kind and helpful reply.
|
|
|
|
|
Hi ALL,
I need only Time format in Textbox..So please..How to do..
Regards
Somasundaram G
|
|
|
|
|
Hello
Give me the complete detail? If u wnat to put time in a text box then simply change the format using DateTime class and then assigned to text box. Simple.
Syed Shahid Hussain
|
|
|
|
|
I need Time Format like this 06:30:40 in TextBox.So pls..
I want to join in the website
|
|
|
|
|
if you put in the textbox..
here's the code...
textBox1.Text = DateTime.Now.ToLongTimeString();
if you want the seconds to move like a digital clock use timer with it..
|
|
|
|
|
System.DateTime dTime = new System.DateTime(2001, 5, 16, 3, 2, 15);
// If the current culture is "en-US",
// longDateString gets "Wednesday, May 16, 2001"
string longDateString = dTime.ToLongDateString();
// If the current culture is "en-US",
// longTimeString gets "3:02:15 AM"
string longTimeString = dTime.ToLongTimeString();
or u can use DateTime.ToShortTimeString() method
|
|
|
|
|