C# - How do I put the Function Regex working? [closed] - c#

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Good morning,
I am doing a page for my intership and I found this function but it isn't working, can someone help?
Thank you in advance.
string _id = this.txtIdGrupo.Text;
if (!Regex.IsMatch(_id, #"^\d+$"))
return false;
The output says this:
error CS0103: The name 'Regex' does not exist in the current context

this is not an issue with the bit of code you showed.
This is a namespace issue at the very top of your file.
The solution should be https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/namespaces/
using System;
using System.Text.RegularExpressions;
public class Test
{
public static void Main ()
{
var isNumeric1 = IsNumeric("1");
Console.WriteLine(isNumeric1);
var isNumeric2 = IsNumeric("HelloWorld");
Console.WriteLine(isNumeric2);
//call IsNumeric with the value of this.txtIdGrupo.Text like this
//var isNumeric = IsNumeric(this.txtIdGrupo.Text);
}
private static bool IsNumeric(string str)
{
string id = str;
if (!Regex.IsMatch(id, #"^\d+$"))
return false;
return true;
}
}
See it in action:
https://dotnetfiddle.net/I7cAKs
notice the "using System.Text.RegularExpressions"
Using tools like Resharper and similar will definitely give you hints and will improve your day to day !

Make sure you have the System.Text.RegularExpressions
is imported

Related

Problems with Unity Random Function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Good evening, I was working on a graphic adventure and I want to make a random function with a 4 digit code, turns out that as I am a very novice programmer, I crashed with an inconvenient
private void CheckResults()
{
if (codeSequence = RandomCode.RanCode.ToString())
{
SoundManager.PlaySound("nice");
}
else
{
SoundManager.PlaySound("wrong");
}
"RandomCode.RanCode" is a int void, and adding "ToString" will shot an error saying
"RanCode is a method which is not valid in the given context"
This is the RandomCode void:
public void RanCode()
{
Code = Random.Range(1111, 9999);
}
If anyone out there has any ideas or solves, I will be eternally grateful
There's no such thing as an "int void". A method either returns nothing (void) or returns a value (in your case you seem to expect int).
// Returns nothing
// |
// v
public void RanCode()
{
// assigns result of `Range` to `Code` property/field
Code = Random.Range(1111, 9999);
}
If you want to also return the value, you need to rewrite your method like this:
public int RanCode()
{
Code = Random.Range(1111, 9999);
return Code;
}
You do have another issue here:
RandomCode.RanCode.ToString()
To call a method you need (), so it should be this:
RandomCode.RanCode().ToString()
Also, for equality checks you want == not = (which is assignment).
I do wonder why you're assigning a value to Code here. If we use your existing code (without my fix above), I guess that perhaps you intended something like this instead?:
private void CheckResults()
{
RandomCode.RanCode();
if (codeSequence == RandomCode.Code.ToString())
{
SoundManager.PlaySound("nice");
}
else
{
SoundManager.PlaySound("wrong");
}
}

Can not write text to a file [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
So, basically, I am trying to make a save point in my text rpg. I wanted to do it by using a .txt file and jumping to each method that way. However, when I try and put in the path of the file and the string contents, it returns a CS0029 error and states that it can not convert a void to a string.
public void PickingUpScroll()
{
savePoint = "PickingUpScroll";
saveData = File.WriteAllText(savePath, savePoint);
hasFireMagic = true;
Console.WriteLine("You approach the strange scroll in your room and pick it up. A strange light eminates from the foreign symbols on the paper. You feel a warmth in your hands.\nLooking down, you notice they are glowing with a strange light, almost like fire.");
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("You have accquired 'Fire Magic'");
Console.ForegroundColor = ConsoleColor.White;
if (hasWeapons == false)
{
Console.WriteLine("Do you approach the weapons?\n[1] Yes [2] No");
int scrollToSwordCheck = Convert.ToInt32(Console.ReadLine());
if (scrollToSwordCheck == 1)
{
PickingUpSword();
}
}
else
{
LeavingRoom();
}
}
The File.WriteAllText method is a void (it doesn't return any value)
https://learn.microsoft.com/en-us/dotnet/api/system.io.file.writealltext?view=net-5.0
You can get rid of the variable saveData in the line:
saveData = File.WriteAllText(savePath, savePoint);
and just use the method:
File.WriteAllText(savePath, savePoint);
(and keep in mind that you will need to instantiate and assign the variable savePath somewhere prior its usage)

CS1519 for C# in unity [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm new to coding and have encountered an error with the code and I'm unsure how to fix it. I'm using unity 2018 and it tells me -
error CS1519: Invalid token '=' in class, struct, or interface member declaration
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AutoCookies : MonoBehaviour {
public bool CreatingCookie = false;
public static CookieIncrease = 1;
public int InternalIncrease;
void Update() {
InternalIncrease = CookieIncrease;
if (CreatingCookie == false)
{CreatingCookie = true;
StartCoroutine(CreateTheCookie());
}
}
IEnumerator CreateTheCookie ()
{
GlobalCookie.CookieCount += InternalIncrease;
yield return new WaitForSeconds(1);
CreatingCookie = false;
}
}
I've looked at what the error actually means and I've tried to fix it but I've been unsuccessful. The code is designed to auto create cookies for a game I'm coding for my year 12 Major Works.
As Daniel White said, public static CookieIncrease = 1; is the problem.
There is no type definition for CookieIncrease.
public static int CookieIncrease = 1; is probably what you were going for.

Lacking await operators? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Error comes up for the method requiring await operators but where do I put them and what is an await operator and async and Task? :)
public async void OnLocationChanged(Location location)
{
_currentLocation = location;
if (_currentLocation == null)
{
_locationText.Text = "Unable to determine your location. Try again in a short while.";
}
else
{
_locationText.Text = string.Format("{0:f6},{1:f6}", _currentLocation.Latitude, _currentLocation.Longitude);
as
}
}
I want specific help in the example above :)
Just remove asycn keyword from method's signature !
public void OnLocationChanged(Location location)

Check Public Static Bool in C# [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
im kinda new to C# and i got a code that uses public static bool.
But how do i check it?
I tried to do this
public static bool CheckForInternetConnection()
{
try
{
using (var client = new WebClient())
{
using (var stream = client.OpenRead("http://www.google.com"))
{
return true;
}
}
}
catch
{
return false;
}
}
private async void Form1_Load(object sender, EventArgs e)
{
await Task.Delay(5000);
if (CheckForInternetConnection() = true)
{
}
}
And it gave me an error.
I confirm the comments: it's only a syntax error due to the lack of the 2nd = char in the if(...) conditional block. I've tested your code in a console project on the run and, with the 2nd = it works fine.

Categories