473,732 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Switch case statement error: A constant value is expected

22 New Member
Hi all
I have a TreeView in a MDI Apps. When the Nodes is selected it displays a MDI child form.

I want to use a switch statement to determine which node is selected and to load or show the correct MDI child form.

Here is part of the code:

private void treeViewMDI_Aft erSelected (object sender, TreeViewEvenArg s e)
{
frmAB frmAB1 = new frmAB ();
frmUDC frmUDC1 = new frmUDC();

switch(e.Node.T ext)
{
case frmAB1.Text: // This did not compile
frmAB1.MDIParen t=this;
frmAB1.show();
break;

case frmUDC1.Text: //This did not compile
frmUDC1.MDIPare nt=this;
frmUDC1.show();
break;
}
}


The line of code that did not compile is stated above.

Please assist or point me to right direction to solve this problem.

Marsh
Nov 8 '07 #1
4 19605
r035198x
13,262 MVP
Hi all
I have a TreeView in a MDI Apps. When the Nodes is selected it displays a MDI child form.

I want to use a switch statement to determine which node is selected and to load or show the correct MDI child form.

Here is part of the code:

private void treeViewMDI_Aft erSelected (object sender, TreeViewEvenArg s e)
{
frmAB frmAB1 = new frmAB ();
frmUDC frmUDC1 = new frmUDC();

switch(e.Node.T ext)
{
case frmAB1.Text: // This did not compile
frmAB1.MDIParen t=this;
frmAB1.show();
break;

case frmUDC1.Text: //This did not compile
frmUDC1.MDIPare nt=this;
frmUDC1.show();
break;
}
}


The line of code that did not compile is stated above.

Please assist or point me to right direction to solve this problem.

Marsh
Like the error message says, you have to use a constant value for your cases. This is to ensure that the compiler can enforce the rule which says that "no two case constants within the same switch". If you use variables then the there is a possibility of breaking the rule.
Nov 8 '07 #2
balabaster
797 Recognized Expert Contributor
Like the error message says, you have to use a constant value for your cases. This is to ensure that the compiler can enforce the rule which says that "no two case constants within the same switch". If you use variables then the there is a possibility of breaking the rule.
Hmm...doesn't look like you have this ability in C#. However, in VB we can do:

Expand|Select|Wrap|Line Numbers
  1. Select Case SomeObjectParameter 
  2.   Case Is = SomeSecondObject.Parameter
  3.     'Do Stuff
  4.   Case Is = SomeThirdObject.Parameter
  5.     'Do Stuff
  6.   Case Else
  7.     'Do something else
  8. End Select
Nov 8 '07 #3
r035198x
13,262 MVP
Hmm...doesn't look like you have this ability in C#. However, in VB we can do:

Expand|Select|Wrap|Line Numbers
  1. Select Case SomeObjectParameter 
  2.   Case Is = SomeSecondObject.Parameter
  3.     'Do Stuff
  4.   Case Is = SomeThirdObject.Parameter
  5.     'Do Stuff
  6.   Case Else
  7.     'Do something else
  8. End Select
VB's case is closer to C#'s if else than to the switch.
Nov 9 '07 #4
Plater
7,872 Recognized Expert Expert
Oh VB nad your butchering of standard coding concepts.
That is not a switch/case statement
Nov 9 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

21
7650
by: Andy | last post by:
Can someone tell me if the following Switch...Case construct is valid? I'm wanting to check for multiple values in the Case statement without explicitly listing each values. So for example, will case 1-35: work? If this would work, will it consider all numbers between 1 and 35 inclusive of 1 and 35? Please let me know if the following will work below: switch(state){ case 1-35: case 37:
2
1451
by: Guy Noir | last post by:
Hello again. I'm assuming I'm doing something dumb here.... When I try and add a default: case to my switch statement, I get an error that says "Error 1 Syntax error, '(' expected" and the red squiggly is under the : after the word default. Can anyone see my silly error? Thanks for the extra set of eyes! -Guy Here is the code for the entire method:
7
3535
by: sam_cit | last post by:
Hi Everyone, In the following code, i have a common action for three switch cases, is there any other better way to write the three values in a single case? Thanks in advance #include <stdio.h>
2
2712
by: =?Utf-8?B?QnJpYW4gQ29vaw==?= | last post by:
New programmer lost!!! I need some help with a switch/Case statement; I need to use OpenFileDialog to open a file, If the file extension is .dat, then do x. If the file extension is .rtf, then do y. if the file extension is .txt, then do z.
1
2827
by: Cloudbender | last post by:
how to create a program in c that shows your zodiac sign by using switch case statement. heres an example output: 1 - jan. 2 - feb. 3 - mar. 4 - apr. 5 - may 6 - jun. 7 - jul. 8 - aug.
2
5640
by: gooner | last post by:
Hey I'm new to linux, and for an assignment I have to make a switch case statement to output microprocessor workload. When the script is run it should output one of the following results: >83 - over work load >75 - medium work load >50 - average workload <50 - under working
4
1995
by: DaemonCoder | last post by:
I need some suggestions on how to betterer write the following code. I tried the array of arguments, and san through for a match then strcmp but i get massive errors. The code below is the only way i could get it to work.. if some one has any sugestions on hjow to make this better please let me know... The definiton of the class was left out intentionally. if you need this please let me know int EXIT=1; int main() { System system...
13
11826
by: Satya | last post by:
Hi everyone, This is the first time iam posting excuse me if iam making any mistake. My question is iam using a switch case statement in which i have around 100 case statements to compare. so just curious to find out is it effective to use this method?? or is there is any other alternative method present so that execution time and code size can be reduced?? Thanks in advance.
6
16183
HaLo2FrEeEk
by: HaLo2FrEeEk | last post by:
I'm trying to make a switch statement that will handle hotkeys assigned to different functions in my application. The hotkeys will be user-changable, so I've got them saved as application settings. I have a class that allows the program to hook the keyboard and accept the hotkeys even if the program doesn't have focus. In the event handler for that hook I'm trying to create a switch statement that looks like this: switch (e.KeyCode) ...
0
8946
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9181
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6735
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.