|
Thanks, Dave,
My observation that using the return value of a function is not required in C# was, perhaps, surprising to me since I associate such lexical 'slack' as being found in more weakly typed languages (LISP, PostScript in my experience).
Personally I would like the C# compiler to flag cases where I ignore the return type of a function.
The C# compiler will certainly not let you get away with this:
private void someFunc(string someValue)
{
}
private string someFunc(string someValue)
{
}
best, Bill
"In the River of Delights, Panic has not failed me." Jorge Luis Borges
|
|
|
|
|
Correct. You can't vary an overload just by the return type.
But, in this example, the compiler will throw if you specify a return type, but don't return anything.
|
|
|
|
|
Dave Kreskowiak wrote: In the second, your code is still in a namespace. Open the Project Properties and you'll find "Default Namespace" specified.
Wrong, code without a namespace ends up in the global:: namespace. The "Default Namespace" in the Project Properties is used for the autogenerated classes like resources and settings, etc.
"Don't confuse experts with facts" - Eric_V
|
|
|
|
|
Crap, my bad. I was thinking about VB.NET's version.
|
|
|
|
|
Must be a slow news day.
|
|
|
|
|
in C# a method may or may not have a return type (other than void ), and when it does you may or may not be interested in it.
The same idea has existed ever since C and C++ came to be; and it is different from the Basicy function/subroutine dichotomy.
So there is no error whatsoever.
However, there is one particular case where a warning message would be extremely welcome:
string text="...";
text.Insert(0, "aha");
since strings are immutable, string methods like the one shown are returning strings, ignoring the return value does not make much sense (unless you really want to test for valid parameters and dealing with a possible exception).
|
|
|
|
|
+5 . The example you mentioned is perfect. Beginners in .NET who are unaware of the immutability of strings write such code and wonder why their strings have not changed. A compiler error (or at least a warning) would be welcome.
"Don't confuse experts with facts" - Eric_V
|
|
|
|
|
In the first case, almost all languages allow you to do that, they just discard the return value.
In the second case, code not enclosed in a namespace declaration ends up in the global:: namespace. If there is a naming conflict between two classes, one inside a namespace and the other outside of any namespace, then the latter can be access using the global:: namespace.
namespace WindowsFormsApplication1 {
class Class2 {
}
static class Program {
[STAThread]
static void Main() {
Class2 c2 = new Class2();
c2.DoSomething();
global::Class2 c3 = new global::Class2();
c3.DoSomething();
}
}
}
class Class2 {
public void DoSomething() {
}
}
"Don't confuse experts with facts" - Eric_V
|
|
|
|
|
hello everyone,
i don't know much about service application,
i want to know dat can we create window services in VS2008
using c# ?
i didn't find window service template in my VS2008 professional edition.
and Is this possible that my window app which is develop in
c# VS2008 can work as window service on vista.
thank you.....
-- Modified Monday, August 22, 2011 3:20 AM
|
|
|
|
|
As far as I know the template is only available for C++. However, you may find some reasonable ideas here[^].
|
|
|
|
|
It's available in the Windows section of the c# project types and is called "Windows Service", although I'm not sure if it's installed by default in all versions of VS but would have thought it was in the professional edition.
|
|
|
|
|
yes i found it thank you......
but can i call my app exe in window service.
|
|
|
|
|
I've re-read you question.
Yes you can use your application as a service but not without some modification. If most of the functionality is in class libraries then your application (be it a console application or a windows service) acts as an entry point to your functionality.
You will need to do some extra work around error reporting and service specific things like startup options but a basic introduction to windows services via google will help with this.
modified on Monday, August 22, 2011 9:17 AM
|
|
|
|
|
Hi All,
First of all I would like to thank you for the support. I am new in coding and need assistance in getting a specific xml content to an excel spreadsheet or to a datagridview.
the XML file is not a standard one (i think and below is a sample).
what i need from this xml file is the following data:
the name (Towers xxx] of the alert and the gps points (dont need the utm)
this is the xml
[code]
="1.0"="UTF-16"
<layer>
<name>
SF Zones
</name>
<alert_area_object>
<creation_time>
1303889594
</creation_time>
<name>
Tower 1
</name>
<description>
Fence Alert
</description>
<event_types num="1">
<event_type>
11111
</event_type>
</event_types>
<event_severity>
2
</event_severity>
<event_override_flag>
false
</event_override_flag>
<identities num="0"/>
<categories num="0"/>
<groups num="0"/>
<target_appear_in_area>
true
</target_appear_in_area>
<target_enter_area>
true
</target_enter_area>
<target_leave_area>
true
</target_leave_area>
<from_hour>
0
</from_hour>
<from_minute>
0
</from_minute>
<to_hour>
23
</to_hour>
<to_minute>
59
</to_minute>
<from_speed>
0
</from_speed>
<to_speed>
98.8889
</to_speed>
<weighting>
<weight>
<type>
2
</type>
<value>
99
</value>
</weight>
<weight>
<type>
204
</type>
<value>
99
</value>
</weight>
<weight>
<type>
205
</type>
<value>
99
</value>
</weight>
</weighting>
<visual_feedback>
true
</visual_feedback>
<sound_repeats>
-1
</sound_repeats>
<wave_file>
C:\4C\RunTime\OWP\Sounds\sirenwawa.wav
</wave_file>
<utm_zone>
40
</utm_zone>
<shape type="ClosedPolygon">
<num_of_alloc_points>
22
</num_of_alloc_points>
<num_of_points>
5
</num_of_points>
<geo_position lat="40.76876678920284" long="-73.98097831075269"/>
<geo_position lat="40.76869191861909" long="-73.98078254132794"/>
<geo_position lat="40.76839192572469" long="-73.97999998924834"/>
<geo_position lat="40.76749000414105" long="-73.97804528571041"/>
<geo_position lat="40.76643902004108" long="-73.97593868227233"/>
<utm_points x="132131" y="132131"/>
<utm_points x="132131" y="132131"/>
<utm_points x="132131" y="132131"/>
<utm_points x="132131" y="132131"/>
<utm_points x="132131" y="132131"/>
</shape>
</alert_area_object>
<alert_area_object>
<creation_time>
1303890003
</creation_time>
<name>
Towers 1 to 4
</name>
<description>
Fence Alert
</description>
<event_types num="1">
<event_type>
11111
</event_type>
</event_types>
<event_severity>
2
</event_severity>
<event_override_flag>
false
</event_override_flag>
<identities num="0"/>
<categories num="0"/>
<groups num="0"/>
<target_appear_in_area>
true
</target_appear_in_area>
<target_enter_area>
true
</target_enter_area>
<target_leave_area>
true
</target_leave_area>
<from_hour>
0
</from_hour>
<from_minute>
0
</from_minute>
<to_hour>
23
</to_hour>
<to_minute>
59
</to_minute>
<from_speed>
0
</from_speed>
<to_speed>
98.8889
</to_speed>
<weighting>
<weight>
<type>
2
</type>
<value>
99
</value>
</weight>
<weight>
<type>
204
</type>
<value>
99
</value>
</weight>
<weight>
<type>
205
</type>
<value>
99
</value>
</weight>
</weighting>
<visual_feedback>
true
</visual_feedback>
<sound_repeats>
-1
</sound_repeats>
<wave_file>
C:\4C\RunTime\OWP\Sounds\sirenwawa.wav
</wave_file>
<utm_zone>
40
</utm_zone>
<shape type="ClosedPolygon">
<num_of_alloc_points>
22
</num_of_alloc_points>
<num_of_points>
6
</num_of_points>
<geo_position lat="40.76876678920284" long="-73.98097831075269"/>
<geo_position lat="40.76869191861909" long="-73.98078254132794"/>
<geo_position lat="40.76839192572469" long="-73.97999998924834"/>
<geo_position lat="40.76749000414105" long="-73.97804528571041"/>
<geo_position lat="40.76643902004108" long="-73.97593868227233"/>
<utm_points x="132131" y="132131"/>
<utm_points x="132131" y="132131"/>
<utm_points x="132131" y="132131"/>
<utm_points x="132131" y="132131"/>
<utm_points x="132131" y="132131"/>
</shape>
</alert_area_object>
</layer>
[/code]
thanks again
|
|
|
|
|
Use XSLT to transform the XML and extract the data you need into the proper format.
I know the language. I've read a book. - _Madmatt
"The OP herself was not sure about her question"
"The OP is from India and I know what she meant." - Shameel
|
|
|
|
|
Hi,
this is the code i currently have but it parse the xml but is missing some nodes and is not exactly what i need.
i need that it will get the name of the alert and all the gps poings while with this code it takes most of the data except the gps points
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Xml;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
XmlDataDocument xmlDatadoc = new XmlDataDocument();
xmlDatadoc.DataSet.ReadXml(@"C:\Desktop\Book Application\zone.xml");
DataSet ds = new DataSet("book DataSet");
ds = xmlDatadoc.DataSet;
dataGridView1.DataSource = ds.DefaultViewManager;
dataGridView1.DataMember = "alert_area_object";
}
}
}
thanks
modified on Thursday, August 25, 2011 3:04 AM
|
|
|
|
|
Edit this and use the code block toolbar item to properly format the code snippet.
I know the language. I've read a book. - _Madmatt
"The OP herself was not sure about her question"
"The OP is from India and I know what she meant."
|
|
|
|
|
|
I have a win app and a web app developed in .Net Framework 3.5.
I want to pass an object from web to win app.
I want the receiver app be notified from coming object and recive it.
What are the solutions?
Best wishes
|
|
|
|
|
Your options:
0) Write a web service that you then access from your desktop app (not kowing the specific details of the data being requested, or why it's being requested, this is the way I'd do it)
1) Write a new page on your web site that you can browse to that returns the data you want, and use the HTTPWebRequest object to browse to that web page.
You can't "push" to the desktop app because web services and web sites are request-driven, meaning the data is only returned when that data is requested.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997
|
|
|
|
|
Please look into the Bayeux Protocol[^]. It was based on the idea of inverting the usual client request / server response paradigm. Of course the client still has to connect to the server and keep that connection open, but after that the server can push notifications/data to the client.
I'm not sure if there are any client side APIs that would let you use this in a WinForm application, but you could circumvent this by using a webbrowser control inside your WinForm application.
Best Regards,
—MRB
"With sufficient thrust, pigs fly just fine."
Ross Callon, The Twelve Networking Truths, RFC1925modified on Monday, August 22, 2011 10:58 AM
|
|
|
|
|
|
Thanks Keith. It looks as if deep linking isn't allowed. I pasted in the link from the google page and now it works.
Cheers!
"With sufficient thrust, pigs fly just fine."
Ross Callon, The Twelve Networking Truths, RFC1925
|
|
|
|
|
Don't have the attention span to post as article - but thought this might be interesting:
Automatic properties are great but the annoyance of not being able to set them automatically defers a lot of people from using them.
What we (read - I) want to be able to do something like this:
double MyPie { get; set; } = "3.141";
string MyLove { get; set; } = "Pizza";
The default value attribute in System.ComponentModel is of no help there, though a lot of us (read - me) tried that in the beginning:
[DefaultValue(0.3141)]
double MyPie { get; set; }
[DefaultValue("Pizza")]
string MyLove { get; set; }
Initializing the properties one by one in the constructor is at least as annoying as simply doing this:
double _myPie = 0.3141;
double MyPie { get { return _myPie; } set { _myPie = value; } }
double _myLove = "Pizza";
double MyLove { get { return _myLove; } set { _myLove = value; } }
Luckily the nice little feature of extension methods allows us something not exactly optimal or fully elegant - but functional and a good exercise in C# clockwork.
So with just a bit of hammering we can enable automatic reset properties to their declared System.ComponentModel.DefaultValueAttribute if any was specified.
Test code:
using System;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;
using CSharpUtils;
namespace MyAppNamespace
{
public class TestClass
{
[DefaultValue(0.3141)]
public double MyPie { get; set; }
[DefaultValue("Pizza")]
public string MyLove { get; set; }
public TestClass()
{
MessageBox.Show(string.Format("MyPie = {0}, MyLove = {1}", MyPie, MyLove == null ? "null" : MyLove));
int ret = (this as Object).ResetPropsUsingDefaultAttributes(false);
MessageBox.Show(string.Format("MyPie = {0}, MyLove = {1}. {2} properties we set", MyPie, MyLove == null ? "null" : MyLove, ret));
MyLove = "My dear wife";
MessageBox.Show(string.Format("MyPie = {0}, MyLove = {1}", MyPie, MyLove == null ? "null" : MyLove));
ret = (this as Object).ResetPropsUsingDefaultAttributes(false);
MessageBox.Show(string.Format("MyPie = {0}, MyLove = {1}. {2} properties we set", MyPie, MyLove == null ? "null" : MyLove, ret));
}
}
}
This (as you may have already guessed) is done using an extension method to the System.
You will need a public static class to define the extension method in. here's the basic implementation source (feel free to use the code and extend it's functionality):
using System;
using System.Text;
using System.Reflection;
using System.ComponentModel;
namespace CSharpUtils
{
static class CSharpUtilsExtensionMethods
{
public static int ResetPropsUsingDefaultAttributes(this Object oThis, bool initInheritedProperties)
{
int count = 0;
Type oType = oThis.GetType();
PropertyInfo[] infos = oType.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo inf in infos)
{
if (initInheritedProperties || inf.DeclaringType.Equals(oType))
{
object[] oDefAtts = inf.GetCustomAttributes(typeof(DefaultValueAttribute), initInheritedProperties);
if (oDefAtts.Length > 0)
{
DefaultValueAttribute defAtt = oDefAtts[oDefAtts.Length - 1] as DefaultValueAttribute;
if (defAtt != null && defAtt.Value != null && !defAtt.Value.Equals(inf.GetValue(oThis, BindingFlags.GetProperty, null, null, null)))
{
inf.SetValue(oThis, defAtt.Value, BindingFlags.SetProperty, null, null, null);
count++;
}
}
}
}
return count;
}
}
}
All that's left now is use out test class (here again - with no message box and other nonsense
using System;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;
using CSharpUtils;
namespace MyAppNamespace
{
public class TestClass
{
[DefaultValue(0.3141)]
public double MyPie { get; set; }
[DefaultValue("Pizza")]
public string MyLove { get; set; }
public void ResetProps()
{
(this as Object).ResetPropsUsingDefaultAttributes(false);
}
public TestClass()
{
ResetProps();
}
}
}
Notes:
- To use the extension method - the .cs file making the call must have a 'using' declaration of namespace where the extension method resides in.
- Passing true for parameter initInheritedProperties also initializes any parent class properties - at the expense of possibly resetting already initialized properties. you can always extend the extension method to try and account for these cases.
- Resetting the properties in this manner works also outside constructor - anywhere in the program with the proper access.
- There's still the annoyance of having to add a line in the constructor - but it is just one line and will no require additional modifications if properties are added, removed or their type/default value is changed.
There you go,
have fun
Yours,
<><<br mode="hold" />Itai
|
|
|
|
|
Itai Basel wrote: Don't have the attention span to post as article
Then post it as a Tip/Trick[^]
where do these people come from?
[edit]
I see from the profile - a lurker for 8 years with never a message, impressive. Must have been a bitch trying to find your old password
Do use the tip/trick area as it is specifically designed for those with limited attention spans and good ideas. They do get vetted and need to be released by a gold member but this one would have no problem at all. Keep it up!
[/edit]
Never underestimate the power of human stupidity
RAH
|
|
|
|