Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
.NET
.NET Core
.NET MAUI
.NET Standard
Active Directory
ADO.NET
Agile Development
AI
AJAX
AlbertAGPT
Alchemy
Alexa Skills
Algorand
Algorithms in C#
Android
Angular
ArcObject
ASP.NET
ASP.NET Core
Augmented Reality
Avalanche
AWS
Azure
Backbonejs
Big Data
BizTalk Server
Blazor
Blockchain
Bootstrap
Bot Framework
Business
Business Intelligence(BI)
C#
C# Corner
C# Strings
C, C++, MFC
Career Advice
Careers and Jobs
Chapters
ChatGPT
Cloud
Coding Best Practices
Cognitive Services
COM Interop
Compact Framework
Copilot
Cortana Development
Cosmos DB
Cryptocurrency
Cryptography
Crystal Reports
CSS
Current Affairs
Custom Controls
Cyber Security
Data Mining
Data Science
Databases & DBA
Databricks
Design Patterns & Practices
DevExpress
DevOps
DirectX
Dynamics CRM
Enterprise Development
Entity Framework
Error Zone
Exception Handling
F#
Files, Directory, IO
Flutter
Games Programming
GDI+
General
Generative AI
GO
Google Cloud
Google Development
Graphics Design
Graphite Studio
Hardware
Hiring and Recruitment
HoloLens
How do I
HTML 5
Infragistics
Internet & Web
Internet of Things
Ionic
Java
Java and .NET
JavaScript
JQuery
JSON
JSP
Knockout
Kotlin
Langchain
Leadership
Learn .NET
Learn iOS Programming
LINQ
Machine Learning
Metaverse
Microsoft 365
Microsoft Fabric
Microsoft Office
Microsoft Phone
Microsoft Teams
Mobile Development
MongoDB
MuleSoft
MySQL
NEAR
NetBeans
Networking
NFT
NoCode LowCode
Node.js
Office Development
OOP/OOD
Open Source
Operating Systems
Oracle
Outsourcing
Philosophy
PHP
Polygon
PostgreSQL
Power Apps
Power Automate
Power BI
Power Pages
Printing in C#
Products
Progress
Progressive Web Apps
Project Management
Public Speaking
Python
Q#
QlikView
Quantum Computing
R
React
React Native
Reports using C#
Robotics & Hardware
RPA
Ruby on Rails
RUST
Salesforce
Security
Servers
ServiceNow
SharePoint
Sharp Economy
SignalR
Smart Devices
Snowflake
Software Architecture/Engineering
Software Testing
Solana
Solidity
Sports
SQL
SQL Server
Startups
Stratis Blockchain
Swift
SyncFusion
Threading
Tools
TypeScript
Unity
UWP
Visual Basic .NET
Visual Studio
Vue.js
WCF
Wearables
Web API
Web Design
Web Development
Web3
Windows
Windows Controls
Windows Forms
Windows PowerShell
Windows Services
Workflow Foundation
WPF
Xamarin
XAML
XML
XNA
XSharp
Register
Login
0
Answer
Changing value in variable from another called class
David G
3y
648
1
Reply
Hey all I have been racking my brain trying to figure out what it is I am missing from the code below in order for the value "7:50 pm" to be placed into the variable theCntDownTimerTime from the TinyWS.cs class.
Let say the code below starts out with the time "7:00 pm".
I send the command from PostMAN (7:50 pm) and it picks it up just fine and the jsonSent.changeCNT does have the correct value of "7:50 pm". However, after this it seems to lose that value once the timer updates in the clockCoiuntDown.xaml.cs page for the theCntDownTimerTime.ToLower(). That value is still the same value (7:00 pm) as it was when the program started up and read my text file to get the default time.
clockCountDown.xaml.cs
namespace
laptopWatcher {
public
partial
class
clockCountDown: Window {
public
string
theCntDownTimerTime =
""
;
public
clockCountDown() {
InitializeComponent();
//get countdown time
theCntDownTimerTime = getTimeToStart();
}
private
void
Window_Loaded(
object
sender, RoutedEventArgs e) {
var desktopWorkingArea = SystemParameters.WorkArea;
this
.Left = desktopWorkingArea.Right -
this
.Width - 20;
this
.Top = desktopWorkingArea.Top +
this
.Height - 30;
var ts = ((60 - DateTime.Now.Second) * 1000 - DateTime.Now.Millisecond);
clockTimer.Tick +=
new
EventHandler(clockTimer_tick);
clockTimer.Interval =
new
TimeSpan(0, 0, 35);
clockTimer.Start();
//Populate the time once to show to user
bgTxt.Text = DateTime.Now.ToString(
"h:mm tt"
);
txt.Text = DateTime.Now.ToString(
"h:mm tt"
);
//check if time for count down
if
(txt.Text.ToLower().Equals(theCntDownTimerTime.ToLower())) {
_timer.Stop();
TimeSpan time = TimeSpan.Parse(theCntDownTimerTime);
cdownTimer((_time).TotalSeconds);
}
_tws =
new
TinyWS();
Thread t =
new
Thread(
new
ThreadStart(_tws.startWS));
t.Start();
}
private
void
clockTimer_tick(
object
sender, EventArgs e) {
bgTxt.Text = DateTime.Now.ToString(
"h:mm tt"
);
txt.Text = DateTime.Now.ToString(
"h:mm tt"
);
Console.WriteLine(txt.Text.ToLower() +
" vs "
+ theCntDownTimerTime.ToLower());
}
public
string
getTimeToStart() {
string
line;
using
(StreamReader sr =
new
StreamReader(Environment.CurrentDirectory +
"\\timestart.txt"
)) {
line = sr.ReadLine();
}
return
line.ToLower();
}
// etc etc....
}
}
TinyWS.cs
namespace
laptopLogin {
class
TinyWS: clockCountDown {
HttpListener listener =
new
HttpListener();
clockCountDown ccd =
new
clockCountDown();
public
class
receivedData {
public
string
changeCNT {
get
;
set
;
}
public
int
timeAdd {
get
;
set
;
}
public
bool
internet {
get
;
set
;
}
public
bool
shutdownNow {
get
;
set
;
}
}
public
void
startWS() {
var prefixes =
new
List <
string
> () {
"http://*:8888/"
};
foreach
(
string
s
in
prefixes) {
listener.Prefixes.Add(s);
}
listener.Start();
Console.WriteLine(
"Listening..."
);
while
(
true
) {
HttpListenerContext context = listener.GetContext();
HttpListenerRequest request = context.Request;
string
documentContents;
using
(Stream receiveStream = request.InputStream) {
using
(StreamReader readStream =
new
StreamReader(receiveStream, Encoding.UTF8)) {
documentContents = readStream.ReadToEnd();
}
}
Console.WriteLine($
"Recived request for {request.Url}"
);
Console.WriteLine(documentContents);
HttpListenerResponse response = context.Response;
//Check out the response
receivedData jsonSent = JsonConvert.DeserializeObject < receivedData > (documentContents);
if
(jsonSent.changeCNT.ToLower() != ccd.getTimeToStart()) {
//The default shutdown time has changed so update the txt file.
using
(StreamWriter sw =
new
StreamWriter(Environment.CurrentDirectory +
"\\timestart.txt"
,
false
)) {
sw.WriteLine(jsonSent.changeCNT);
}
ccd.theCntDownTimerTime = jsonSent.changeCNT;
}
//Reply
string
responseString =
"hit"
;
byte
[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
response.ContentLength64 = buffer.Length;
Stream output = response.OutputStream;
output.Write(buffer, 0, buffer.Length);
output.Close();
}
}
public
void
stopWS() {
listener.Stop();
}
}
}
So what would I be missing? It looks fine to me but just doesn't store the value over.
I also have tried:
public
string
theCntDownTimerTime =
""
;
public
string
_newCNT {
get
{
return
theCntDownTimerTime;
}
set
{
theCntDownTimerTime = value;
}
}
And on the TinyWS.cs page I have it sending by like ccd._newCNT = jsonSent.changeCNT;
But that also does not hold the new value.
Post
Reset
Cancel
Answers (
0
)
Next Recommended Forum
Help out with this code please. Image from datagridview to desktop.
Converting VB code to C#