-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(); } 
11
  • It's just a normal windows C# form, it's for password entry, thus the hash function. Commented Aug 24, 2017 at 11:12
  • 1
    make HashingSystem a public class Commented Aug 24, 2017 at 11:14
  • 1
    If they really are in the same namespace and project it shouldn't matter that they are internal right. Commented Aug 24, 2017 at 11:16
  • 1
    The OP is most likely creating an instance of HashingSystem instead of calling it statically Commented Aug 24, 2017 at 11:21
  • 3
    Possible duplicate of Calling a Static method in C# Commented Aug 24, 2017 at 11:27

2 Answers 2

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).

Sign up to request clarification or add additional context in comments.

3 Comments

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
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.
There's one class HashingSystem. I have tried making the class public which did not work.
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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.