Search
Archives

You are currently browsing the archives for the C#, C, C++ category.

Archive for the ‘C#, C, C++’ Category

** SharePoint users be aware! The command “stsadm -o uninstall” works, and it doesn’t warn you, it simply uninstalls SharePoint!

I thought this was a commical status of a user on  the microsoft foumns, still a very good piece of information.   No warning!!! Oversight?

Multiline JavaScript Tip:

Ever wanted to have multi-line strings in JavaScript?  Ever been sitting there putting html into javascript and having finish out each like with a “+.  Well here is a way to perform the the return line without any breaking quote.  A continuous string across spanning multiple lines.  The simple answer is escape the end of the line with a backslash \  this will cause JavaScript to look for the char to escape and allow you to have clean looking HTML in JavaScipt without quotes.

 Multiline JavaScript Code Example:

 var multilineString =
“<table> \
      <tr> \
           <td> \
           </td> \
      </tr> \
</table>”;

Magic!!!!

  • Open Solution Explorer
  • Right Click Project and Hit Properties

    SolutionExplorer Project Properties

    SolutionExplorer Project Properties

  • Target Application Framework will be set to “.Net Framework 3.5 Client Profile” or “.Net Framework 4.0 Client Profile”  Change this to just “.Net Framework 3.5” or “.Net Framework 4.0”.

    Change Framework From ClientProfile to Regular

    Change Framework From ClientProfile to Regular

  • Click “Yes” on the popup in order to change the framework

    Choose Yes To Change Framework

    Choose Yes To Change Framework

  • Additional Notes: You may also need to change build framework to x64 or Any

Senario


Upgrading from WSS 3.0 or SharePoint 2007 to SharePoint 2010
I’ve encoutered this many times I figured I would add it up.

Problem Description


Only one instance of a ScriptManager can be added to the page.

Fix Explanation

SharePoint 2010 already has a ScriptManger in its defalut.master so if you extend form there you will need to remove the ScriptManger on your page and everything will work!

ScriptManger in default.master and v4.master

<asp:ScriptManager id="ScriptManager" runat="server" EnablePageMethods="false" EnablePartialRendering="true" EnableScriptGlobalization="false" EnableScriptLocalization="true" />


Modify the SharePoint 2010 ScriptManger properties from your project:

protected void Page_Init(object sender, EventArgs e){
ScriptManager current = ScriptManager.GetCurrent(Page);
current.AsyncPostBackTimeout = 36000;
current.EnablePartialRendering = true;
}

 

Senario

Problem Description:

The type or namespace name ‘ConfigurationManager’ does not exist in the namespace ‘System.Configuration’ (are you missing an assembly reference?)

Fix:

Go to Project References and add System.Configuration from the .Net tab

 


Problem description


Feature ‘optional parameter’ cannot be used because it is not part of the 3.0 C# language specification

Uable to use Optional parameters in a Project targeting .net 3.5 or .net 3.0. Often times the server will support optional parameters as well as Visual Studio 2010.


What are Optional Parameters


public void MyMethod(String requiredParam, String optionalParam = String.Empty, Int32 optionalInt = 0) {}
The parameters followed by = sign are known as optional parameters and value after the equals sign the default value, which was a nice feature of C that C# originally had not replicated.

It saves you form having to create several methods like this:

public void MyMethod(String requiredParam) { MyMethod(requiredParam, String.Empty, 0); }
public void MyMethod(String requiredParam, String optionalParam = String.Empty) { MyMethod(requiredParam, optionalParam, 0) }
public void MyMethod(String requiredParam, String optionalParam = String.Empty, Int32 optionalInt = 0) {}


My Specific Senario (SharePoint 2007 to 2010 upgrade)


My specific senario is a SharePoint 2010 Custom Project. Specifically a couple layouts based application pages.
I am upgrading form a SharePoint 2007 Solution. So optional parameters are also available in SharePoint 2007 and WSS 3.0.

Fix


Go to project properties -> Build Tab -> click button ‘Advanced’ -> set language version to ‘default’ -> save -> be happy 😉
Reference: http://stackoverflow.com/questions/1210679/can-you-use-optional-parameters-in-code-targeting-net-3-5

This article explains how to create custom serialization and deserialization events without having to inherit iSerializable.
This allows you to skip serialization on a field and then fill it in upon deserialization.


[XmlIgnore()]


class SomeClass {
    [XmlIgnore] int someID;
    public string someString;
}


Serializing Events


The gist is put these items before an property:

  • [Serializable()]
  • [NonSerialized()]

And these are events that get called by serialization: (place in front of methods)

  • [OnSerializing()]
  • [OnSerialized()]
  • [OnDeserializing()]
  • [OnDeserialized()]

Method structure:     internal void customMethod(StreamingContext context){}


Example Serializing Events

Example code from Microsoft:
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.ondeserializedattribute.aspx

// This is the object that will be serialized and deserialized.
[Serializable()]
public class TestSimpleObject
{
    // This member is serialized and deserialized with no change.
    public int member1;
    // The value of this field is set and reset during and
    // after serialization.
    private string member2;
    // This field is not serialized. The OnDeserializedAttribute
    // is used to set the member value after serialization.
    [NonSerialized()]
    public string member3;
    // This field is set to null, but populated after deserialization.
    private string member4;
    // Constructor for the class.
    public TestSimpleObject()
    {
        member1 = 11;
        member2 = "Hello World!";
        member3 = "This is a nonserialized value";
        member4 = null;
    }
    public void Print()
    {
        Console.WriteLine("member1 = '{0}'", member1);
        Console.WriteLine("member2 = '{0}'", member2);
        Console.WriteLine("member3 = '{0}'", member3);
        Console.WriteLine("member4 = '{0}'", member4);
    }
    [OnSerializing()]
    internal void OnSerializingMethod(StreamingContext context)
    {
        member2 = "This value went into the data file during serialization.";
    }
    [OnSerialized()]
    internal void OnSerializedMethod(StreamingContext context)
    {
        member2 = "This value was reset after serialization.";
    }
    [OnDeserializing()]
    internal void OnDeserializingMethod(StreamingContext context)
    {
        member3 = "This value was set during deserialization";
    }
    [OnDeserialized()]
    internal void OnDeserializedMethod(StreamingContext context)
    {
        member4 = "This value was set after deserialization.";
    }
}


Example Serializing Events Output



// Output:
// Before serialization the object contains:
// member1 = '11'
// member2 = 'Hello World!'
// member3 = 'This is a nonserialized value'
// member4 = ''
//
// After serialization the object contains:
// member1 = '11'
// member2 = 'This value was reset after serialization.'
// member3 = 'This is a nonserialized value'
// member4 = ''
//
// After deserialization the object contains:
// member1 = '11'
// member2 = 'This value went into the data file during serialization.'
// member3 = 'This value was set during deserialization'
// member4 = 'This value was set after deserialization.'


Conclusions


With these methods you can fully regualte how things are serialied:

  • How do I serialize a property but clear it upon deserialization?  Serialze property but dont deserialize?
    • Keep the attribute as normal in the serializeable class,  in the [OnDeserialized()] method clear out the value
  • How do exclude  a property from serialization but fill it in on deserialization?  Don’t serialze property but load/fill/set it on deserialize?
    • Set the attrbiute as [NonSerialized()]  in the serializeable class,  in the [OnDeserialized()] method clear set it, based on some information form serialization.

My goal was to figure out the difference between two DateTime objects, more specifically how many days form a specific date.  I Originally searched for DateTime.CompareTo hoping that would be the answer, however DateTime.CompareTo returns (-1, 0, 1) (Less Then, Equal To, Greater Then) respectivly.


Code


 

DateTime startTime = DateTime.Now;
DateTime endTime = DateTime.Now.AddSeconds( 75 );


TimeSpan span = endTime.Subtract ( startTime );
Console.WriteLine( "Time Difference (seconds): " + span.Seconds );
Console.WriteLine( "Time Difference (minutes): " + span.Minutes );
Console.WriteLine( "Time Difference (hours): " + span.Hours );
Console.WriteLine( "Time Difference (days): " + span.Days );


Output




Time Difference (seconds): 15
Time Difference (minutes): 1
Time Difference (hours): 0
Time Difference (days): 0


Other TimeSpan Options



Only a couple worth mentioning: 
Methods:

Public method Supported by the XNA Framework Supported by Portable Class Library Add Returns a new TimeSpan object whose value is the sum of the specified TimeSpan object and this instance.
Public method Supported by the XNA Framework Supported by Portable Class Library CompareTo(Object) Compares this instance to a specified object and returns an integer that indicates whether this instance is shorter than, equal to, or longer than the specified object.
Public method Supported by the XNA Framework Supported by Portable Class Library Subtract Returns a new TimeSpan object whose value is the difference between the specified TimeSpan object and this instance.
Public method Supported by the XNA Framework Supported by Portable Class Library ToString Converts the value of the current TimeSpan object to its equivalent string representation. (Overrides ValueType.ToString.)   [-][d.]hh:mm:ss[.fffffff]  where – if you want to show if there is a negative time span, d = days, and fffffff is fraction seconds.

 

Properties:

Public property Supported by the XNA Framework Supported by Portable Class Library Days Gets the days component of the time interval represented by the current TimeSpan structure.
Public property Supported by the XNA Framework Supported by Portable Class Library Hours Gets the hours component of the time interval represented by the current TimeSpan structure.
Public property Supported by the XNA Framework Supported by Portable Class Library Milliseconds Gets the milliseconds component of the time interval represented by the current TimeSpan structure.
Public property Supported by the XNA Framework Supported by Portable Class Library Minutes Gets the minutes component of the time interval represented by the current TimeSpan structure.
Public property Supported by the XNA Framework Supported by Portable Class Library Seconds Gets the seconds component of the time interval represented by the current TimeSpan structure.
Public property Supported by the XNA Framework Supported by Portable Class Library Ticks Gets the number of ticks that represent the value of the current TimeSpan structure.
Public property Supported by the XNA Framework Supported by Portable Class Library TotalDays Gets the value of the current TimeSpan structure expressed in whole and fractional days.
Public property Supported by the XNA Framework Supported by Portable Class Library TotalHours Gets the value of the current TimeSpan structure expressed in whole and fractional hours.
Public property Supported by the XNA Framework Supported by Portable Class Library TotalMilliseconds Gets the value of the current TimeSpan structure expressed in whole and fractional milliseconds.
Public property Supported by the XNA Framework Supported by Portable Class Library TotalMinutes Gets the value of the current TimeSpan structure expressed in whole and fractional minutes.
Public property Supported by the XNA Framework Supported by Portable Class Library TotalSeconds Gets the value of the current TimeSpan structure expressed in whole and fractional seconds.

View all options here: http://msdn.microsoft.com/en-us/library/system.timespan.aspx

References:

http://www.dotnetspider.com/resources/458-How-find-difference-between-two-Dates-C-or.aspx
http://msdn.microsoft.com/en-us/library/system.timespan.aspx
http://msdn.microsoft.com/en-us/library/1ecy8h51.aspx
http://msdn.microsoft.com/en-us/library/system.datetime.compareto.aspx