473,407 Members | 2,306 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Static Method

Hi I have the following code

private string GetUser()
{
Decryption decrypt = new Decryption();
return (string) decrypt.DecodeText("agyL70eOPb=", key);
}

which calls this static method

public class Decryption
{
public static string DecodeText(string encrypted, string source)
{
string decrypted;
TripleDESCryptoServiceProvider des;
MD5CryptoServiceProvider hashmd5;
byte[] pwdhash, buff;
hashmd5 = new MD5CryptoServiceProvider();
pwdhash = hashmd5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(s ource));
hashmd5 = null;
des = new TripleDESCryptoServiceProvider();
des.Key = pwdhash;
des.Mode = CipherMode.ECB;
buff = Convert.FromBase64String(encrypted);
decrypted = ASCIIEncoding.ASCII.GetString(des.CreateDecryptor
().TransformFinalBlock(buff, 0, buff.Length));
des = null;
return decrypted;
}
}
Is there a way to get rid of the 'new' line from the calling code

ie

private string GetUser()
{
return (string) DecodeText("agyL70eOPb=", key);
}

does not seem to work.

Nov 16 '05 #1
5 1170
how about:

return (string) Decryption.DecodeText("agyL70eOPb=", key);

This will work ONLY if DecodeText is declared static (which it is in
your example).
Nov 16 '05 #2
Remigiusz Dybka napisał(a):
how about:

return (string) Decryption.DecodeText("agyL70eOPb=", key);

This will work ONLY if DecodeText is declared static (which it is in
your example).


....and of course the cast to string is unneeded :) - sorry, forgot that.
Nov 16 '05 #3
used

return Library.Decryption.DecodeText("agyL70eOPb=", key);

but I get the compile error

An object reference is required for the nonstatic field, method, or property
'Library.Decryption.DecodeText(string, string)'

can't see why as the DecodeText is static?


"Remigiusz Dybka" wrote:
Remigiusz Dybka napisał(a):
how about:

return (string) Decryption.DecodeText("agyL70eOPb=", key);

This will work ONLY if DecodeText is declared static (which it is in
your example).


....and of course the cast to string is unneeded :) - sorry, forgot that.

Nov 16 '05 #4
Sorry, my fault, I was referencing the wrong Library!!!

"jez123456" wrote:
used

return Library.Decryption.DecodeText("agyL70eOPb=", key);

but I get the compile error

An object reference is required for the nonstatic field, method, or property
'Library.Decryption.DecodeText(string, string)'

can't see why as the DecodeText is static?


"Remigiusz Dybka" wrote:
Remigiusz Dybka napisał(a):
how about:

return (string) Decryption.DecodeText("agyL70eOPb=", key);

This will work ONLY if DecodeText is declared static (which it is in
your example).


....and of course the cast to string is unneeded :) - sorry, forgot that.

Nov 16 '05 #5
This code will not compile in C# as it is illegal to call a static method through an object reference. You absolutely don't need the "new" line but you have to call through the type name, i.e.

private string GetUser()
{
return Decryption.DecodeText("agyL70eOPb=", key);
}

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hi I have the following code

private string GetUser()
{
Decryption decrypt = new Decryption();
return (string) decrypt.DecodeText("agyL70eOPb=", key);
}

which calls this static method

public class Decryption
{
public static string DecodeText(string encrypted, string source)
{
string decrypted;
TripleDESCryptoServiceProvider des;
MD5CryptoServiceProvider hashmd5;
byte[] pwdhash, buff;
hashmd5 = new MD5CryptoServiceProvider();
pwdhash = hashmd5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(s ource));
hashmd5 = null;
des = new TripleDESCryptoServiceProvider();
des.Key = pwdhash;
des.Mode = CipherMode.ECB;
buff = Convert.FromBase64String(encrypted);
decrypted = ASCIIEncoding.ASCII.GetString(des.CreateDecryptor
().TransformFinalBlock(buff, 0, buff.Length));
des = null;
return decrypted;
}
}
Is there a way to get rid of the 'new' line from the calling code

ie

private string GetUser()
{
return (string) DecodeText("agyL70eOPb=", key);
}

does not seem to work.

Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Murat Tasan | last post by:
so here is another general question about java... why can't you declare an abstract static method. i can envision the case (indeed i have experienced the case) where one would want an...
5
by: Chris | last post by:
Hi I have a scenario where I've created another AppDomain to dynamically load a DLL(s) into. In this newly loaded DLL I want to call a static method on a class. The problem arise is that I have...
1
by: baylor | last post by:
In C#, an interface cannot mark any method as static. i'm told the ILASM supports it but i've never tested that Two questions. First, why? OK, i've heard the reason about interfaces being...
3
by: Jay | last post by:
Why are there static methods in C#. In C++ static was applied to data only (I believe) and it meant that the static piece of data was not a part of the object but only a part of the class (one...
8
by: Fernando Lopes | last post by:
Hi there! Someone has some code sample about when is recommend use a statis method? I know this methos don't want to be initialized and all but I want to know when I need to use it. Tks....
4
by: Dave | last post by:
I used the following class and .aspx code below to understand how static works on variables and methods taken from...
2
by: superseed | last post by:
Hi, I'm pretty new to C#, and I'm quite stuck on the following problem. I would like to add to my application a Windows.Form (singleton) on which I could display a message of one of the...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
4
by: Steffen Bobek | last post by:
Extension methods are made for use with instances. I'd like to "misuse" them as static methods, too. Let me tell you my ambition: I use an extension method to serialize objects somehow like this:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.