Posts Tagged ‘.net’
Extension method in .net
Extension methods enable developers to add a method to an existing class without deriving or affecting the existing class. This extension methods only support a sub routine or a function.
Example if we want to extend the functionality of the String type
-
Imports System.Runtime.CompilerServices
-
-
Module StringExtensions
-
-
<Extension()>
-
Public Sub Print(ByVal aString As String)
-
Console.WriteLine(aString)
-
End Sub
-
-
<Extension()>
-
Public Sub PrintAndPunctuate(ByVal aString As String,
-
ByVal punc As String)
-
Console.WriteLine(aString & punc)
-
End Sub
-
-
End Module
To use that function
-
Sub Main()
-
-
Dim example As String = "Example string"
-
example.Print()
-
-
example = "Hello"
-
example.PrintAndPunctuate(".")
-
example.PrintAndPunctuate("!!!!")
-
-
End Sub
C#.NET
If you notice the parameter there is a this keyword, that is how it done.
-
namespace ExtensionMethods
-
{
-
public static class StringExtensions
-
{
-
public static int Print(<strong>this </strong>String str)
-
{
-
Console.WriteLine(String );
-
}
-
}
-
}
-
-
string s = "Hello Extension Method";
-
s.Print();
Namespace or type specified in the project-level Imports ‘System.Xml.Linq’ doesn’t contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn’t use any aliases
There are 2 ways to remove this warning
First, you may need to convert the current .Net Framework used by the application to higher version, let’s say from .net framework 2 to 3.5
- Goto Property Pages of the web site project
- View Menu->Property Pages or press shift+F4
- Click the Build option from the left side
- Make sure the Target Framework is .Net Framework 3.5
Second, remove the namespace that shown in the warning in this case the “System.Xml.Linq” from the web.config
Error 1001. Exception occurred while initializing the installation. System.IO.FileNotFoundException
Posted by: admin in .NET, Deployment, Exception on June 22nd, 2010
This error occurs when the CustomActionData has a supplied value,
Example:
The value of the CustomActionData is /TARGETDIR=[TARGETDIR], what you need to do is to update it to /TARGETDIR=”[TARGETDIR]\”
How to get msi setup file location using installer class
Posted by: admin in .NET, Deployment on June 21st, 2010
To get the current msi setup path,
- Goto the setup project
- Right click and select view->custom actions
- Under Install Node, click the “primary output from….”
- Press F4 to show its property
- Add the /Source=[SourceDir] to the CustomActionData property
- Can then be access through the installer class
Context.Parameters(“Source”)
How to calculate Time span in .Net
Here’s how to code the time span;
Function ExecuteLargeRec() {
DateTime d1 = DateTime.Now
DateTime d2
TimeSpan ts
‘ Your query here / processing
d2 = DateTime.Now
ts = d1.Subtract(d2)
MessageBox.Show(ts.ToString())
}
The install location for prerequisites has not been set to
Posted by: admin in .NET, Deployment on June 9th, 2010
Follow this steps to solve this problem:
Update the Package Data
- Open the [Program Files]\Microsoft SDKs\Windows\v6.0A\Bootstrapper\Packages\DotNetFx35SP1 folder or %ProgramFiles(x86)%\Microsoft SDKs\Windows\v6.0A\Bootstrapper\Packages\DotNetFx35SP1 on x64 operating systems
- Edit the Product.xml file in Notepad.
- Paste the following into the < PackageFiles > element:
- Find the element for < PackageFile Name=”dotNetFX30\XPSEPSC-x86-en-US.exe” and change the PublicKey value to:
3082010A0282010100A2DB0A8DCFC2C1499BCDAA3A34AD23596BDB6CBE2122B79
4C8EAAEBFC6D526C232118BBCDA5D2CFB36561E152BAE8F0DDD14A36E284C7F163
F41AC8D40B146880DD98194AD9706D05744765CEAF1FC0EE27F74A333CB74E5EFE
361A17E03B745FFD53E12D5B0CA5E0DD07BF2B7130DFC606A2885758CB7ADBC85E
817B490BEF516B6625DED11DF3AEE215B8BAF8073C345E3958977609BE7AD77C13
78D33142F13DB62C9AE1AA94F9867ADD420393071E08D6746E2C61CF40D5074412
FE805246A216B49B092C4B239C742A56D5C184AAB8FD78E833E780A47D8A4B2842
3C3E2F27B66B14A74BD26414B9C6114604E30C882F3D00B707CEE554D77D208557
6810203010001 - Find the element for < PackageFile Name=”dotNetFX30\XPSEPSC-amd64-en-US.exe” and change the PublicKey value to the same as in step 4 above
- Save the product.xml file
Download and Extract the Core Installation Files
- Navigate to the following URL: http://go.microsoft.com/fwlink?LinkID=118080
- Download the dotNetFx35.exe file to your local disk.
- Open a Command Prompt window and change to the directory to which you downloaded dotNetFx35.exe.
- At the command prompt, type:
dotNetFx35.exe /x:.
This will extract the Framework files to a folder named “WCU” in the current directory.
Copy the contents of the WCU\dotNetFramework folder and paste them in the %Program Files%\Microsoft SDKs\Windows\v6.0A\Bootstrapper\Packages\DotNetFx35SP1 folder (%ProgramFiles(x86)%\Microsoft SDKs\Windows\v6.0A\Bootstrapper\Packages\DotNetFx35SP1 on x64 operating systems).
Note: Do not copy the WCU\dotNetFramework folder itself. There should be 5 folders under the WCU folder, and each of these should now appear in the DotNetFx35SP1 folder. The folder structure should resemble the following: DotNetFx35SP1 (folder)
-
- dotNetFX20 (folder
- dotNetFX30 (folder)
- dotNetFX35 (folder)
- dotNetMSP (folder)
- TOOLS folder)
- en (or some other localized folder)
- dotNetFx35setup.exe (file)
You may now delete the files and folders you downloaded and extracted in steps 2 and 4.
You can read more on the attached readme file.
Cannot write to the registry key in .Net
Public Function SetValue(ByVal Value As String, ByVal k As String) As String
Dim mReg As RegistryKey = Registry.LocalMachine.OpenSubKey(“Software”, RegistryKeyPermissionCheck.ReadWriteSubTree, Security.AccessControl.RegistryRights.FullControl)
Try
mReg.OpenSubKey(My.Application.Info.CompanyName & “\” & My.Application.Info.Description, True).SetValue(k, Value)
Return True
Catch ex As Exception
Throw ex
End Try
End Function
How to get Month Name in .net
Posted by: admin in .NET, Date Formatting on May 25th, 2010
dim dt as Datetime
dt = Datetime.Today
Messabox.show(dt.ToString(“MMM”))
Export to Excel with error Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046}
Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80040154.
This error occurs when the Microsoft office has not installed in the target machine.
You can export the file to csv.
Could not load file or assembly ‘ADODB,Version’
Solution 1:
go to a machine that has this folder and copy it (or the adodb.dll file if you have the folder) to the machines that cannot find the adodb file
Go into this folder and into the primary interop folder
copy this folders contents to the GAC (c:\windows\assembly)
your app should find the adodb.dll now

