|
Nitin1981 wrote: If so why DataReader can be used without new.
It is common practice to provide a method that instantiates an object and returns it to you without requiring you to do anything to create it. ExecuteDataReader internally creates a DataReader and returns an instantiated version to you.
the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
you either need to call new to create a new object or make a call that calls new on an object.
Think of the Color object
Color myColor = Color.Red;
Or the dataReader
DataReader myDataReader = myCommand.ExecuteReader();
The DataReader is easier to explain
There is a method on the command object that looks something like:
public DataReader ExecuteReader()
{
Some code.....
DataReader returnReader = new DataReader();
Some more code....
return returnReader;
}
I assume that the DataReader() constructor is private or internal so can't be called by me but the DataReader can use it fine.
For the Color example:
There will be a property like:
public static Color Red
{
get
{
return new Color(255,0,0);
}
}
You don't write new in either case but new has been called somewhere on your behalf.
HTH
Russ
|
|
|
|
|
Hello,
new is a keyword used to create objects in the memory. Yes, it is only used for declaring reference types.
new allocates memory in the heap for the object and after allocation returns its reference ( memory address ) back to the stack. you can then store that refernce in your object reference.
About DataReader, yes it is correct. new is called inside ExecuteDataReader, so you can not see it.
I hope it helps you.
Kindest Regards,
|
|
|
|
|
interface Mirror wrote: Yes, it is only used for declaring reference types.
No, Value types and reference types.
|
|
|
|
|
J4amieC wrote: interface Mirror wrote:
Yes, it is only used for declaring reference types.
No, Value types and reference types.
You can declare without new .
And everything in .net is an object, so int x = new int() ; is valid, but unnecessary.
Essentially, new calls the constructor of a class (or struct) to create a new instance of that class (or struct). In the case of Command.ExecuteReader() that call is made inside ExecuteReader to save you from the details.
You may also be used to creating the Command object with new , but you don't have to, the Connection has a CreateCommand method that will do that for you. I wish the Command object had a method to create a DataAdapter too
|
|
|
|
|
interface Mirror wrote: new is a keyword used to create objects in the memory. Yes, it is only used for declaring reference types.
new allocates memory in the heap for the object and after allocation returns its reference ( memory address ) back to the stack. you can then store that refernce in your object reference.
The new keyword can also be used to create values of a value type. In that case it won't create an object on the heap, it will just return the value.
---
It's amazing to see how much work some people will go through just to avoid a little bit of work.
|
|
|
|
|
Hi guys. Thanks for your help. One thing remians unclear which I would like Guffa to clarify please. Why is it necessary to use new while declaring a point like in window forms we have.
this.button1.Location = new System.Drawing.Point(32, 56);
if I remove new it gives error while Point is a struct (a value type which does not require new )
Please clarify my doubt.
Thanks
Don't Quit
|
|
|
|
|
The built-in value types does not need a constructor, as the compiler already knows how to create the values. For any user defined value type, though, you need a constructor to create a value.
A structure always has a parameterless constructor, that you can use to create a zero initalised value. If you declare a structure as a member variable of a class, it will also be zero initalised. If the structure is mutable (like the Point structire), you can then change the members of the structure value. If the structire is not mutable, using a constructor is the only way to create a value.
A constructor for a structure is a bit different from a constructor for a class, as it doesn't allocate memory for an object instance, it just returns the created value. A class constructor returns the reference to the allocated object, while a structure constructor returns the actual value.
The new keyword is used to call a constructor, regardless if you use it on a structure or a class. You can't call a constructor without the new keyword.
---
It's amazing to see how much work some people will go through just to avoid a little bit of work.
|
|
|
|
|
1. why do overloaded operators have to be static?
2. why do they have to be unary?
3. why does the overloaded operator being static imply that they are never polymorphic? (my attempt at answering #3: is it because since they are static you don't need an instance and therefore it can never change behaviour at runtime since you don't need an instance (just the class) to call the operator... meaning there is no opportunity to override this operator on derived class since it is class-based not instance based.
4. why is it recommended that operators are overloaded only on value types?
5. finally, why would i implement the postfix operator differently for a class than for a struct?
ps. these aren't assignment questions, i am trying to learn c# without a book. what books wou=ld you recommend? i am thinking of buying 3: 1) clr via c# 2) visual c# 2005 by Francesco Balena 3) inside c#
tia!
-- modified at 7:25 Thursday 21st December, 2006
|
|
|
|
|
j11Software wrote: 1. why do overloaded operators have to be static?
They get passed the values upon which to act -- e.g. a+b ==> operator+(a,b)
j11Software wrote: 2. why do they have to be unary?
They don't, they may be binary.
j11Software wrote: 3. why does the overloaded operator being static imply that they are never polymorphic?
Static methods can't be virtual.
j11Software wrote: 4. why is it recommended that operators are overloaded only on value types?
Never heard of that. Use them wherever they make sense.
j11Software wrote: 5. finally, why would i implement the postfix operator differently for a class than for a struct?
What's a postfix operator?
|
|
|
|
|
The characteristics of operator overloading are what they are by design.
One of the main goals must have been to avoid confusion.
Hence, overloaded operators are:
- static (int+int performs addition irrespective of numeric values, so obj+obj should
do that too)
- either unary or binary (you can define obj-obj as well as -obj)
- similar in precedence as for numeric operations (* before +)
All of this makes the source code stays readable and intuitive.
So operator overloading is just fine for classes such as complex numbers,
big integers, etc, and anything really fancy is prohibited.
For a good example, take a look at http://www.codeproject.com/csharp/biginteger.asp[^]
Luc Pattyn
|
|
|
|
|
Hi all
I am developing one installation wizard. I have one issue. If i click the next button I made the current form visible = false and creating instance for the next from and showing the form. There problem I facing is, I dont know how to show the previous form by hidding the current form. Can anybody help me please....
Thanks
kesavan
kesavan
|
|
|
|
|
Hello,
You could use a property Form1 in youre Form2, which is set by Form1.
private Form myform1;
public Form MyForm1
{
get
{
return myform1;
}
set
{
myform1 = value;
}
}
{
MyForm1.Visible=true;
MyForm1=null;
this.Close();
}
{
Form2 form2 = new Form2()
form2.MyForm1 = this;
}
All the best,
Martin
|
|
|
|
|
Thank you so much martin. I have done with your help
kesavan
|
|
|
|
|
Why don't you send a reference of form1 throught the constructor of form2.
Example:
the constructor of form2 is:
<br />
private Form1 parentForm;<br />
<br />
public void Form2(From1 form)<br />
{<br />
parentForm = form1;<br />
}<br />
and when u initialize the second form to call it in form1,
use this:
<br />
Form2 form2 = new Form2(this);<br />
form2.show();<br />
now you can use the visible property of the form1 from form2 and u can unhide it by simply writting this:
<br />
parentForm.Visible = true;<br />
Good luck
|
|
|
|
|
Thank you so much smart. I have done with your help
kesavan
|
|
|
|
|
depending how many forms you are going to end up with. I would be tempted to make a control class that holds the data and controls which forms get loaded / unloaded at which point. If you are only ever going to have 2 forms it's probably not worth it but if you don't know how many forms you might end up with or which order people are going to want them in it's probably worth the extra effort up front.
Russ
|
|
|
|
|
hi
i want to use oledbCommandBuilder for Insert,Update,Delete Commands dinamically, but how to use it ?
thanks
|
|
|
|
|
|
Hi,
I have a Peculiar problem in my C# Windows application. The scenario is, we have a windows application running in windows Xp environment, its main operation is copy the Employee word Document file from one employee machine and paste it to the other employee. It’s purely a File copy and Paste operation. To do this we shared One drive in all the employee machine with full access writes to every in the network. So my application go to that particular shared drive copy all document from that location and paste it in to other shared drive. This is working fine in the current environment.
My problem is, now that all shared drives permissions are changed and restricted to Read only permission, every one in the network they only read the file in the network shared drive, they can not paste any file in to that drive. This is to avoid other employee miss use and security purpose. After this implementation my application is not copying the file from that shared drive and pasting. It’s throwing the error unauthorized access or don’t have a permission to access folder.
My question is how to give permission to the application to access that folders and copy to other network shared drive even if the application runs in windows user login mode. This application runs in entire employee machine. Once they login to the system it starts running. Please suggest me the best way to over come this issue
Thanks in advance.
By,
Rajasekarn Shan
|
|
|
|
|
You should probably use TCP/IP sockets to send the file across. When a file needs to be copied you can establish a connection with the particular machine and send the file, the receiving machine can save the file into a configured folder.
Tarakeshwar Reddy
MCP, CCIE Q(R&S)
|
|
|
|
|
Hi tarakeshwar,
can you provide me a little more about TCP/IP? some examples if possible.
By,
Rajasekaran.S
Rajasekarn Shan
|
|
|
|
|
|
Hi there,
I am currently creating a web application in c# 2005, on my search form i would like to display an animated gif image while busy doing the search in the sql database.
As soon as the user clicks the search button i want the image to become visible and then as soon as the gridview got binded with the results, i want the image to be invisible again.
Now the problem is that the image does not become visible at all
mmmm i dunno where to start finding the problem cause it does run the code but does not actually do it lol.
I am thinking i should probably use like "sleep" or threads but thats some weird stuff for me as i am still beginner. Is there any other solution to this problem?
Thanks in advance
Regards
|
|
|
|
|
Hello,
I think the problem is that you are running the control with the gif in the same threat (which is busy) where you are doing the search.
You could do a control.Refresh();, after you set Visible=true.
But it would only show the not- or rarely moving gif I think.
All the best,
Martin
|
|
|
|