-2

I have a class file which contains a function to hash an input string.

using System;
using System.Security.Cryptography;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XHD_Console
{
    public class HashingSystem
    {
        public static string Sha256(string text)
        {
            string hashString = string.Empty;
            //code for hashing here, contains some things i'd rather not release.
            return hashString;
        }
    }
}

I want to call the sha256 function from a form, intellisense detects the class HashingSystem, but not the function. Is there a reason? I've read it needs to be static, done that but to no avail. Both classes are in the same namespace, but the class hashingsystem has it's own file, hashingsystem.cs

To call the function:

private void submit_Click(object sender, EventArgs e){
    this.EnteredPassword = HashingSystem.sha256(input_Password.Text);
    this.DialogResult = DialogResult.OK;
    this.Close();
}
Leo Skingsley
  • 43
  • 1
  • 6

2 Answers2

7

You need to call static members against the class, not against an instance of a class. So you need to use:

HashingSystem.sha256("texthere");

Also, consider changing:

class HashingSystem

to:

public class HashingSystem

Classes are internal by default. I would recommend you always be explicit about visibility (i.e. always specify internal, public or private).

mjwills
  • 23,389
  • 6
  • 40
  • 63
  • It's public static class, but calling it like `HashingSystem.sha256(string)` gives me an error; the name 'sha256' does not exist in the current context – Leo Skingsley Aug 24 '17 at 12:09
  • Please update your post to include the **entire method of the calling code**, and the actual `HashingSystem` class (since the `HashingSystem` class in your post is definitely **not** public). Also please include a screenshot of the compiler error you are receiving. – mjwills Aug 24 '17 at 12:10
  • There's one class HashingSystem. I have tried making the class public which did not work. – Leo Skingsley Aug 24 '17 at 12:15
3

Are you trying to do this?

    HashingSystem hs = new HashingSystem();
    hs.sha256("Hello World"); //This wont work as static methods cannot be called via instances

Use the below way instead

    HashingSystem.sha256("Hello world");//Calling directly via class
kkakroo
  • 1,043
  • 2
  • 11
  • 21